0% found this document useful (0 votes)
8 views

Linux Cheat Sheet

Uploaded by

sundharesan.p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Linux Cheat Sheet

Uploaded by

sundharesan.p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

DevOps Shack

Linux Complete Detailed Cheat Sheet


1. File and Directory Management
1.1 ls - List directory contents

ls -l /home

• Description: Lists all files and directories in /home with detailed information.

• Options:

o -l: Long format (permissions, owner, size, etc.)

o -a: Show hidden files.

o -h: Human-readable file sizes.

1.2 cd - Change directory

cd /var/log

• Description: Moves to the /var/log directory.

• Tips:

o cd ..: Go to the parent directory.

o cd ~: Go to the home directory.

1.3 pwd - Print working directory

pwd

• Description: Outputs the full path of the current working directory.

1.4 mkdir - Create directories

mkdir -p /tmp/test_dir/sub_dir

• Description: Creates /tmp/test_dir and sub_dir if they don’t already exist.

o -p: Create parent directories as needed.


1.5 rmdir - Remove empty directories

rmdir /tmp/test_dir/sub_dir

• Description: Removes the sub_dir if it's empty.

1.6 rm - Remove files or directories

rm -rf /tmp/test_dir

• Description: Deletes the test_dir directory and its contents recursively.

• Options:

o -r: Recursive delete.

o -f: Force delete without confirmation.

1.7 cp - Copy files or directories

cp -r /etc/nginx /tmp/

• Description: Copies the /etc/nginx directory to /tmp/.

• Options:

o -r: Copy directories recursively.

1.8 mv - Move or rename files

mv /tmp/nginx /tmp/nginx_backup

• Description: Renames or moves /tmp/nginx to /tmp/nginx_backup.

1.9 touch - Create empty files

touch newfile.txt

• Description: Creates an empty file named newfile.txt.

1.10 stat - Display file status

stat newfile.txt

• Description: Provides detailed information about the file, such as size, permissions, and
timestamps.

2. File Viewing and Editing

2.1 cat - View file contents

cat /etc/hosts

• Description: Displays the contents of /etc/hosts.

• Tips:

o Use cat file1 file2 > combined_file to concatenate files.


2.2 less - View file contents page by page

less /var/log/syslog

• Description: Opens the /var/log/syslog file in a scrollable view.

2.3 head - Display the first lines of a file

head -n 5 /etc/passwd

• Description: Shows the first 5 lines of the /etc/passwd file.

2.4 tail - Display the last lines of a file

tail -n 10 /var/log/syslog

• Description: Shows the last 10 lines of the /var/log/syslog file.

• Tips:

o Use tail -f to monitor a file in real time.

2.5 nano - Edit files

nano /tmp/test.txt

• Description: Opens test.txt in the Nano text editor.

• Tips:

o Ctrl + O: Save.

o Ctrl + X: Exit.

3. File Permissions and Ownership

3.1 chmod - Change file permissions

chmod 644 /tmp/test.txt

• Description: Sets the file permissions to rw-r--r-- for test.txt.

• Options:

o 777: Full permissions for all.

o 644: Owner can read/write, others can only read.

3.2 chown - Change file ownership

chown user:user /tmp/test.txt

• Description: Changes the owner and group of test.txt to user.

3.3 umask - Set default file permissions

umask 022

• Description: Sets the default permission for new files to 755.


4. Searching and Finding Files

4.1 find - Search for files

find / -name "test.txt"

• Description: Searches for files named test.txt in the root directory.

• Options:

o -type f: Search for files.

o -type d: Search for directories.

4.2 grep - Search inside files

grep "error" /var/log/syslog

• Description: Searches for the term error in /var/log/syslog.

5. Networking

5.1 ping - Test network connectivity

ping -c 4 google.com

• Description: Sends 4 ICMP packets to google.com to check connectivity.

5.2 curl - Send HTTP requests

curl https://example.com

• Description: Fetches the contents of https://example.com.

6. Disk and Filesystem Management

6.1 df - Display disk space usage

