centos commands cheat sheet
Tara Nienow
CentOS Commands Cheat Sheet: Your Ultimate Guide to Managing CentOS Systems
centos commands cheat sheet is an essential resource for system administrators, developers, and IT professionals who work with CentOS Linux. Whether you're performing routine maintenance, troubleshooting issues, or deploying applications, having a solid understanding of core CentOS commands can significantly streamline your workflow. This comprehensive guide aims to provide a detailed overview of the most useful commands, organized into categories for easy reference. From system management to network configuration, this cheat sheet covers the essential commands needed to efficiently operate and manage CentOS servers.
Getting Started with Basic CentOS Commands
Before diving into advanced commands, it's crucial to familiarize yourself with basic commands that form the foundation of CentOS system management.
1. Checking System Information
- uname -a: Displays detailed kernel and system information.
- hostnamectl: Shows or sets the hostname.
- cat /etc/centos-release: Reveals the CentOS version.
- uptime: Shows how long the system has been running.
- lsb_release -a: Displays distribution-specific information.
2. Managing Files and Directories
- ls -l: Lists directory contents in long format.
- cd /path/to/directory: Changes the current directory.
- pwd: Prints the current working directory.
- cp source destination: Copies files or directories.
- mv source destination: Moves or renames files.
- rm -rf directory: Recursively deletes a directory and its contents.
- mkdir directory_name: Creates a new directory.
3. Managing Users and Permissions
- useradd username: Adds a new user.
- passwd username: Sets or changes user password.
- usermod -aG group username: Adds user to a group.
- groupadd groupname: Creates a new group.
- chmod 755 filename: Sets permissions.
- chown user:group filename: Changes ownership.
Package Management Commands
CentOS uses yum (CentOS 7) and dnf (CentOS 8 and later) for package management. Knowing how to install, update, and remove packages is fundamental.
1. Installing Packages
- yum install package_name (CentOS 7)
- dnf install package_name (CentOS 8+)
2. Removing Packages
- yum remove package_name (CentOS 7)
- dnf remove package_name (CentOS 8+)
3. Updating Packages and System
- yum update (CentOS 7)
- dnf update (CentOS 8+)
4. Searching for Packages
- yum search package_name (CentOS 7)
- dnf search package_name (CentOS 8+)
5. Listing Installed Packages
- yum list installed (CentOS 7)
- dnf list installed (CentOS 8+)
Service and Process Management
Managing services and processes is vital for maintaining server health and ensuring applications run smoothly.
1. Managing Systemd Services
- systemctl start service_name: Starts a service.
- systemctl stop service_name: Stops a service.
- systemctl restart service_name: Restarts a service.
- systemctl enable service_name: Enables service to start on boot.
- systemctl disable service_name: Disables service from starting on boot.
- systemctl status service_name: Checks the status of a service.
2. Listing Processes
- ps aux: Displays all running processes.
- top: Real-time process monitoring.
- htop (if installed): An enhanced interactive process viewer.
3. Managing Processes
- kill PID: Sends SIGTERM to terminate process.
- kill -9 PID: Forcefully kills a process.
- pkill process_name: Kills processes by name.
- killall process_name: Kills all processes with the specified name.
Network Configuration and Troubleshooting
Effective network management is essential for server accessibility and security.
1. Viewing Network Interfaces
- ip addr show: Displays all network interfaces.
- ifconfig: Deprecated but still available on some systems for interface info.
- ip link show: Shows link layer details.
2. Managing Network Services
- systemctl restart network.service: Restarts network service.
- nmcli device status: Checks network device status (NetworkManager CLI).
3. Testing Network Connectivity
- ping hostname/IP: Tests connectivity.
- traceroute hostname/IP: Traces route to host.
- netstat -tulnp: Lists active network connections and listening ports.
- ss -tuln: Alternative to netstat for socket statistics.
4. Configuring Firewall
- firewall-cmd --state: Checks firewall status.
- firewall-cmd --list-all: Lists current firewall rules.
- firewall-cmd --add-port=80/tcp --permanent: Opens port 80 permanently.
- firewall-cmd --reload: Reloads firewall rules.
Disk and Storage Management Commands
Managing disk space and partitions is key for performance and data integrity.
1. Viewing Disk Usage
- df -h: Shows disk space usage in human-readable format.
- du -sh /path/to/directory: Displays size of a directory.
2. Managing Disks and Partitions
- lsblk: Lists block devices.
- fdisk -l: Lists partition tables.
- parted /dev/sdX: Used for partitioning disks.
- mkfs.ext4 /dev/sdX: Formats a partition with ext4 filesystem.
- mount /dev/sdX /mnt/mount_point: Mounts a filesystem.
- umount /mnt/mount_point: Unmounts a filesystem.
3. Logical Volume Management (LVM)
- lvcreate -L 10G -n volume_name volume_group: Creates a logical volume.
- lvextend -L +10G /dev/volume_group/volume_name: Extends a volume.
- lvremove /dev/volume_group/volume_name: Removes a volume.
- vgcreate and vgextend: Manage volume groups.
Security and User Management Commands
Security is a top priority, and CentOS provides robust tools for managing users, permissions, and security policies.
1. Managing User Accounts
- useradd username: Adds a new user.
- passwd username: Sets user password.
- usermod -aG group username: Adds user to a group.
- userdel username: Deletes a user.
2. Managing SSH Access
- systemctl restart sshd: Restarts SSH service.
- vi /etc/ssh/sshd_config: Edits SSH configuration.
- ssh user@hostname: Connects via SSH.
3. Setting Up Firewalls and SELinux
- getenforce: Checks SELinux status.
- setenforce 1: Sets SELinux to enforcing mode.
- semanage port -a -t http_port_t -p tcp 8080: Changes SELinux port context.
System Monitoring and Log Management
Monitoring your server's health and analyzing logs are vital for proactive maintenance.
1. Viewing System Logs
- journalctl: Displays system logs.
- tail -f /var/log/messages: Real-time log monitoring.
- less /var/log/secure: Security-related logs.
2. Monitoring System Resources
- free -h: Shows memory usage.
- vmstat: Reports system performance.
- iostat: Monitors CPU and I/O statistics.
3. Performance Testing Tools
- top / htop: Real-time process monitoring.
- nload: Network bandwidth usage.
- iftop: Displays network bandwidth per connection.
Backup and Restore Commands
Data integrity and disaster recovery depend on proper backup procedures.
1. Creating Archives
- tar -cvzf archive_name.tar.gz /path/to/directory: Creates a compressed archive.
- tar -xvzf archive_name.tar.gz: Extracts archive.
2. Copying Files and Directories
- rsync -avz /source /destination: Synchronizes files efficiently.
CentOS Commands Cheat Sheet: Your Comprehensive Guide to Navigating CentOS Linux
CentOS, a popular enterprise-class Linux distribution derived from sources freely provided by Red Hat, is widely used for servers, development environments, and enterprise applications. Mastering CentOS commands is essential for system administrators, developers, and anyone managing CentOS systems. Whether you're performing routine maintenance, troubleshooting, or deploying services, having a solid command-line knowledge base can significantly streamline your workflow. This article offers a detailed CentOS commands cheat sheet, providing a structured, easy-to-reference guide to the most common and powerful commands on CentOS systems.
Why Mastering CentOS Commands Matters
Working with CentOS primarily involves the command line, especially in server environments where graphical interfaces are often disabled for performance or security reasons. Commands allow you to install software, manage files, monitor system health, configure network settings, and troubleshoot issues efficiently. Having a comprehensive cheat sheet helps in quick referencing, reducing the time spent searching for syntax or options, and ensures you follow best practices.
Basic System Information Commands
- uname
Displays information about the Linux kernel.
- `uname -r` — Kernel version
- `uname -a` — All kernel information, including hostname and architecture
- hostname
Shows or sets the system hostname.
- `hostname` — Display current hostname
- `hostnamectl` — View and edit hostname and other system info
- uptime
Shows how long the system has been running, including load averages.
- `uptime` — System uptime and load averages
- arch
Displays the system architecture.
- `arch` or `uname -m`
User Management Commands
- whoami
Displays the current logged-in user.
- id
Shows user and group IDs.
- useradd / userdel / usermod
Manage user accounts.
- `useradd username` — Create new user
- `usermod -aG groupname username` — Add user to a group
- `userdel username` — Delete user
- passwd
Change user password.
- `passwd username` — Change password for a user
- groups
Lists groups the user belongs to.
File and Directory Commands
- ls
Lists directory contents.
- `ls -l` — Long listing format
- `ls -a` — Show hidden files
- `ls -lh` — Human-readable sizes
- cd
Change directory.
- `cd /path/to/directory`
- pwd
Print current working directory.
- cp / mv / rm
Copy, move, and delete files.
- `cp source destination`
- `mv source destination`
- `rm filename` — Remove file
- `rm -r directory` — Remove directory recursively
- mkdir / rmdir
Create or remove directories.
- `mkdir newdir`
- `rmdir directory`
- find
Search for files and directories.
- `find /path -name filename`
- `find /path -type f -name ".log"`
Package Management Commands (YUM/DNF)
CentOS traditionally uses YUM, but newer versions adopt DNF (Dandified YUM).
- YUM
- `yum check-update` — Check for available updates
- `yum update` — Update all packages
- `yum install package_name` — Install new package
- `yum remove package_name` — Remove package
- `yum list installed` — List installed packages
- `yum search keyword` — Search for packages
- DNF (CentOS 8 and later)
- `dnf check-update`
- `dnf update`
- `dnf install package_name`
- `dnf remove package_name`
- `dnf list installed`
- `dnf search keyword`
Service and Process Management
- systemctl
CentOS 7+ uses systemd for service management.
- `systemctl start service_name` — Start a service
- `systemctl stop service_name` — Stop a service
- `systemctl restart service_name` — Restart a service
- `systemctl enable service_name` — Enable service at boot
- `systemctl disable service_name` — Disable service at boot
- `systemctl status service_name` — Check service status
- `systemctl is-active service_name` — Check if service is active
- ps / top / htop
Monitor processes.
- `ps aux` — List all running processes
- `top` — Real-time process viewer
- `htop` — Interactive process viewer (may require installation)
- kill / killall / pkill
Terminate processes.
- `kill PID` — Kill process by ID
- `killall process_name` — Kill all processes with a given name
- `pkill process_name` — Send signals to processes by name
Disk and Filesystem Management
- df / du
Display disk space usage.
- `df -h` — Human-readable disk space usage
- `du -sh /path/to/directory` — Total size of directory
- mount / umount
Mount or unmount filesystems.
- `mount /dev/sdX /mnt/point`
- `umount /mnt/point`
- fdisk / parted
Partition disks.
- `fdisk /dev/sdX` — Manage disk partitions
- `parted /dev/sdX` — Advanced partitioning
- mkfs
Create filesystem.
- `mkfs.ext4 /dev/sdX1` — Format partition with ext4
Network Management Commands
- ip / ifconfig
Configure and display network interfaces.
- `ip addr` — Show IP addresses
- `ifconfig` — Deprecated, but still used in some systems
- ping
Test network connectivity.
- `ping google.com`
- netstat / ss
Display network connections.
- `netstat -tuln` — Show active listening ports
- `ss -tuln` — Modern alternative to netstat
- nmcli
Manage network connections via NetworkManager.
- `nmcli device status` — Show device status
- `nmcli connection show` — List network connections
- firewall-cmd
Manage firewalld (CentOS 7+).
- `firewall-cmd --state` — Check status
- `firewall-cmd --permanent --add-service=http` — Allow HTTP
- `firewall-cmd --reload` — Reload firewall
System Monitoring and Logs
- journalctl
Query systemd logs.
- `journalctl -xe` — Show recent logs with details
- `journalctl -u service_name` — Logs for specific service
- free
Show memory usage.
- `free -h` — Human-readable output
- uptime / load average
Monitor system load.
File Compression and Archiving
- tar
Create or extract archives.
- `tar -cvf archive.tar /path/to/files` — Create archive
- `tar -xvf archive.tar` — Extract archive
- `tar -czvf archive.tar.gz /path/to/files` — Create gzip compressed archive
- `tar -xzvf archive.tar.gz` — Extract gzip archive
- gzip / gunzip
Compress or decompress files.
- `gzip filename`
- `gunzip filename.gz`
- zip / unzip
Create or extract ZIP files.
- `zip archive.zip files`
- `unzip archive.zip`
User and Permission Management
- chmod
Change file permissions.
- `chmod 755 filename` — Set permissions
- chown
Change file ownership.
- `chown user:group filename`
- sudo
Execute commands as root.
- `sudo command`
Troubleshooting and Maintenance
- ping / traceroute
Diagnose network issues.
- nslookup / dig
Query DNS records.
- `nslookup domain.com`
- `dig domain.com`
- systemctl reboot / poweroff
Reboot or power off.
- `systemctl reboot`
- `systemctl poweroff`
Final Tips
- Always run commands with appropriate permissions, often requiring `sudo`.
- Regularly update your system to ensure security and stability.
- Use man pages (`man command`) for detailed command options.
- Backup configuration files before making significant changes.
Conclusion
Mastering CentOS commands is vital for efficient system administration, troubleshooting, and automation. This cheat sheet covers the core commands you need to manage files, users, services, networks, and more on your CentOS systems. Keep this guide handy as a reference, and continue exploring the rich set of tools available within CentOS to become a proficient Linux administrator.
Question Answer What is the command to check the version of CentOS installed on my system? You can check the CentOS version by running: cat /etc/centos-release or lsb_release -a. How do I list all the files and directories in the current directory? Use the command: ls. For detailed information, add options like ls -l or ls -la. What command is used to update all packages on CentOS? Run: sudo yum update to update all installed packages to their latest versions. How can I start, stop, or restart a service in CentOS? Use systemctl commands, e.g., sudo systemctl start service_name, stop, or restart service_name. Which command is used to check disk space usage? Use the command: df -h to display disk space usage in human-readable format. How do I view real-time system logs on CentOS? Use: journalctl -f to follow system logs in real-time. What command helps me monitor CPU and memory usage? Commands like top or htop (if installed) can be used to monitor CPU and memory usage interactively. How do I permanently add an environment variable in CentOS? Add the export statement to /etc/profile or ~/.bash_profile, e.g., export MY_VAR='value', and then source the file or log out and back in.
Related keywords: CentOS, Linux commands, command line, terminal commands, cheat sheet, system administration, shell commands, Linux tips, command shortcuts, CentOS tutorials