🚀 Essential Linux Commands for DevOps Engineers
1. File and Directory Operations
ls
: List files and directories.
ls -l
Example: List all files and directories in the current directory with detailed information.
cd
: Change directory.
cd /var/log
Example: Navigate to the /var/log
directory.
cp
: Copy files or directories.
cp file1.txt /backup/
Example: Copy file1.txt
to the /backup/
directory.
mv
: Move or rename files or directories.
mv file1.txt file2.txt
Example: Rename file1.txt
to file2.txt
.
rm
: Remove files or directories.
rm -rf /tmp/*
Example: Forcefully remove all files in the /tmp/
directory.
mkdir
: Create directories.
mkdir /myproject/logs
Example: Create a new directory logs
inside /myproject
.
touch
: Create an empty file.
touch newfile.txt
Example: Create a new empty file named newfile.txt
.
cat
: Concatenate and display file content.
cat /etc/hosts
Example: Display the contents of the /etc/hosts
file.
2. Process Management
ps
: Display currently running processes.
ps aux | grep nginx
Example: List all processes related to nginx
.
top
: Display real-time system processes and resource usage.
top
Example: Monitor system processes and resource usage interactively.
kill
: Terminate a process by PID.
kill 1234
Example: Terminate the process with PID 1234
.
systemctl
: Control system services (start, stop, restart).
systemctl restart nginx
Example: Restart the nginx
service.
3. Networking
ip
: Show/manipulate routing, devices, policy routing, and tunnels.
ip addr show
Example: Display all network interfaces and their IP addresses.
ping
: Check connectivity to a remote server.
ping google.com
Example: Send ICMP echo requests to google.com
.
curl
: Transfer data from or to a server.
curl https://example.com
Example: Retrieve the content of https://example.com
.
scp
: Securely copy files between hosts.
scp file.txt user@remotehost:/path/to/destination
Example: Copy file.txt
to a remote host under /path/to/destination
.
4. Package Management
apt-get
(Debian/Ubuntu): Install a package.
sudo apt-get install nginx
Example: Install the nginx
web server.
yum
(Red Hat/CentOS): Install a package.
sudo yum install httpd
Example: Install the httpd
web server.
rpm
: Install a package using RPM.
sudo rpm -ivh package.rpm
Example: Install a package from an RPM file.
5. Disk Usage
df
: Display disk space usage.
df -h
Example: Show disk space usage in a human-readable format.
du
: Estimate file space usage.
du -sh /var/log
Example: Display the total size of the /var/log
directory.
6. Text Processing
grep
: Search for patterns in files.
grep "error" /var/log/syslog
Example: Search for the term “error” in the /var/log/syslog
file.
awk
: Pattern scanning and processing language.
awk '{print $1}' file.txt
Example: Print the first column from file.txt
.
sed
: Stream editor for filtering and transforming text.
sed 's/oldtext/newtext/g' file.txt
Example: Replace all occurrences of oldtext
with newtext
in file.txt
.
sort
: Sort lines of text files.
sort file.txt
Example: Sort the lines in file.txt
.
uniq
: Report or omit repeated lines.
sort file.txt | uniq
Example: Remove duplicate lines from file.txt
.
wc
: Word, line, character, and byte count.
wc -l file.txt
Example: Count the number of lines in file.txt
.
7. User Management
useradd
: Create a new user.
sudo useradd devops
Example: Create a new user named devops
.
usermod
: Modify a user account.
sudo usermod -aG sudo devops
Example: Add the user devops
to the sudo
group.
passwd
: Update a user's password.
sudo passwd devops
Example: Set or change the password for the devops
user.
sudo
: Execute a command as another user, typically the superuser.
sudo systemctl restart nginx
Example: Restart the nginx
service as a superuser.
8. Compression
tar
: Create or extract tar archives.
tar -cvf archive.tar /myproject/
Example: Create a tar archive of the /myproject/
directory.
tar -xvf archive.tar
Example: Extract the contents of archive.tar
.
gzip
: Compress a file using gzip.
gzip file.txt
Example: Compress file.txt
using gzip.
gunzip
: Decompress a file using gzip.
gunzip file.txt.gz
Example: Decompress file.txt.gz
.
9. Version Control (Git)
git clone
: Clone an existing repository.
git clone https://github.com/user/repo.git
Example: Clone the Git repository at the specified URL.
git status
: Show the working tree status.
git status
Example: Display the current status of your Git repository.
git add
: Add files to staging.
git add file.txt
Example: Add file.txt
to the staging area.
git commit
: Commit changes with a message.
git commit -m "Initial commit"
Example: Commit changes with the message “Initial commit.”
git push
: Push changes to the remote repository.
git push origin main
Example: Push your commits to the main
branch on the remote repository.
10. SSH and Remote Management
ssh
: Log into a remote machine.
ssh user@hostname
Example: Connect to a remote host as the specified user.
ssh-keygen
: Generate a new SSH key.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Example: Generate a new SSH key pair.
ssh-copy-id
: Copy your SSH key to a remote machine.
ssh-copy-id user@hostnam
Example: Copy your public SSH key to the remote host.
11. Monitoring and Logs
tail -f
: Monitor logs in real-time.
tail -f /var/log/syslog
Example: Monitor the system log file in real-time.
dmesg
: Display system message buffer.
dmesg | grep error
Example: Filter and display system messages related to errors.
journalctl
: Query and display messages from the systemd journal.
journalctl -u nginx.service
Example: Display logs specific to the nginx
service.
12. File Permissions and Ownership
chmod
: Change file permissions.
chmod 755 script.sh
Example: Set the file script.sh
to be executable by the owner and readable/executable by others.
chown
: Change file ownership.
sudo chown user:usergroup file.txt
Example: Change the owner and group of file.txt
to user
and usergroup
.
umask
: Set default permissions for newly created files.
umask 022
Example: Set the default permissions for new files to 755
.
13. Search and Find
find
: Search for files and directories.
find /var/log -name "*.log"
Example: Find all .log
files under /var/log
.
locate
: Quickly find files by name.
locate apache2.conf
Example: Locate the file apache2.conf
on the system.
which
: Locate a command's executable file location.
which python3
Example: Find the path to the python3
executable.
14. Advanced File Operations
rsync
: Sync files and directories between two locations.
rsync -av /source/ /destination/
Example: Recursively sync files from /source/
to /destination/
.
ln
: Create symbolic or hard links.
ln -s /path/to/original /path/to/link
Example: Create a symbolic link to a file or directory.
diff
: Compare files line by line.
diff file1.txt file2.txt
Example: Display differences between file1.txt
and file2.txt
.
tar -cvzf
: Create a compressed archive.
tar -cvzf archive.tar.gz /myproject/
Example: Create a compressed tarball of the /myproject/
directory.
15. System Information
uname
: Show system information.
uname -a
Example: Display all system information, including kernel version, hostname, and more.
uptime
: Show how long the system has been running.
uptime
Example: Display the current time, how long the system has been up, number of users, and load average.
hostnamectl
: Control and query the system hostname and related settings.
hostnamectl set-hostname new-hostname
Example: Set the system hostname to new-hostname
.
free
: Display memory usage.
free -h
Example: Show memory usage in a human-readable format.
lscpu
: Display information about the CPU architecture.
lscpu
Example: Display detailed information about the CPU.
lsblk
: List information about block devices.
lsblk
Example: Display all block devices, including their mount points.
df -h
: Display disk space usage.
df -h
Example: Show the disk usage in a human-readable format.
du -sh
: Show the total size of a directory.
du -sh /home/user/
Example: Display the total disk space used by /home/user/
.
16. Networking Utilities
netstat
: Network statistics.
netstat -tuln
Example: List all listening ports and their associated services.
traceroute
: Trace the route packets take to a network host.
traceroute example.com
Example: Trace the path packets take to reach example.com
.
dig
: DNS lookup utility.
dig example.com
Example: Perform a DNS query for example.com
.
nslookup
: Query DNS to obtain domain name or IP address mapping.
nslookup example.com
Example: Query the DNS for example.com
.
nmap
: Network scanner.
nmap -sP 192.168.1.0/24
Example: Scan a network for active hosts.
iptables
: Configure the Linux kernel firewall.
sudo iptables -L
Example: List all current iptables rules.
17. Advanced Text Processing
tr
: Translate or delete characters.
echo "hello" | tr 'a-z' 'A-Z'
Example: Convert lowercase letters to uppercase.
cut
: Remove sections from each line of files.
cut -d ':' -f 1 /etc/passwd
Example: Display only the first field from /etc/passwd
.
paste
: Merge lines of files.
paste file1.txt file2.txt
Example: Combine corresponding lines from file1.txt
and file2.txt
.
xargs
: Build and execute command lines from standard input.
cat files.txt | xargs rm
Example: Read filenames from files.txt
and remove them.
18. Job Management
jobs
: List background jobs.
jobs
Example: Display all current background jobs.
bg
: Resume a job in the background.
bg %1
Example: Resume the first job in the background.
fg
: Bring a job to the foreground.
fg %1
Example: Bring the first job to the foreground.
nohup
: Run a command immune to hangups.
nohup command &
- Example: Run a command in the background, ignoring hangup signals.
19. System Performance
vmstat
: Report virtual memory statistics.
vmstat 5
Example: Display system performance information every 5 seconds.
iostat
: Report CPU and I/O statistics.
iostat -x 5
Example: Display extended I/O statistics every 5 seconds.
sar
: Collect, report, or save system activity information.
sar -u 5
Example: Display CPU usage every 5 seconds.
20. Archiving and Backup
zip
: Create a compressed archive.
zip -r archive.zip /myfolder/
Example: Create a ZIP archive of the /myfolder/
directory.
unzip
: Extract files from a ZIP archive.
unzip archive.zip
Example: Extract the contents of archive.zip
.
rsnapshot
: File system snapshot utility for backups.
rsnapshot daily
Example: Perform a daily backup using rsnapshot
(requires configuration).
These commands are foundational for managing Linux environments in DevOps practices, enabling efficient file operations, process management, networking, and more.