df -h

• Description: Shows disk space usage in a human-readable format.

• Options:

o -h: Human-readable sizes.

o -T: Displays filesystem type.

6.2 du - Display directory size

du -sh /var/log

• Description: Shows the size of the /var/log directory.

• Options:
o -s: Summary for the directory.

o -h: Human-readable format.

6.3 mount - Mount a filesystem

mount /dev/sdb1 /mnt

• Description: Mounts the partition /dev/sdb1 to /mnt.

• Options:

o -t ext4: Specify the filesystem type.

6.4 umount - Unmount a filesystem

umount /mnt

• Description: Unmounts the /mnt directory.

6.5 lsblk - List information about block devices

lsblk

• Description: Displays information about block devices, such as partitions.

6.6 blkid - Display block device UUIDs

blkid

• Description: Lists the UUIDs and filesystem types of block devices.

6.7 fsck - Filesystem check and repair

fsck /dev/sdb1

• Description: Checks and repairs filesystem errors on /dev/sdb1.

6.8 mkfs - Create a filesystem

mkfs.ext4 /dev/sdb1

• Description: Formats /dev/sdb1 with the ext4 filesystem.

7. Process Management

7.1 ps - Display process information

ps aux | grep apache

• Description: Lists all processes with details and filters for apache.

7.2 top - Monitor system processes

top

• Description: Displays a real-time view of system processes and resource usage.

7.3 htop - Enhanced process viewer


htop

• Description: Provides an interactive process viewer (requires installation).

7.4 kill - Terminate a process

kill -9 1234

• Description: Sends the SIGKILL signal to terminate process 1234.

7.5 pkill - Kill processes by name

pkill -f apache

• Description: Terminates all processes with apache in their name.

7.6 jobs - List background jobs

jobs

• Description: Displays jobs running in the background.

7.7 bg - Resume a job in the background

bg %1

• Description: Resumes job 1 in the background.

7.8 fg - Bring a job to the foreground

fg %1

• Description: Brings job 1 to the foreground.

8. User Management

8.1 who - Show who is logged in

who

• Description: Lists users currently logged into the system.

8.2 whoami - Show the current user

whoami

• Description: Displays the username of the current user.

8.3 adduser - Add a new user

sudo adduser john

• Description: Creates a new user john.

8.4 usermod - Modify user details

sudo usermod -aG sudo john

• Description: Adds john to the sudo group.


8.5 passwd - Change user password

passwd john

• Description: Changes the password for user john.

8.6 deluser - Remove a user

sudo deluser john

• Description: Deletes user john.

9. Networking

9.1 ifconfig - Display or configure network interfaces

ifconfig

• Description: Shows network interface details.

• Note: Use ip a for modern systems.

9.2 ip - Display or configure IP addresses

ip a

• Description: Lists all network interfaces and their IP addresses.

9.3 netstat - Network statistics

netstat -tuln

• Description: Lists open ports and active connections.

• Options:

o -t: TCP connections.

o -u: UDP connections.

9.4 traceroute - Trace the route to a host

traceroute google.com

• Description: Displays the path packets take to reach google.com.

9.5 wget - Download files from the web

wget https://example.com/file.zip

• Description: Downloads file.zip from example.com.

9.6 scp - Securely copy files between systems

scp file.txt [email protected]:/tmp/

• Description: Copies file.txt to /tmp/ on a remote host.


10. System Monitoring

10.1 uptime - System uptime and load

uptime

• Description: Shows how long the system has been running.

10.2 free - Display memory usage

free -h

• Description: Displays memory usage in a human-readable format.

10.3 vmstat - System performance metrics

vmstat 1 5

• Description: Provides CPU, memory, and I/O stats every second for 5 seconds.

10.4 iostat - CPU and I/O statistics

iostat

• Description: Shows CPU and disk I/O statistics.

10.5 sar - System activity report

sar -u 1 3

• Description: Displays CPU usage over 3 intervals.

11. Package Management

11.1 apt - Manage packages on Debian-based systems

