0% found this document useful (0 votes)
9 views15 pages

Linux Cheat Sheet

The document is a comprehensive Linux cheat sheet that covers essential commands across various categories including basic commands, file permissions, process management, user management, network commands, and more. It provides syntax and examples for each command, making it a useful reference for users working with Linux systems. The cheat sheet also includes sections on system monitoring, package management, and troubleshooting techniques.

Uploaded by

dimognetehem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Linux Cheat Sheet

The document is a comprehensive Linux cheat sheet that covers essential commands across various categories including basic commands, file permissions, process management, user management, network commands, and more. It provides syntax and examples for each command, making it a useful reference for users working with Linux systems. The cheat sheet also includes sections on system monitoring, package management, and troubleshooting techniques.

Uploaded by

dimognetehem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DevOps Shack

LINUX CHEAT SHEET

1. Basic Commands

● pwd – Print current working directory

● ls – List files and directories

○ ls -l – Long format listing


○ ls -a – Show hidden files
○ ls -lh – Human-readable sizes

● cd <directory> – Change directory


○ cd .. – Move up one directory
○ cd - – Go to previous directory
● mkdir <dir> – Create a directory

● rmdir <dir> – Remove an empty directory

● rm -r <dir> – Remove a directory and its contents

● touch <file> – Create an empty file

● cp <source> <destination> – Copy files/directories


○ cp -r <dir1> <dir2> – Copy a directory recursively
● mv <old> <new> – Move/rename files or directories

● rm <file> – Delete a file

● cat <file> – View file contents

● less <file> – View file with navigation


● head <file> – Show the first 10 lines of a file

● tail <file> – Show the last 10 lines of a file


○ tail -f <file> – Live update file output

2. File Permissions & Ownership

● ls -l – View file permissions

● chmod 755 <file> – Change file permissions


○ chmod u+x <file> – Grant execute permission to user
○ chmod g-w <file> – Remove write permission from group
○ chmod o+r <file> – Add read permission to others
● chown <user>:<group> <file> – Change ownership

● chown -R <user>:<group> <dir> – Change ownership recursively

3. File Searching

● find <dir> -name "<pattern>" – Find files by name

● find <dir> -type d -name "<pattern>" – Find directories by name

● find <dir> -type f -size +100M – Find files larger than 100MB

● locate <file> – Find file using index

● updatedb – Update the locate database

4. File Compression & Archiving

● tar -cvf archive.tar <files> – Create a tar archive


● tar -xvf archive.tar – Extract a tar archive

● tar -czvf archive.tar.gz <files> – Create a compressed archive

● tar -xzvf archive.tar.gz – Extract a compressed archive

● zip archive.zip <files> – Create a zip archive

● unzip archive.zip – Extract a zip archive

5. Process Management

● ps aux – List running processes

● top – Show real-time system processes

● htop – Interactive process viewer (requires installation)

● kill <PID> – Terminate a process

● kill -9 <PID> – Force kill a process

● pkill <name> – Kill process by name

● nohup <command> & – Run process in background

● jobs – List background processes

● fg %1 – Bring background job to foreground

● bg %1 – Resume a background job


6. Disk Usage

● df -h – Show disk usage in human-readable format

● du -sh <dir> – Show directory size

● du -h --max-depth=1 <dir> – Show size of each subdirectory

● lsblk – Show block devices

● mount /dev/sdX /mnt – Mount a partition

● umount /dev/sdX – Unmount a partition

7. User Management

● whoami – Show current user

● who – Show logged-in users

● id – Show user ID (UID) and group ID (GID)

● adduser <username> – Add a user

● passwd <username> – Change password

● usermod -aG <group> <user> – Add user to group

● deluser <username> – Delete a user

● groups <user> – Show user groups


8. Network Commands

● ifconfig – Show network interfaces (deprecated)

● ip a – Show IP addresses

● ip r – Show routing table

