Linux is a powerful and versatile operating system that is widely used in many industries, including software development, data analysis, and web hosting. As a Linux engineer, it's important to have a good understanding of the essential Linux commands that can help you manage your systems and perform common daily tasks quickly.
Additionally, with the goal of increasing collaboration and streamlining processes between software development and IT operations teams, DevOps has become an integral part of successful software development projects. A key aspect of DevOps is the use of Linux, the open-source operating system that has become the backbone of many modern applications and software systems. For DevOps engineers, mastering Linux is critical to effectively managing and deploying software in a variety of environments.
In this article, we'll explore some of the most important Linux commands you should know as a DevOps engineer or to use Linux efficiently. But first, let us learn what these signs mean in Linux and answer some common questions.
What does $() do in Linux?
In Linux, $( ) is a syntax for command substitution. It allows you to use the output of one command as an argument to another command or as part of a larger command. This is a powerful feature of Linux shell scripting that allows you to chain commands together and automate tasks.
Here's an example of how $( ) works:
$ echo "Today is $(date)"
In this example, the $(date) command is substituted with the current date and time. The output of the date command is then used as an argument to the echo command, resulting in the following output:
Today is Thu Feb 16 08:30:00 UTC 2023
You can also use command substitution to assign the output of a command to a variable, like this:
$ output=$(ls -l)
In this example, the $(ls -l) command is substituted with the output of the ls -l command, which is a list of files and directories in the current directory. The output is then assigned to the output variable.
You can then use the output variable in another command or script, like this:
$ echo "$output"
This will display the output of the ls -l command that was stored in the output variable.
What does $ do in Linux?
The $ symbol is used in various ways depending on the context. Here are a few common uses:
Prompt: In the Linux command line interface (CLI), the $ symbol is often used to indicate the prompt, which is the point where the user enters commands.
$
Variables: The $ symbol is used to reference variables in shell scripts. When a variable is referenced, the $ symbol is placed before the variable name. For example, if a variable named NAME contains the value "John", you can reference the variable like this:
echo $NAME
This will display the value of the NAME variable, which is "John".
Environment variables: In Linux, there are many predefined environment variables that contain system information or user preferences. These variables can be referenced using the $ symbol. For example, the $HOME variable contains the path to the current user's home directory. You can reference the $HOME variable like this:
cd $HOME
This will change the current directory to the home directory of the current user.
Process ID: The $ symbol followed by a number can be used to reference the process ID (PID) of a running process. For example, if you want to kill a process with a PID of 1234, you can use the kill command like this:
kill -9 $1234
This will send a signal to the process with the PID of 1234 to terminate it.
Now, Let us start with the most used commands.
System Information
uname
The uname command is used to display information about the system, such as the kernel version and system architecture. You can use this command to check if you're running the latest version of the Linux kernel. Here's an example:
$ uname -a
Linux myserver 5.10.0-8-amd64 #1 SMP Debian 5.10.46-4 (2021-07-03) x86_64 GNU/Linux
lsb_release
The lsb_release command displays Linux Standard Base (LSB) information about the distribution. This can be useful when you need to know the exact version of your Linux distribution. Here's an example:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
hostname
The hostname command is used to display or set the system's host name. The host name is a label that identifies the system in a network. By default, the host name is set during installation, but you can change it using the hostname command.
To display the current host name, run the command with no arguments:
$ hostname
myhost
To set a new host name, use the -b option followed by the new host name:
$ sudo hostname -b newhost
Tip
You can add the new host name to the /etc/hosts file to make the change permanent.
top
The top command is used to display system process information and monitor system resources. It shows a real-time list of the processes running on the system and their resource usage, such as CPU and memory usage.
To launch the top command, simply type top in the terminal. The default view shows the processes sorted by the percentage of CPU usage:
$ top
You can use various keys to interact with the top command. For example, you can press q to quit, k to kill a process, and f to change the displayed fields.
Tip
To sort the processes by a specific field, press the corresponding key. For example, press P to sort by CPU usage, M to sort by memory usage, and T to sort by total CPU time.
File and Directory Management
ls
The ls command is used to list the contents of a directory. By default, it lists the files and directories in the current working directory. You can also use it to list the contents of a specific directory by specifying the directory path as an argument. You can use the -l flag to get more detailed information about each file, such as the file size and permissions.
To list the contents of the current working directory, run the ls command with no arguments:
$ ls
file1.txt file2.txt dir1
To list the contents of a specific directory, specify the directory path as an argument:
$ ls /home/user/documents
file1.txt file2.txt dir1
To list the contents of a directory in long format, use the -l flag:
$ ls -l
-rw-r--r-- 1 user user 15 Sep 24 16:25 file1.txt
-rw-r--r-- 1 user user 23 Sep 24 16:26 file2.txt
drwxr-xr-x 2 user user 6 Sep 24 16:27 dir1
cd
The cd command is used to change the current working directory. You can use it to navigate the file system.
To change the current working directory to a specific directory, specify the directory path as an argument:
$ cd /home/user/documents
To change to the parent directory, use cd ..:
$ cd ..
To change to the home directory, use cd with no arguments:
$ cd
mkdir
The mkdir command is used to create a new directory.
To create a new directory in the current working directory, specify the directory name as an argument:
$ mkdir newdir
To create a new directory with multiple levels of subdirectories, use the -p flag:
$ mkdir -p newdir/subdir1/subdir2
rmdir
The rmdir command is used to remove a directory.
To remove an empty directory, specify the directory name as an argument:
$ rmdir newdir
touch
The touch command is used to create a new file or update the modification time of an existing file.
To create a new file in the current working directory, specify the file name as an argument:
$ touch newfile.txt
To update the modification time of an existing file, specify the file name as an argument:
$ touch existingfile.txt
rm
The rm command is used to remove files.
To remove a file, specify the file name as an argument:
$ rm file.txt
To remove multiple files, specify the file names as arguments separated by spaces:
$ rm file1.txt file2.txt
cp
The cp command is used to copy files or directories.
To copy a file to a new location, specify the source file and the destination directory as arguments:
$ cp file1.txt /home/user/documents
To copy a directory and all its contents to a new location, use the -r flag:
$ cp -r dir1 /home/user/documents
mv
The mv command is used to move or rename files or directories.
To move a file to a new location, specify the source file and the destination directory as arguments:
$ mv file1.txt /home/user/documents
To rename a file, specify the old file name and the new file name as arguments:
$ mv oldfile.txt newfile.txt
To move a directory and all its contents to a new location, use the -r flag:
$ mv -r dir1 /home/user/documents
Tip
Be careful when using the rm command, as it can permanently delete files and directories without confirmation. Always double-check the list of files you're about to delete before running the command.
System Maintenance
reboot
The reboot command is used to restart the system.
To restart the system, simply run the reboot command:
$ sudo reboot
Tip
Before running the reboot command, make sure to save any unsaved work and close any running applications.
shutdown
The shutdown command is used to shut down the system.
To shut down the system, simply run the shutdown command with the -h flag:
$ sudo shutdown -h now
Tip
Before running the shutdown command, make sure to save any unsaved work and close any running applications.
df
The df command is used to show disk space usage. It displays the file system usage for all mounted file systems.
To display the disk space usage, run the df command with no arguments:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 492670736 2979884 462082592 1% /
tmpfs 16383224 0 16383224 0% /dev/shm
...
You can use the -h flag to display the sizes in a human-readable format:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 471G 2.9G 441G 1% /
tmpfs 16G 0 16G 0% /dev/shm
...
du
The du command is used to show disk usage of a file or directory.
To display the disk usage of a file or directory, run the du command followed by the file or directory path:
$ du /home/user/documents
784 /home/user/documents/dir1/subdir1
200 /home/user/documents/dir1/subdir2
1024 /home/user/documents/dir1
15 /home/user/documents/file1.txt
23 /home/user/documents/file2.txt
1863 /home/user/documents
sizes in a human-readable format:
$ du -h /home/user/documents
784K /home/user/documents/dir1/subdir1
200K /home/user/documents/dir1/subdir2
1.1M /home/user/documents/dir1
15K /home/user/documents/file1.txt
23K /home/user/documents/file2.txt
1.9M /home/user/documents
ps
The ps command is used to show the current processes running on the system.
To display the current processes, run the ps command:
$ ps
PID TTY TIME CMD
2713 pts/0 00:00:00 bash
2824 pts/0 00:00:00 ps
You can use the -aux flag to display all the processes in a full format:
$ ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 2713 0.0 0.1 25572 6860 pts/0 Ss 11:42 0:00 bash
user 2825 0.
kill
The kill command is used to send a signal to a process to terminate it.
To kill a process, you first need to identify its process ID (PID) using the ps command. Then, you can use the kill command with the PID as an argument:
$ ps aux | grep processname
user 1234 0.0 0.1 25572 6860 pts/0 Ss 11:42 0:00 processname
$ sudo kill 1234
You can also use the pkill command to kill a process by its name:
$ pkill processname
Tip
When killing a process, use the kill -9 command if the process is not responding to the regular kill command.
User and Group Management
useradd
The useradd command is used to create a new user account.
To create a new user account, run the useradd command followed by the desired username:
$ sudo useradd username
You can also specify additional options, such as the home directory and login shell:
$ sudo useradd -m -s /bin/bash username
usermod
The usermod command is used to modify an existing user account.
To modify an existing user account, run the usermod command followed by the desired options and username:
$ sudo usermod -d /new/home/dir -s /bin/zsh username
You can use various options to modify different attributes of the user account, such as the home directory, login shell, and group membership.
userdel
The userdel command is used to delete an existing user account.
To delete an existing user account, run the userdel command followed by the username:
$ sudo userdel username
passwd
The passwd command is used to set or change the password for a user account.
To set or change the password for a user account, run the passwd command followed by the username:
$ sudo passwd username
You will be prompted to enter the new password and then confirm it.
groupadd
The groupadd command is used to create a new group.
To create a new group, run the groupadd command followed by the desired group name:
$ sudo groupadd groupname
groupmod
The groupmod command is used to modify an existing group.
To modify an existing group, run the groupmod command followed by the desired options and group name:
$ sudo groupmod -n newgroupname groupname
You can use various options to modify different attributes of the group, such as the group name.
groupdel
The groupdel command is used to delete an existing group.
To delete an existing group, run the groupdel command followed by the group name:
$ sudo groupdel groupname
Tip
When adding or modifying a user account, make sure to assign the user to the appropriate group to grant the appropriate permissions.
Package Management
apt-get
The apt-get command is used for package management on Debian-based systems.
To install a package using apt-get, run the command followed by the package name:
$ sudo apt-get install packagename
To remove a package using apt-get, run the command followed by the package name:
$ sudo apt-get remove packagename
To update the package list using apt-get, run the command with the update option:
$ sudo apt-get update
To upgrade all installed packages using apt-get, run the command with the upgrade option:
$ sudo apt-get upgrade
yum
The yum command is used for package management on Red Hat-based systems.
To install a package using yum, run the command followed by the package name:
$ sudo yum install packagename
To remove a package using yum, run the command followed by the package name:
$ sudo yum remove packagename
To update the package list using yum, run the command with the update option:
$ sudo yum update
To upgrade all installed packages using yum, run the command with the upgrade option:
$ sudo yum upgrade
dpkg
The dpkg command is used for package management on Debian-based systems.
To install a package using dpkg, run the command followed by the package name:
$ sudo dpkg -i packagename.deb
To remove a package using dpkg, run the command followed by the package name:
$ sudo dpkg -r packagename
To list all installed packages using dpkg, run the command with the -l option:
$ sudo dpkg -l
To search for a package using dpkg, run the command followed by the package name:
$ sudo dpkg -s packagename
Tip
When managing packages, make sure to only install packages from trusted sources to avoid potential security risks.
pacman
The pacman command is used for package management on Arch Linux systems.
To install a package using pacman, run the command followed by the package name:
$ sudo pacman -S packagename
To remove a package using pacman, run the command followed by the package name:
$ sudo pacman -R packagename
To update the package list using pacman, run the command with the -Sy options:
$ sudo pacman -Sy
To upgrade all installed packages using pacman, run the command with the -Syu options:
$ sudo pacman -Syu
To search for a package using pacman, run the command followed by the search term:
$ sudo pacman -Ss searchterm
Tip
When managing packages, make sure to only install packages from trusted sources to avoid potential security risks.
Text Editors
nano
nano is a simple text editor for the terminal. It is easy to use and requires no prior knowledge of command-line text editors.
To open a file in nano, simply run the command followed by the file name:
$ nano filename
You can then use the keyboard to edit the file. Press Ctrl + X to save and exit the file.
vim
vim is a powerful text editor that can be used in the terminal or in a GUI. It is a modal editor, which means it has different modes for editing and navigation.
To open a file in vim, simply run the command followed by the file name:
$ vim filename
You can then use the keyboard to edit the file. Press Esc to enter command mode, and i to enter insert mode. To save and exit the file, type :wq and press Enter.
emacs
emacs is another powerful text editor with extensive customization options. It is also a modal editor, but with a different set of modes than vim.
To open a file in emacs, simply run the command followed by the file name:
$ emacs filename
You can then use the keyboard to edit the file. Press Ctrl + X followed by Ctrl + S to save the file, and Ctrl + X followed by Ctrl + C to exit Emacs.
Tip
When using vim or emacs, it can be helpful to learn some of the basic keyboard shortcuts to increase your efficiency.
Network Management
ifconfig
ifconfig is a command-line tool used to show information about network interfaces on a Linux system.
To display information about all network interfaces, simply run the command without any options:
$ ifconfig
You can then see information such as the IP address, netmask, and MAC address of each interface.
ip (don't work for mac)
ip is a more modern tool used to show and configure network interfaces and routing tables on a Linux system.
To display information about all network interfaces, run the command with the address option:
$ ip address
You can then see information such as the IP address, netmask, and MAC address of each interface.
netstat
netstat is a command-line tool used to display network connections, routing tables, and network interface statistics on a Linux system.
To display all open network connections, run the command with the a and n options:
$ netstat -an
You can then see information such as the protocol, local address, foreign address, and state of each connection.
ping
ping is a command-line tool used to test network connectivity to a host by sending ICMP echo request packets and waiting for ICMP echo reply packets.
To test network connectivity to a host, run the command followed by the host name or IP address:
$ ping hostname
You can then see information such as the number of packets sent and received, and the round-trip time for each packet.
traceroute
traceroute is a command-line tool used to show the path that packets take to reach a destination host.
To show the path to a destination host, run the command followed by the host name or IP address:
$ traceroute hostname
You can then see information such as the IP addresses and round-trip times for each hop along the path.
nslookup
nslookup is a command-line tool used to query the DNS (Domain Name System) to obtain information about a domain or hostname.
To look up the IP address of a domain or hostname, run the command followed by the domain or hostname:
$ nslookup domainname
You can then see information such as the IP address and DNS server used for the lookup.
dig
dig is a command-line tool similar to nslookup that is used to query the DNS to obtain information about a domain or hostname.
To look up the IP address of a domain or hostname, run the command followed by the domain or hostname:
$ dig domainname
You can then see information such as the IP address and DNS server used for the lookup.
ifup and ifdown
ifup and ifdown are command-line tools used to bring up or take down a network interface on a Linux system.
To bring up a network interface, run the ifup command followed by the interface name:
$ sudo ifup interfacename
To take down a network interface, run the ifdown command followed by the interface name:
$ sudo ifdown interfacename
route
route is a command-line tool used to view and configure the routing table on a Linux system.
To display the current routing table, run the command with the -n option:
$ route -n
You can then see information such as the destination network, gateway, and interface for each route.
iptables
iptables is a command-line tool used to configure the Linux kernel's firewall rules. It can be used to block or allow incoming or outgoing network traffic based on a variety of criteria.
To view the current firewall rules, run the command with the -L option:
$ sudo iptables -L
You can then see information such as the chain, target, and protocol for each rule.
Tip
When managing network connections, use a combination of these tools to diagnose and troubleshoot network issues.
Conclusion:
learning Linux is critical for any engineer or technical professional, regardless of their specific field. Here are a few reasons why:
Linux is widely used in the industry: Linux is one of the most widely used operating systems in the tech industry, particularly in server and cloud environments. Learning Linux can give you a strong foundation in the skills and tools used by many companies and organizations.
Linux is open-source: Linux is open-source software, which means that its source code is freely available to anyone. This allows developers and engineers to modify, improve, and extend the operating system to suit their specific needs.
Linux is highly customizable: Linux is highly customizable and can be configured to suit a wide range of use cases. This makes it a popular choice for developers and engineers who need to build custom solutions for their organizations.
Linux offers a powerful command-line interface: Linux is well-known for its powerful command-line interface, which provides a high degree of control and flexibility for users. This can be particularly useful for engineers who need to automate tasks, manage networks, or work with servers.
Linux skills are in high demand: Linux skills are in high demand in the tech industry, particularly in fields like cloud computing, network administration, and software development. By learning Linux, you can increase your career opportunities and earning potential.
In summary, learning Linux can provide a strong foundation in the skills and tools used by many organizations in the tech industry. Its open-source nature, customizability, and powerful command-line interface make it a popular choice for developers and engineers. With the high demand for Linux skills in the tech industry, learning Linux can also provide significant career benefits.
Top comments (0)