sudo apt update && sudo apt upgrade -y

• Description: Updates the package list and upgrades all packages to the latest version.

• Options:

o install: Install a package (sudo apt install nginx).

o remove: Uninstall a package (sudo apt remove nginx).

o search: Search for a package (apt search apache).

11.2 yum - Manage packages on RHEL-based systems

sudo yum install httpd

• Description: Installs the Apache web server on Red Hat-based distributions.

• Options:

o update: Update all packages (sudo yum update).

o remove: Remove a package (sudo yum remove httpd).


11.3 dnf - Next-gen package manager for RHEL systems

sudo dnf install vim

• Description: Installs the Vim editor using dnf.

11.4 rpm - Manage RPM packages

rpm -ivh package.rpm

• Description: Installs an RPM package.

• Options:

o -e: Remove a package (rpm -e package_name).

o -q: Query installed packages (rpm -q package_name).

11.5 snap - Manage Snap packages

sudo snap install vlc

• Description: Installs the VLC media player as a Snap package.

12. Archiving and Compression

12.1 tar - Archive files

tar -cvf archive.tar /path/to/files

• Description: Archives files into archive.tar.

• Options:

o -c: Create an archive.

o -x: Extract an archive.

o -z: Compress with gzip (tar -czvf archive.tar.gz /path/to/files).

12.2 gzip - Compress files

gzip file.txt

• Description: Compresses file.txt into file.txt.gz.

12.3 gunzip - Decompress files

gunzip file.txt.gz

• Description: Decompresses file.txt.gz back to file.txt.

12.4 zip - Compress files into a zip archive

zip archive.zip file1.txt file2.txt

• Description: Compresses file1.txt and file2.txt into archive.zip.

12.5 unzip - Extract a zip archive


unzip archive.zip

• Description: Extracts all files from archive.zip.

13. Security and Permissions

13.1 sudo - Execute a command as superuser

sudo apt install nginx

• Description: Runs the apt install nginx command as a superuser.

13.2 passwd - Change user password

sudo passwd username

• Description: Sets or changes the password for the username.

13.3 chmod - Modify file permissions

chmod 755 script.sh

• Description: Sets permissions to rwxr-xr-x for script.sh.

13.4 chown - Change file ownership

sudo chown user:group file.txt

• Description: Changes the owner and group of file.txt to user and group.

13.5 iptables - Manage firewall rules

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

• Description: Allows incoming SSH traffic on port 22.

13.6 ufw - Uncomplicated Firewall

sudo ufw allow 80

• Description: Allows incoming HTTP traffic on port 80.

13.7 ssh - Connect to a remote server

ssh [email protected]

• Description: Connects to the remote server with IP 192.168.1.10 as user.

13.8 scp - Securely copy files

scp file.txt [email protected]:/tmp/

• Description: Copies file.txt to the /tmp/ directory on a remote host.

14. Text Processing

14.1 cat - Concatenate and display file contents


cat file.txt

• Description: Displays the contents of file.txt.

14.2 grep - Search text patterns in files

grep "error" /var/log/syslog

• Description: Searches for the word "error" in /var/log/syslog.

14.3 awk - Pattern scanning and processing

awk '{print $1}' file.txt

• Description: Prints the first column of file.txt.

14.4 sed - Stream editor for transforming text

sed 's/old/new/g' file.txt

• Description: Replaces "old" with "new" in file.txt.

14.5 sort - Sort lines in a file

sort file.txt

• Description: Sorts the lines in file.txt alphabetically.

14.6 uniq - Remove duplicate lines

uniq file.txt

• Description: Removes consecutive duplicate lines in file.txt.

14.7 wc - Word, line, and character count

wc -l file.txt

• Description: Counts the number of lines in file.txt.

14.8 cut - Extract sections of text

cut -d':' -f1 /etc/passwd

• Description: Extracts the first field (username) from /etc/passwd.

15. Shell Scripting

15.1 Basic Shell Script

#!/bin/bash

echo "Hello, World!"