● ping <host> – Check connectivity

● traceroute <host> – Trace route to a host

● netstat -tulnp – Show listening ports

● ss -tulnp – Alternative to netstat

● curl -I <url> – Fetch HTTP headers

● wget <url> – Download a file

9. Package Management

Debian/Ubuntu:

● apt update – Update package lists

● apt upgrade – Upgrade installed packages

● apt install <package> – Install a package

● apt remove <package> – Remove a package

● dpkg -i <package.deb> – Install a .deb package


RHEL/CentOS:

● yum update – Update packages

● yum install <package> – Install a package

● yum remove <package> – Remove a package

● rpm -i <package.rpm> – Install an .rpm package

Arch Linux:

● pacman -Syu – Update system

● pacman -S <package> – Install package

● pacman -R <package> – Remove package

10. Log Management

● cat /var/log/syslog – View system logs

● cat /var/log/auth.log – View authentication logs

● dmesg – Show boot messages

● journalctl -xe – View systemd logs

● tail -f /var/log/syslog – Monitor logs live


11. SSH & Remote Access

● ssh user@host – Connect to a remote server

● ssh -i <key> user@host – Connect using an SSH key

● scp <file> user@host:/path – Copy file to a remote server

● scp user@host:/file . – Copy file from remote server

● rsync -av <src> <dest> – Sync files locally or remotely

12. Text Processing

● grep "pattern" <file> – Search for a pattern in a file

● grep -r "pattern" <dir> – Search recursively in a directory

● awk '{print $1}' <file> – Print the first column

● sed 's/old/new/g' <file> – Replace text in a file

● sort <file> – Sort lines in a file


uniq <file> – Remove duplicate lines

● cut -d':' -f1 /etc/passwd – Extract first field from file

13. Crontab (Task Scheduling)

● crontab -e – Edit crontab

● crontab -l – List crontab jobs


● Example cron job:
○ 0 * * * * /path/to/script.sh (Runs every hour)
○ @reboot /path/to/script.sh (Runs on startup)


14. System Monitoring

● uptime – Show system uptime

● free -h – Show memory usage

● vmstat – Show system performance

● iostat – Show CPU and disk I/O stats

● sar -u 5 10 – CPU usage every 5 sec for 10 times

15. Miscellaneous
alias ll='ls -lah' – Create a command alias

● unalias ll – Remove alias

● history – Show command history

● !! – Repeat last command

● !n – Run command from history (e.g., !100)

● echo $PATH – Show system PATH

● which <command> – Find command location

● man <command> – Show command manual

16. System Performance & Optimization

● top – Monitor system performance interactively

● htop – Advanced interactive process viewer (needs installation)

● vmstat 1 – Show system performance every second


● iostat -x 1 – Show disk I/O statistics
● sar -r 5 10 – Monitor memory usage every 5 seconds for 10 times

● free -m – Show memory usage in MB

● uptime – Show system uptime and load average

● watch -n 1 <command> – Run a command every second

17. Firewall & Security

● ufw status – Check firewall status (Ubuntu)

● ufw enable – Enable UFW firewall

● ufw allow <port> – Allow incoming traffic on a port

● ufw deny <port> – Deny traffic on a port

● iptables -L -v – List firewall rules (RHEL/CentOS)

● iptables -A INPUT -p tcp --dport 22 -j ACCEPT – Allow SSH

● iptables -A INPUT -p tcp --dport 80 -j ACCEPT – Allow HTTP

● iptables -D INPUT -p tcp --dport 80 -j ACCEPT – Remove rule

● fail2ban-client status – Check Fail2Ban status (if installed)


● passwd -l <user> – Lock a user account

● passwd -u <user> – Unlock a user account

18. Kernel & System Information

● uname -r – Show kernel version

● uname -a – Show full kernel details


● cat /etc/os-release – Show OS details

● hostnamectl – Show system hostname and details

● lsmod – List loaded kernel modules

