A Comprehensive Guide to Linux Commands for DevOps Warriors
Master the Essential Linux Commands for DevOps Success
System Info Commands
Understanding system information is crucial for any DevOps engineer. Here are some essential Linux commands that provide valuable insights into your system:
hostname
The hostname
command displays the name of the system host. It helps you identify the machine you're working on, especially in a networked environment.
hostid
The hostid
command shows the host ID assigned to the system by the operating system. It can be useful for network administration and security purposes.
date
The date
command shows the current date and time in UTC format. It is helpful for tracking system events and synchronizing time-sensitive operations.
whoami
The whoami
command displays the currently logged-in username of the terminal. It helps you identify the user context in which you are operating.
uptime
The uptime
command shows the elapsed time duration since the machine logged in. It provides information about the system's uptime, load average, and active users.
uname
The uname
command provides information about the system's kernel and operating system. It displays details such as the kernel name, version, release, and machine architecture.
clear
The clear
command clears the terminal screen, making it easier to read and work with the command output.
history
The history
command lists all the commands executed in the current session. It helps you review and repeat previously executed commands.
sudo
The sudo
command stands for "Super User Do" and allows authorized users to execute commands with administrative privileges. It is often used to perform system-level tasks.
echo $?
The echo $?
command shows the exit status of the last executed command. A status of 0 indicates success, while any other value indicates an error or failure.
shutdown -r now
The shutdown -r now
command restarts the machine immediately. The -r
option specifies a restart, and now
indicates an immediate action.
printenv
The printenv
command displays all the environment variables of the Linux system. It helps you view and manage system-wide and user-specific settings.
last
The last
command shows a list of previous logins in the Linux system. It provides information about the users who have logged in and logged out.
Directory Commands
Managing directories is an essential part of working with Linux. Here are some useful commands for navigating and manipulating directories:
pwd
The pwd
command shows the present working directory. It displays the full path of the directory you are currently in, helping you understand your current location in the file system.
cd
The cd
command is used to change the current directory. Here are some variations of the cd
command:
cd ..
: Moves to the parent directory (i.e., one level up).cd <directory>
: Changes to the specified directory.cd ~
orcd
: Changes to the currently logged-in user's home directory.cd ../..
: Changes the directory two levels up.cd -
: Changes to the last working directory.
mkdir
The mkdir
command is used to create a new directory. Here are some variations of the mkdir
command:
mkdir <directory>
: Creates the specified directory.mkdir -p <directory>
: Creates the directory along with its parent directories if they do not exist.
ls
The ls
command lists the files and folders in the current directory. Here are some variations of the ls
command:
ls -a
: Lists all files and folders, including hidden files.ls -al
: Lists all files and folders in a formatted manner, including hidden files.
File Commands
Working with files is a common task in the DevOps world. Here are some essential Linux commands for managing files:
touch
The touch
command creates an empty file or updates the timestamp of an existing file. Here are some variations of the touch
command:
touch <file>
: Creates a single empty file.touch <file1> <file2>
: Creates multiple empty files.
cat
The cat
command concatenates and displays the contents of files. Here are some variations of the cat
command:
cat <file>
: Displays the contents of the file.cat > <file>
: Creates a new file, allows interactive input, and redirects the inputted content to the created file.
head
The head
command displays the first few lines of a file. Here are some variations of the head
command:
head <file>
: Displays the first 10 lines of the file by default.head -n <number> <file>
: Displays the first<number>
lines of the file.
tail
The tail
command displays the last few lines of a file. Here are some variations of the tail
command:
tail <file>
: Displays the last 10 lines of the file by default.tail -n <number> <file>
: Displays the last<number>
lines of the file.
less
The less
command is used to view large files in a paginated manner. It allows you to scroll through the file and search for specific content.
rm
The rm
command is used to remove files and directories. Here are some variations of the rm
command:
rm <file>
: Removes the specified file.rm -r <directory>
: Removes files and folders of the directory recursively.rm -rf <directory>
: Forcefully removes files and folders of the directory recursively.
cp
The cp
command is used to copy files and directories. Here are some variations of the cp
command:
cp <source> <destination>
: Copies the files and folders from the source to the destination.cp -r <dir1> <dir2>
: Copies thedir1
directory to thedir2
directory recursively.
mv
The mv
command is used to move or rename files and directories. Here are some variations of the mv
command:
mv <file> <new_name>
: Renames the file to the new name.mv <file> <path>
: Moves the file to the new path.
File Permission Commands
Managing file permissions is crucial for security and access control. Here are some Linux commands for managing file permissions:
ls -l
The ls -l
command shows the permissions of a file. It displays detailed information about the file, including ownership, permissions, size, and modification date.
ls -ld
The ls -ld
command shows the permissions of a directory. It displays the permissions and other details of the directory itself, rather than its contents.
chmod
The chmod
command is used to change the mode or permissions of a file. Here are some variations of the chmod
command:
chmod <permissions> <file>
: Changes the permissions of the file.chmod -R <permissions> <directory>
: Changes the permissions of the directory and its contents recursively.
chown
The chown
command is used to change the user ownership of a file. Here are some variations of the chown
command:
chown <user> <file>
: Changes the user ownership of the file.chown <user>:<group> <file>
: Changes the user and group ownerships of the file.
chgrp
The chgrp
command is used to update the group name for a file or directory. Here are some variations of the chgrp
command:
chgrp <group> <file>
: Updates the group name for the file or directory.
getfacl
The getfacl
command shows the file or directory's access control list (ACL). It displays detailed information about the file's permissions and access restrictions.
setfacl
The setfacl
command is used to modify the access control list (ACL) of a file or directory. Here are some variations of the setfacl
command:
setfacl -m u::<permissions> <file>
: Modifies the current ACL of the file for the user.setfacl -x u::<permissions> <file>
: Removes the ACL permissions for the user.setfacl -m g::<permissions> <file>
: Modifies the group ACLs for the file.setfacl -x g::<permissions> <file>
: Removes the group ACL permissions for the file.
File Permission Octal Numbers
In Linux, file permissions are represented using octal numbers. Each permission (read, write, execute) is assigned a value:
Read (r) - 4
Write (w) - 2
Execute (x) - 1
To provide permissions to a file or directory, you add up the numbers:
rwx
(read, write, execute) = 4 + 2 + 1 = 7rw-
(read, write) = 4 + 2 = 6r-x
(read, execute) = 4 + 1 = 5
By summing the numbers, you can easily assign the appropriate permissions to a file or directory.
User Management Commands
Managing user accounts is crucial for system administration. Here are some Linux commands for user management:
useradd
The useradd
command is used to create a user account. Here are some variations of the useradd
command:
useradd <username>
: Creates a user account without home and mail spool directories.useradd -m <username>
: Creates a user account with home and mail spool directories.
passwd
The passwd
command is used to create a password for a user and store it in the /etc/shadow
file.
userdel
The userdel
command is used to delete a user from the system. Here are some variations of the userdel
command:
userdel <username>
: Deletes the user from the system.userdel -r <username>
: Deletes the user from the system along with the home and mail spool directories.
/etc/passwd
The /etc/passwd
file stores information about user accounts. You can use the cat /etc/passwd
command to display the complete list of users on the machine.
/etc/shadow
The /etc/shadow
file stores the password for users in an encrypted format. You can use the cat /etc/shadow
command to display the complete list of user passwords on the machine.
su
The su
command stands for "substitute user" and allows you to switch to another user account. Here are some variations of the su
command:
su <username>
: Switches to the specified user.exit
: Logs out from the current user account.
usermod
The usermod
command is used to modify user accounts. Here are some variations of the usermod
command:
usermod -aG <group> <username>
: Adds the user to another group without removing them from other groups.
chsh
The chsh
command is used to change the default shell for a user. Here are some variations of the chsh
command:
chsh -s /bin/bash <username>
: Changes the shell to bash for the user.chsh -s /bin/sh <username>
: Changes the shell to sh for the user.
Group Management Commands
Managing groups is essential for organizing users and controlling access to resources. Here are some Linux commands for group management:
groupadd
The groupadd
command is used to create a group. It adds a new group to the system.
groupdel
The groupdel
command is used to delete a group from the system. It removes an existing group from the system.
/etc/group
The /etc/group
file stores information about groups. You can use the cat /etc/group
command to display the complete list of groups on the machine.
gpasswd
The gpasswd
command is used to manage group passwords and membership. Here are some variations of the gpasswd
command:
gpasswd -a <user> <group>
: Adds the user to the group.gpasswd -d <user> <group>
: Removes the user from the group.gpasswd -M <user1,user2> <group>
: Adds multiple users to the group and removes the existing ones from the group.
Searching Commands
Searching for files and directories is a common task for a DevOps engineer. Here are some Linux commands to help you with searching:
locate
The locate
command is used to search for files and directories based on their names. However, you need to update the database using the sudo updatedb
command before using locate
to ensure up-to-date results.
grep
The grep
command is a powerful tool for searching and filtering text. Here are some variations of the grep
command:
grep <pattern> <file>
: Searches for text patterns within the file.grep -i <pattern> <file>
: Searches for text patterns within the file, ignoring case.grep -v <pattern> <file>
: Searches for non-matching lines of text patterns.grep -l <pattern> <file>
: Displays the file names that contain the matching string.
find
The find
command is used to search for files and directories based on various criteria. Here are some variations of the find
command:
find . -name <file>
: Finds the specified file in the current directory.find <directory> -name <file>
: Finds the specified file in the directory.find <directory> -perm 754
: Finds files in the directory with the specified permission.
Hardware Information Commands
Understanding hardware information is essential for system administration and troubleshooting. Here are some Linux commands for retrieving hardware information:
free -h
The free -h
command shows system memory information in a human-readable format. It provides details about total memory, used memory, free memory, and memory utilization.
df -h
The df -h
command shows the disk space usage of mounted file systems. It displays information about total disk space, used space, available space, and file system mount points.
du
The du
command is used to calculate and display disk usage. Here are some variations of the du
command:
du -h
: Displays disk usage information in human-readable format.du -sh
: Displays the total size of the directory instead of individual files in human-readable format.du -sh <file/directory>
: Displays the total size of the specified file or directory.
Network Commands
Networking is a fundamental aspect of DevOps. Here are some Linux commands for network management and troubleshooting:
ping
The ping
command tests the reachability and responsiveness of a remote host. It sends ICMP echo request packets to the host and measures the response time.
dig
The dig
command shows DNS information of a domain. It provides details about DNS records, name servers, and other DNS-related information.
wget
The wget
command is used to retrieve or download files from the internet. It is commonly used for automated file downloads, such as software packages or updates.
curl
The curl
command is a versatile tool for making HTTP requests and retrieving data from web servers. It supports various protocols and can be used for testing APIs and downloading files.
ifconfig
The ifconfig
command displays information about available network interfaces. It provides details about IP addresses, network masks, and other network-related settings.
ip addr
The ip addr
command is an alternative to ifconfig
and displays and manipulates network interface information. It provides more detailed and up-to-date information about network interfaces.
curl
ifconfig.me
The curl
ifconfig.me
command shows the public IP address of the machine. It is useful for checking the machine's external IP address, especially in a networked environment.
netstat -antp
The netstat -antp
command shows all open TCP ports on the machine. It provides information about active connections, listening ports, and associated processes.
traceroute
The traceroute
command traces the route using packets from the source to the destination host. It shows the network path and measures the round-trip time (RTT) for each hop.
Process Information Commands
Managing processes is crucial for system performance and resource utilization. Here are some Linux commands for viewing and managing processes:
ps
The ps
command is used to display information about running processes. Here are some variations of the ps
command:
ps
: Shows the currently running processes.ps -u <username>
: Shows the processes of the specified username.ps -ef
: Shows all the processes on the system.
top
The top
command provides a real-time, dynamic view of the running processes on the system. It displays CPU usage, memory usage, and other system statistics.
kill
The kill
command is used to terminate processes gracefully. Here are some variations of the kill
command:
kill <pid>
: Terminates the process with the specified process ID (PID).
pgrep
The pgrep
command shows the process ID of processes based on their names or other criteria. It helps you identify and work with specific processes.
bg
The bg
command sends a process to the background and continues its execution without interruption. It is commonly used for running processes in the background.
fg
The fg
command brings a background process to the foreground and makes it the active process. It is used to interact with background processes.
nohup
The nohup
command runs a command or script in the background, even after the terminal is closed or the user logs out. It prevents the process from being affected by SIGHUP (hangup) signals.
Archiving File Commands
Managing and compressing files is essential for storage and transfer. Here are some Linux commands for archiving files:
tar
The tar
command is used to create and extract tar archives. Here are some variations of the tar
command:
tar -cvf <filename> <directory>
: Creates a tar file with the specified filename for the directory.tar -xvf <filename> -C <destination>
: Extracts the files from the source tar file to the destination directory.
Ubuntu Package Related Commands
Managing software packages is crucial for system configuration and maintenance. Here are some Linux commands for package management in Ubuntu:
apt
The apt
command is the package manager for Debian-based Linux distributions like Ubuntu. It provides a high-level interface for managing software packages.
apt-get
The apt-get
command is the older version of the package manager and is used for basic package management operations. It is commonly used for installing, updating, and removing packages.
apt update
The apt update
command updates the package list. It downloads the latest package information from the configured repositories, ensuring that you have up-to-date package details.
apt list --installed
The apt list --installed
command lists all the installed packages on the system. It provides a comprehensive view of the installed software packages.
apt show
The apt show
command shows detailed information about a specific package. It displays package metadata, including the package version, dependencies, and description.
apt search
The apt search
command is used to search for packages based on their names or descriptions. It helps you find relevant packages for installation or reference.
apt install
The apt install
command is used to install a package from the configured repositories. It automatically resolves dependencies and installs the required packages.
apt remove
The apt remove
command is used to remove a package from the system. It removes the specified package without removing its configuration files.
apt purge
The apt purge
command is used to remove a package along with its configuration files. It ensures a complete removal of the package from the system.
Different Linux Distributions
Linux is available in various distributions, each with its own characteristics and target audience. Here are some popular Linux distributions:
Popular Desktop Linux OS
Ubuntu Linux
Linux Mint
Arch Linux
Fedora
Debian
OpenSuse
Popular Server Linux OS
Red Hat Enterprise Linux
Ubuntu Server
CentOS
SUSE Enterprise Linux
Most Used Linux Distros in the IT Industry
In the IT industry, certain Linux distributions are more prevalent than others. Here are the most commonly used Linux distros:
RPM-based: Red Hat Enterprise Linux (RHEL) and CentOS
Debian-based: Ubuntu Server
Difference Between RPM-based and Debian-based Distros
From a user's point of view, there isn't much difference between RPM-based and Debian-based distros. Both formats are archive files with metadata attached to them. However, there are some subtle differences:
RPM files are installation files for Red Hat-based distributions, while DEB files are installation files for Debian-based distributions.
Ubuntu is based on Debian's package management system, APT (Advanced Package Tool), and DPKG (Debian Package).
Red Hat, CentOS, and Fedora are based on the RPM (Red Hat Package Manager) system.
Conclusion
Congratulations! You've completed an extensive journey through essential Linux commands for DevOps warriors. We covered a wide range of commands for system information, directory management, file manipulation, user and group management, searching, hardware information, network management, process information, archiving files, package management, and different Linux distributions. With this knowledge, you're well-equipped to navigate and manage Linux systems efficiently. Keep exploring and experimenting with these commands to become a true DevOps warrior!
Remember, Linux commands are powerful tools, and with great power comes great responsibility. Always exercise caution and double-check your commands before executing them. Happy coding and DevOps-ing! ๐ง๐ป