• Description: A simple shell script that prints "Hello, World!".

• Usage:

chmod +x script.sh
./script.sh

15.2 Variables in Scripts

#!/bin/bash

name="Linux"

echo "Welcome to $name scripting!"

• Description: Demonstrates how to use variables in shell scripts.

15.3 Loops in Scripts

#!/bin/bash

for i in 1 2 3 4 5; do

echo "Number: $i"

done

• Description: A for loop that prints numbers from 1 to 5.

15.4 Conditional Statements

#!/bin/bash
if [ $1 -gt 10 ]; then
echo "Greater than 10"
else
echo "Less than or equal to 10"
fi
• Description: A script that checks if a number is greater than 10.

16. System Information

16.1 uname - System information

uname -a

• Description: Displays system kernel and architecture details.

16.2 hostname - Display hostname

hostname

• Description: Outputs the hostname of the machine.

16.3 uptime - System uptime

uptime

• Description: Shows how long the system has been running.


17. System Performance Monitoring

17.1 vmstat - System performance statistics

vmstat 1 5

• Description: Displays system performance metrics (CPU, memory, I/O) every 1 second for 5
iterations.

17.2 iostat - CPU and I/O usage

iostat -x

• Description: Shows detailed CPU and disk I/O statistics.

17.3 sar - Historical performance data

sar -u 1 5

• Description: Displays CPU usage over 5 intervals, collected every 1 second.

17.4 top - Real-time process monitoring

top

• Description: Shows a dynamic real-time view of processes and system resource usage.

17.5 htop - Interactive process monitoring

htop

• Description: Interactive and visually enriched version of top (requires installation).

17.6 free - Memory usage

free -m

• Description: Displays memory usage in megabytes.

• Options:

o -g: Display in gigabytes.

o -h: Human-readable format.

17.7 dstat - Comprehensive system monitoring

dstat

• Description: Displays real-time CPU, disk, and network usage.

18. Troubleshooting and Logs

18.1 dmesg - Kernel ring buffer

dmesg | grep error

• Description: Filters kernel logs for "error" messages.


18.2 journalctl - Systemd logs

journalctl -u nginx.service

• Description: Shows logs related to the nginx service.

18.3 lsof - List open files

lsof -i :80

• Description: Lists processes using port 80.

18.4 strace - Trace system calls

strace -c ls

• Description: Tracks and summarizes system calls made by the ls command.

18.5 tcpdump - Network packet analyzer

sudo tcpdump -i eth0 port 80

• Description: Captures network traffic on port 80 for the eth0 interface.

18.6 netstat - Network connections

netstat -tuln

• Description: Lists all listening ports and established connections.

18.7 ss - View network sockets

ss -tuln

• Description: Displays listening TCP and UDP ports.

19. Advanced Scripting

19.1 Functions in Shell Scripts

#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "World"
• Description: Demonstrates a function that greets the given input.

19.2 Array Handling

#!/bin/bash
arr=("apple" "banana" "cherry")
for fruit in "${arr[@]}"; do
echo "Fruit: $fruit"
done
• Description: Iterates through an array and prints each element.
19.3 Reading User Input

#!/bin/bash
read -p "Enter your name: " name
echo "Welcome, $name!"
• Description: Prompts the user for input and prints a welcome message.

20. Development Tools

20.1 gcc - Compile C programs

gcc -o hello hello.c

• Description: Compiles hello.c into an executable named hello.

20.2 make - Build automation

make

• Description: Executes the build process defined in a Makefile.

20.3 git - Version control

git clone https://github.com/example/repo.git

• Description: Clones a Git repository from the specified URL.

• Other Commands:

o git add file: Stage changes.

o git commit -m "message": Commit staged changes.

21. Scheduling and Automation

21.1 cron - Schedule recurring tasks

• Setup a Cron Job:

crontab -e

• Example Cron Entry:

javascript
Copy code
0 2 * * * /path/to/script.sh
o Description: Runs script.sh every day at 2:00 AM.

21.2 at - Schedule a one-time task

