If you work with Linux, the terminal is either your best friend or your worst enemy—sometimes both on the same day. If you're just starting out or managing servers, a solid Linux commands cheat sheet can save you time and frustration.
Essential Linux Directory Navigation Commands
1. Navigating and Managing Directories Effectively
pwd– Prints the absolute path of the current working directory, so you always know where you are.ls– Lists files and directories in the current directory. Usels -lfor detailed info andls -ato see hidden files.cd <directory>– Changes the current directory to the specified directory. Example:cd /home/user/docs.cd ..– Moves up one level in the directory tree.cd -– Switches back to the previous directory you were in.mkdir <dir>– Creates a new directory. Example:mkdir new_folder.rmdir <dir>– Removes an empty directory. For non-empty directories, userm -r <dir>.rm -r <dir>– Deletes a directory and all its contents. Use with caution!
2. File Creation, Management, and Manipulation
touch <file>– Creates an empty file or updates the timestamp if the file exists.cp <source> <destination>– Copies files or directories. Example:cp file1.txt /backup/.mv <source> <destination>– Moves or renames files. Example:mv oldname.txt newname.txt.rm <file>– Deletes a file permanently.cat <file>– Displays the content of a file. Combine with| moreto paginate.less <file>– Opens a file for reading, allowing scrolling up and down.head <file>– Shows the first 10 lines of a file. Example:head -20 file.txtfor 20 lines.tail <file>– Shows the last 10 lines of a file. Usetail -f file.logto monitor a log file in real time.
Managing Users and Permissions in Linux
3. User and Group Management
whoami– Displays the currently logged-in user.who– Lists all users currently logged in.id– Shows the user ID (UID) and group ID (GID).adduser <username>– Creates a new user and sets up a home directory.passwd <username>– Changes the password for a user.usermod -aG <group> <username>– Adds a user to an existing group.deluser <username>– Deletes a user account.
4. Managing File Permissions and Ownership
ls -l– Lists files with detailed permission settings.chmod <permissions> <file>– Changes file permissions. Example:chmod 755 script.sh(rwx for owner, rx for others).chown <user>:<group> <file>– Changes file owner and group.chgrp <group> <file>– Changes the group ownership of a file.umask <value>– Sets default file permissions for newly created files.
Monitoring and Managing System Processes
5. Process Management and Monitoring Tools
ps aux– Lists all running processes with details.top– Shows real-time system processes and resource usage.htop– Interactive version oftop, requires installation.kill <PID>– Terminates a process by ID. Example:kill 1234.pkill <name>– Kills a process by its name. Example:pkill firefox.jobs– Lists background processes.fg %<job_id>– Brings a background job to the foreground.nohup <command> &– Runs a command immune to logout.
Networking and Connectivity in Linux
6. Essential Networking Commands for Troubleshooting
ip a– Displays IP addresses of network interfaces.ping <host>– Tests connectivity to a host.curl <URL>– Fetches a webpage or API response.wget <URL>– Downloads a file from the internet.netstat -tulnp– Shows active network connections.ss -tulnp– Modern alternative to netstat.nslookup <domain>– Queries DNS records.dig <domain>– Retrieves detailed DNS info.traceroute <host>– Shows the route packets take.
Disk Usage, Storage, and File System Management
7. Analyzing and Managing Disk Usage
df -h– Displays available disk space.du -sh <directory>– Shows the size of a directory.mount– Lists mounted filesystems.umount <device>– Unmounts a filesystem.fsck <device>– Checks and repairs filesystems.
Managing Software and Packages
8. Handling Software Packages Efficiently
Debian-based (Ubuntu, Debian)
apt update– Refreshes package lists.apt upgrade– Upgrades installed packages.apt install <package>– Installs a package.apt remove <package>– Uninstalls a package.dpkg -i <package.deb>– Installs a.debpackage.
RHEL-based (CentOS, Fedora)
yum update– Updates all packages (CentOS 7).dnf update– Updates all packages (Fedora, CentOS 8+).yum install <package>– Installs a package.dnf install <package>– Installs a package (Fedora, CentOS 8+).rpm -i <package.rpm>– Installs an.rpmpackage.
System Performance and Monitoring
9. Checking System Health and Performance
uptime– Shows system uptime.free -h– Displays memory usage.vmstat– Reports system performance.iostat– Displays CPU and disk usage stats.sar– Collects system activity data.mpstat– Displays per-core CPU usage.iotop– Monitors disk I/O in real-time.
Handling Logs and Troubleshooting
10. Managing and Analyzing Logs
dmesg– Views system boot logs.journalctl– Views system logs.tail -f /var/log/syslog– Monitors system logs live.tail -f /var/log/auth.log– Monitors authentication logs.
Automating Tasks and Scheduling
11. Setting Up Cron Jobs and Scheduled Tasks
crontab -l– Lists scheduled cron jobs.crontab -e– Edits cron jobs.crontab -r– Removes all cron jobs.systemctl list-timers– Displays systemd timers.at <time>– Schedules a one-time job.
Security and Firewall Management
12. Strengthening Linux Security with Firewalls
ufw status– Checks firewall status.ufw allow <port>– Opens a port.ufw deny <port>– Blocks a port.iptables -L– Lists firewall rules.fail2ban-client status– Checks Fail2Ban status.