This page covers critical commands for administrative tasks, package management, and system service control.
46. sudo
- Execute a Command as Another User
Purpose: The sudo
command (substitute user do) allows a permitted user to execute a command as the superuser (root) or another user, as specified by the security policy. It's the primary way to perform administrative tasks without logging in as root directly.
Syntax: sudo COMMAND [ARGUMENTS]
Key Concept:
- Users must be configured in the
sudoers
file (or a file in/etc/sudoers.d/
) to usesudo
. - It typically prompts for the user's own password, not the root password.
Examples:
sudo dnf update # Update system packages sudo systemctl restart nginx # Restart the Nginx web server sudo nano /etc/hosts # Edit system configuration files
47. dnf
- DNF Package Manager
Purpose: dnf
(Dandified YUM) is the next-generation package manager for RPM-based distributions like Rocky Linux. It's used for installing, updating, removing, and managing software packages.
Syntax (Common Usage): sudo dnf COMMAND [PACKAGE_NAME...]
Key Commands:
dnf install PACKAGE
: Install a new package.dnf update
: Update all installed packages to their latest versions.dnf remove PACKAGE
: Uninstall a package.dnf search KEYWORD
: Search for packages by keyword.dnf info PACKAGE
: Display detailed information about a package.dnf repolist
: List configured software repositories.dnf clean all
: Clean up cached package data.
Examples:
sudo dnf install httpd sudo dnf update sudo dnf remove telnet dnf search "web server"
48. systemctl
- Control the Systemd System and Service Manager
Purpose: The systemctl
command is the primary tool for controlling and managing the systemd init system. It's used to manage services, check system status, and interact with the systemd journal.
Syntax (Common Usage): sudo systemctl COMMAND UNIT
Key Commands:
systemctl start SERVICE
: Start a service.systemctl stop SERVICE
: Stop a service.systemctl restart SERVICE
: Restart a service.systemctl enable SERVICE
: Enable a service to start automatically at boot.systemctl disable SERVICE
: Disable a service from starting at boot.systemctl status SERVICE
: Check the status of a service.systemctl is-active SERVICE
: Check if a service is active.systemctl list-units --type=service
: List all services.
Examples:
sudo systemctl start sshd sudo systemctl status firewalld sudo systemctl enable nginx systemctl is-active mariadb
49. hostname
- Show or Set the System's Hostname
Purpose: The hostname
command is used to display or set the system's hostname.
Syntax: hostname [OPTIONS] [NEW_HOSTNAME]
Key Options:
-f
: Display the FQDN (Fully Qualified Domain Name).-I
: Display all IP addresses of the host.
Examples:
hostname hostname -f sudo hostname new_server_name # Temporarily set hostname (resets on reboot)
To permanently change the hostname, use hostnamectl set-hostname NEW_HOSTNAME
(requires root/sudo).
50. history
- Display Command History
Purpose: The history
command displays a list of commands previously executed in the current shell session and typically previous sessions (stored in ~/.bash_history
or similar).
Syntax: history [OPTIONS]
Key Uses:
history
: Display the full command history.history NUM
: Display the last NUM commands.!NUM
: Execute the command at the given history number.!STRING
: Execute the most recent command starting with STRING.Ctrl+R
: Reverse-search through history.
Examples:
history history 10 !456 # Execute command number 456 from history !sudo # Execute the last command that started with 'sudo'