echo "shutdown -h now" | at 10:00 PM

• Description: Schedules a system shutdown at 10:00 PM.


22. File Synchronization

22.1 rsync - Synchronize files/directories

rsync -av /source/ /destination/

• Description: Synchronizes /source/ with /destination/.

• Options:

o -a: Archive mode.

o -v: Verbose output.

23. Containers and Virtualization

23.1 docker - Manage Docker containers

docker run -d -p 8080:80 nginx

• Description: Runs an NGINX container and maps port 8080 to the container’s port 80.

23.2 kubectl - Manage Kubernetes clusters

kubectl get pods

• Description: Lists all running pods in the Kubernetes cluster.

24. Miscellaneous

24.1 alias - Create command shortcuts

alias ll='ls -la'

• Description: Defines ll as an alias for ls -la.

24.2 time - Measure command execution time

time ls

• Description: Measures how long the ls command takes to execute.

24.3 uptime - System uptime

uptime

• Description: Displays how long the system has been running.

5. Cloud and DevOps Tools

25.1 AWS CLI

aws s3 ls s3://my-bucket

• Description: Lists all objects in the my-bucket S3 bucket.


• Other Examples:

o Upload file: aws s3 cp file.txt s3://my-bucket/

o Download file: aws s3 cp s3://my-bucket/file.txt ./

25.2 Terraform

terraform init

• Description: Initializes a Terraform working directory.

• Other Commands:

o terraform plan: Previews infrastructure changes.

o terraform apply: Applies infrastructure changes.

25.3 Kubernetes (kubectl)

kubectl describe pod my-pod

• Description: Shows detailed information about my-pod.

25.4 Docker

docker build -t my-app .

• Description: Builds a Docker image named my-app from the current directory.

26. Advanced File Handling

26.1 xargs - Execute commands on input

cat files.txt | xargs rm

• Description: Reads file names from files.txt and deletes them.

26.2 tee - Redirect and output simultaneously

echo "Logging this message" | tee log.txt

• Description: Writes "Logging this message" to the terminal and log.txt.

26.3 split - Split large files

split -b 10M largefile part_

• Description: Splits largefile into 10MB chunks named part_aa, part_ab, etc.

27. Compression and Archiving

27.1 xz - Compress files with high compression

xz file.txt

• Description: Compresses file.txt into file.txt.xz.


27.2 7z - Compress files using 7-zip

7z a archive.7z file.txt

• Description: Compresses file.txt into a .7z archive.

28. Network Monitoring

28.1 nmap - Network scanner

nmap -sV 192.168.1.1

• Description: Scans the host at 192.168.1.1 for open ports and services.

28.2 dig - Query DNS records

dig example.com

• Description: Fetches DNS information for example.com.

29. User Management

29.1 id - Show user ID and group ID

id

• Description: Displays the current user's UID and GID.

29.2 groupadd - Add a new group

sudo groupadd developers

• Description: Creates a new group named developers.

29.3 groups - Show groups for a user

groups username

• Description: Lists all groups the username belongs to.

30. Security

30.1 gpg - Encrypt and decrypt files

• Encrypt a file:

gpg -c file.txt

• Decrypt a file:

gpg file.txt.gpg
30.2 fail2ban - Prevent brute-force attacks

sudo fail2ban-client status

• Description: Shows the status of the fail2ban service.

31. Process Management (Advanced)

31.1 nice - Set process priority

nice -n 10 ./script.sh

• Description: Runs script.sh with a lower priority (nice value of 10).

31.2 renice - Adjust priority of a running process

renice -n -5 -p 1234

• Description: Sets process 1234 to a higher priority (-5).

32. System Troubleshooting

32.1 uptime

uptime -p

• Description: Shows the system uptime in a human-readable format.

32.2 iotop - Monitor disk I/O usage

sudo iotop

• Description: Displays real-time disk I/O by processes.

33. Time and Date

33.1 date - Display or set the date

date "+%Y-%m-%d %H:%M:%S"

• Description: Displays the current date and time in the specified format.

