How to Master CentOS Commands: The Ultimate Cheat Sheet
- Mastering CentOS commands can help you effectively manage CentOS systems, perform common tasks, and troubleshoot issues.
- Process management is streamlined using commands like ps and top, which help monitor and troubleshoot system performance in real time.
- Each command is described with clear explanations, making it easy to understand their purpose and usage.
CentOS, a popular community-driven Linux distribution, faced a significant change when the CentOS Project discontinued CentOS Linux in favor of CentOS Stream. This transition left many users and organizations seeking a reliable alternative. AlmaLinux and Rocky Linux emerged as leading options, offering seamless compatibility with CentOS and providing long-term support and security for enterprise environments.
For administrators and IT professionals working with CentOS or its successors, a solid understanding of essential commands is crucial for effective management. This cheat sheet aims to provide an overview of the most frequently used CentOS commands, helping you efficiently manage your systems, perform common tasks, and troubleshoot issues.
Whether you’re using CentOS, AlmaLinux, or Rocky Linux, these commands will form the foundation of your system administration tasks.
Basic System Administration CentOS Commands
yum: Package Management
yum is the package manager for CentOS and RHEL, which helps to install, update, and remove packages. However, AlmaLinux and Rocky Linux use dnf (Dandified Yum) as the default package manager. dnf is the successor to yum and has the same syntax as yum.
To install a package on CentOS systems, you can run:
# yum install package_name
To remove a package, you can use:
# yum remove package_name
systemctl: Service Management
systemctl is the service manager which controls the startup, stopping, and restarting of system services. It provides a unified interface for managing various system processes.
To start a service, you can use:
# systemctl start service_name
To stop the service, you can use:
# systemctl stop service_name
You can also enable the service to start at boot up:
# systemctl enable service_name
To check the status of service, you can use:
# systemctl status service_name
firewall-cmd: Firewall Management
Another popular CentOS command is firewall-cmd, a command-line tool for configuring the firewall. It allows you to define rules for incoming and outgoing network traffic, protecting your system from unauthorized access.
To display the status of the firewall, run the command:
firewall-cmd –state
Output:
running
To allow traffic on a specific port, you can run the following command.
firewall-cmd –permanent –add-port=port_number/tcp
useradd, userdel, usermod: User Management
useradd creates new user accounts, specifying their login name, password, and other attributes. usermod modifies existing user accounts, changing their login name, password, or group memberships. userdel deletes user accounts, optionally removing their home directory.
To add a new user to the system, you can use the command:
# useradd username
To remove a user from the system, you can run the command.
# userdel username
To change the login name for a user account, you can use the usermod command with -l option.
# usermod -l username
passwd: Change Passwords
The passwd command is used to change the password for a specific user account. It prompts the user to enter their current password and set a new password. However, with root access, you can directly set a new one.
pwd username
selinux: Security Enhanced Linux
selinux is a security module that provides mandatory access control (MAC) for CentOS. It restricts the access of processes to system resources, enhancing security and preventing unauthorized access.
Displays the current status of SELinux.
sestatus
scp – Secure Copy Files
scp securely transfers files between remote systems over SSH, preserving file attributes and ensuring encrypted data transmission. With syntax like scp file user@host:/destination, administrators can quickly copy files between servers. It’s widely used for backups, migrations, and transferring data across systems, making it essential for remote file management.
Syntax: scp [source] [destination]
To copy a single file to a remote server, you can run:
scp file.txt [email protected]:/home/user
This command copies the file named “file.txt” to “/home/user” directory in the remote server “192.168.1.10”.
To copy a directory recursively, you can use the -r parameter.
scp -r /local/dir user@remote:/remote/dir
This command copies all files and sub-directories present in the local directory to the target remote directory.
Process Management CentOS Commands
top: Real-time System Monitoring
top provides real-time insights into system processes, CPU usage, memory consumption, and more. It continuously updates and allows users to monitor which processes consume the most resources. System administrators use top to track system performance and troubleshoot performance bottlenecks. Sorting processes by memory or CPU usage is key to identifying resource-logging processes.
Syntax: top
Example: Start top
top
ps: Process Monitoring
ps provides a snapshot of the running processes on a system. With ps -ef, users can view detailed information such as process IDs, user information, and CPU usage. It’s often used to identify and track specific processes, find orphaned or zombie processes, or monitor processes owned by specific users. It’s crucial for managing and analyzing system workloads.
Syntax: ps [options]
The following command shows all processes on the system.
ps -ef
Output:
To display processes for a specific user, you can use:
ps -u root
This command shows processes running as root.
kill: Terminate Processes
kill is used to terminate processes by sending signals. It’s commonly used with kill -9, which forcefully kills unresponsive processes. By using the process ID (PID), administrators can stop a specific process without affecting others. The command is critical when processes hang or consume too many resources, ensuring the stability of the system by removing problematic tasks.
Syntax: kill [signal] [PID]
To gracefully stop a process having ID “1234”, you can use:
kill 1234
To forcefully kill a specified process, you can use:
kill -9 1234
Additional CentOS Commands
df: Disk Usage
The df command displays information about the disk usage of mounted filesystems. Using df -h shows human-readable output, providing details like total, used, and available space. It’s useful for monitoring storage availability and quickly identifying filesystems that are running out of space. This command is critical for managing system storage and preventing disk space exhaustion.
Syntax: df [options]
To check disk usage in human-readable format, you can use this command:
df -h
To show filesystem types, you can use:
df -T
crontab: Schedule Tasks
crontab allows users to schedule recurring jobs or tasks, automating repetitive tasks like backups, updates, or log cleaning. Administrators edit crontab files using crontab -e to define tasks that run at specific intervals, like daily, weekly, or monthly. It’s widely used for maintenance activities, enabling efficient system management without manual intervention.
Syntax: crontab -e
Example:
Edit crontab to schedule a job:
crontab -e
List scheduled tasks:
crontab -l
tar – Archive and Extract Files
tar is used to create and extract tarball archives. With tar -cvf, administrators can bundle multiple files or directories into a single compressed file, while tar -xvf extracts them. It’s heavily used for creating backups, transferring large groups of files, or preserving directory structures. It’s a fundamental tool for file management and data archiving on CentOS.
Syntax: tar [options] [archive_name] [files]
Example:
Create an archive:
tar -cvf backup.tar /var/www
Extract an archive:
tar -xvf backup.tar
grep – Search Text
grep searches for patterns in files, making it indispensable for analyzing logs or configuration files. With grep ‘pattern’ file.txt, users can filter output to display only lines containing a specific string. It supports regular expressions and recursive search, making it highly versatile. Administrators use grep to troubleshoot issues by searching logs for errors or specific keywords.
Syntax: grep [pattern] [file]
Example:
Search for a keyword in a file:
grep ‘error’ /var/log/messages
Search recursively in directories:
grep -r ‘keyword’ /etc
Other Useful CentOS Commands
File Management
cp, mv: Copying and Moving Files
- The cp command copies files and directories from one location to another.
- The mv command moves files and directories, renaming them if necessary.
rm: Removing Files
- The rm command deletes files and directories. Use caution when using this command, as deleted files cannot be recovered.
find: Searching for Files
- The find command searches for files and directories based on various criteria, such as name, size, modification time, and location.
Final Thoughts
This cheat sheet provides a quick reference for essential CentOS commands frequently used in system administration. Understanding these commands and their proper usage will enhance your ability to efficiently manage CentOS, AlmaLinux, and Rocky Linux environments.
This is just the beginning! For detailed information on these commands and additional options, visit these man pages.
As you continue to utilize CentOS in your enterprise, consider extending its lifecycle with TuxCare’s Extended Lifecycle Support. TuxCare offers vendor-grade security patches for CentOS 6, CentOS 7, and CentOS 8, ensuring your systems remain secure and up to date even after the end of life.
Maintaining a secure and updated Linux environment is crucial, especially when official support ends. By using TuxCare’s ELS, system administrators can continue to run legacy Linux systems safely without worrying about vulnerabilities. This service ensures that systems remain compliant and secure, reducing the need for immediate migrations while providing ample time to plan future upgrades.
Other supported end-of-life Linux distributions by TuxCare are: CentOS Stream 8, Oracle Linux 6, Oracle Linux 7, Ubuntu 16.04, and Ubuntu 18.04.
Send questions to our Linux security experts to learn more about extended lifecycle support offerings and how it can help your organization.