● modinfo <module> – Show details of a kernel module

● dmesg | tail – View recent kernel messages

● sysctl -a – Show all system configurations

19. Mounting & Unmounting File Systems

● mount – Show mounted file systems

● mount /dev/sdX /mnt – Mount a device


● umount /mnt – Unmount a device

● df -h – Show available disk space

● du -sh /directory – Show directory size

● fsck /dev/sdX – Check and repair a filesystem

● mkfs.ext4 /dev/sdX – Format a partition as ext4

20. System Shutdown & Restart

● shutdown -h now – Shutdown immediately

● shutdown -h +10 – Shutdown in 10 minutes

● shutdown -r now – Restart system immediately

● reboot – Reboot the system


● poweroff – Power off the system

● halt – Halt the system

21. Disk Partitioning

● fdisk -l – List all partitions

● fdisk /dev/sdX – Manage partitions on a disk

● parted /dev/sdX – Create and resize partitions

● mkfs.ext4 /dev/sdX1 – Format partition as ext4

● mount /dev/sdX1 /mnt – Mount a partition

22. Environment Variables

● echo $PATH – Show system PATH

● export VAR=value – Set an environment variable

● printenv VAR – Show variable value

● unset VAR – Remove a variable

● env – List all environment variables

23. Disk & Storage Management

● lsblk – List storage devices

● blkid – Show UUIDs of partitions


● mount -o loop file.iso /mnt – Mount an ISO file

● lsof /path/to/file – Show processes using a file

● tune2fs -m 5 /dev/sdX1 – Set reserved space on ext4

24. Service Management (Systemd & SysVinit)

Systemd (Ubuntu, CentOS 7+, RHEL 7+)

● systemctl start <service> – Start a service

● systemctl stop <service> – Stop a service

● systemctl restart <service> – Restart a service

● systemctl status <service> – Show service status

● systemctl enable <service> – Enable service at boot

● systemctl disable <service> – Disable service at boot

SysVinit (Older Systems)

● service <service> start – Start a service

● service <service> stop – Stop a service

● chkconfig <service> on – Enable service at boot

● chkconfig <service> off – Disable service at boot

25. File & Directory Permissions (Advanced)

● chmod 777 <file> – Full permissions (rwxrwxrwx)


● chmod 644 <file> – Read/write for owner, read-only for others

● chmod 700 <dir> – Full access for owner, no access for others

● chmod +x <file> – Add execute permission

● chmod -w <file> – Remove write permission

● chown user:group <file> – Change ownership

● chown -R user:group <dir> – Change ownership recursively

26. Advanced File Operations

● ls -lt – List files by modification time

● ls -S – List files by size


● find / -type f -name "file.txt" – Search for a file

● find . -type d -empty – Find empty directories

● find . -mtime -1 – Find files modified in the last 24 hours

● ln -s /path/to/original /path/to/link – Create a symbolic link

27. Backup & Restore

● tar -cvf backup.tar /path/to/dir – Create a tar backup

● tar -xvf backup.tar – Extract a tar archive

● rsync -av /source/ /destination/ – Sync files

● dd if=/dev/sdX of=backup.img – Create a disk image


28. Performance Tuning

● nice -n 10 <command> – Run a command with lower priority

● renice -n 10 -p <PID> – Change priority of a


running process

● ulimit -n 65535 – Increase file descriptor limit

● sysctl vm.swappiness=10 – Reduce swap usage

29. Debugging & Troubleshooting

● dmesg | tail -20 – Show recent system messages

● journalctl -xe – View recent logs

● strace -p <PID> – Trace system calls of a process

● lsof -i :80 – Show processes using port 80

● tcpdump -i eth0 – Capture network packets

30. Kernel Module Management

● lsmod – List loaded modules

● modprobe <module> – Load a module

● rmmod <module> – Remove a module

● modinfo <module> – Show module details

You might also like