33.2 timedatectl - Manage system time

timedatectl set-time "2024-01-01 10:00:00"

• Description: Sets the system date and time.

34. System Backup

34.1 rsync - Sync files and directories

rsync -avz /source /backup/


• Description: Backs up /source to /backup with compression (-z).

35. Miscellaneous

35.1 uptime - System uptime

uptime

• Description: Displays the time since the system last booted.

35.2 bc - Command-line calculator

echo "10+5" | bc

• Description: Performs basic arithmetic.

35.3 yes - Auto-answer prompts

yes | sudo apt upgrade

• Description: Automatically confirms all prompts with "yes."

36. Advanced Utilities

36.1 watch - Run a command periodically

watch -n 2 ls -lh

• Description: Runs ls -lh every 2 seconds and displays the output.

36.2 alias - Create command shortcuts

alias ll='ls -la'

• Description: Defines ll as a shortcut for ls -la.

36.3 unalias - Remove an alias

unalias ll

• Description: Removes the ll alias.

36.4 locate - Quickly find files

locate myfile.txt

• Description: Searches for myfile.txt using a pre-built file database.

36.5 updatedb - Update the locate database

sudo updatedb

• Description: Refreshes the database used by locate.


37. Disk Management

37.1 fdisk - Partition a disk

sudo fdisk /dev/sda

• Description: Opens a menu to create, modify, or delete partitions on /dev/sda.

37.2 parted - Advanced partitioning tool

sudo parted /dev/sdb mklabel gpt

• Description: Creates a new GPT partition table on /dev/sdb.

37.3 lsblk - List information about block devices

lsblk -f

• Description: Displays a tree view of block devices and their filesystems.

37.4 mkfs - Create a filesystem

sudo mkfs.ext4 /dev/sdb1

• Description: Formats /dev/sdb1 with the ext4 filesystem.

37.5 mount - Mount a filesystem

sudo mount /dev/sdb1 /mnt/data

• Description: Mounts /dev/sdb1 to /mnt/data.

37.6 umount - Unmount a filesystem

sudo umount /mnt/data

• Description: Unmounts /mnt/data.

38. System Diagnostics

38.1 uptime - System load and uptime

uptime

• Description: Displays system uptime and load averages.

38.2 dmesg - Kernel log messages

dmesg | tail -20

• Description: Shows the last 20 kernel messages.

38.3 sar - Performance statistics

sar -r 1 5

• Description: Displays memory usage over 5 intervals.


38.4 lsof - List open files

sudo lsof /path/to/file

• Description: Shows processes that have the specified file open.

38.5 iotop - Monitor disk I/O usage

sudo iotop

• Description: Displays real-time disk I/O by processes.

39. Performance Optimization

39.1 sysctl - Modify kernel parameters

sudo sysctl -w net.ipv4.ip_forward=1

• Description: Enables IP forwarding.

39.2 nice - Set priority for a command

nice -n 10 ./script.sh

• Description: Runs script.sh with a lower CPU priority.

39.3 renice - Change priority of a running process

renice -n 5 -p 1234

• Description: Adjusts the priority of process ID 1234 to 5.

40. Backup and Restore

40.1 tar - Archive files

tar -czvf backup.tar.gz /path/to/directory

• Description: Archives and compresses a directory into backup.tar.gz.

40.2 rsync - Sync files

rsync -avz /source/ /destination/

• Description: Synchronizes files between /source and /destination.

40.3 scp - Securely copy files

scp file.txt user@remote:/path/

• Description: Copies file.txt to a remote server.

40.4 dd - Create a disk image

dd if=/dev/sda of=/backup/disk.img bs=4M

• Description: Backs up the /dev/sda disk to an image file.


41. Advanced Networking

41.1 ip - Manage network interfaces

ip a

• Description: Displays all network interfaces and IP addresses.

41.2 netcat (nc) - Debug network connections

nc -zv 192.168.1.1 22

• Description: Checks if port 22 is open on 192.168.1.1.

You might also like