The command line interface is a very potent tool in Linux. It helps systems and network admins execute different tasks seamlessly. Here is a list of the top ten Linux commands that can simplify their work.
One of the best things about GNU/Linux is that it is highly configurable through the command line interface. One can manage all of GNU/Linux via the command line. This makes automation really simple, which is why GNU/Linux is so popular among systems administrators. In this article, we will discuss the top ten commands for GNU/Linux systems administrators.
In the first section of this article, we cover commands that can be used to query hardware information; the second section discusses network related commands, and the last section covers miscellaneous commands.
Querying hardware information
Finding out the CPU architecture details: We often need to find out details about the CPU’s architecture, such as its byte order, CPU mode, number of cores/threads, caches, virtualisation support, and so on. GNU/Linux provides a handy command called lscpu, which provides insights about CPU architecture. Given below is the sample output of the command:
$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 2 Core(s) per socket: 2 Vendor ID: GenuineIntel
Note: Some part of the output is not shown here, to save space.
Finding memory details: GNU/Linux has inbuilt ‘free’ commands, which display the total amount of free and used physical and swap memory in the system. In addition, it also shows the buffers and caches used by the kernel. Given below is the sample output of memory usage in megabytes:
$ free -m total used free shared buff/cache available Mem: 7923 935 3229 231 3758 6463 Swap: 2047 0 2047
In the above command, the -m option shows details in megabytes. One can also get the same information in different units, like bytes, kilobytes and gigabytes.
Find the frequency of the RAM: Systems administrators often need to upgrade hardware. To upgrade the RAM, we must know its frequency. Using the commands below, we can find the frequency of the RAM:
$ sudo lshw -short -C memory H/W path Device Class Description ============================================ /0/0 memory 1MiB BIOS /0/6 memory 8GiB System Memory /0/6/0 memory 4GiB SODIMM DDR3 1333 MHz (0.8 ns /0/6/1 memory 4GiB SODIMM DDR3 1333 MHz (0.8 ns /0/c/e memory 32KiB L1 cache /0/c/f memory 256KiB L2 cache /0/c/10 memory 3MiB L3 cache /0/d memory 32KiB L1 cache
Note: This command is executed with root privileges.
Network related commands
Resolving the domain name to the IP address: Using the nslookup command, we can find the IP address associated with a particular domain name. For example, the commands below find the IP address of google.com:
$ nslookup google.com Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: google.com Address: 22.214.171.124 Name: google.com Address: 2404:6800:4009:80a::200e
Tracing the route taken by the network packet: Using the traceroute command, we can find the number of hops between the source and the destination machines. In addition, we can also trace the path travelled by the packet using this utility. For instance, the output below shows the path travelled by the packet:
$ traceroute google.com traceroute to google.com (126.96.36.199), 30 hops max, 60 byte packets 1 _gateway (192.168.1.1) 7.073 ms 7.044 ms 7.024 ms 2 * * * 3 10.124.117.210 (10.124.117.210) 41.581 ms 41.585 ms 41.570 ms 4 10.117.225.82 (10.117.225.82) 149.475 ms 149.525 ms 149.510 ms 5 10.117.137.146 (10.117.137.146) 84.640 ms 84.663 ms 84.649 ms 6 188.8.131.52.static-Mumbai.vsnl.net.in (184.108.40.206) 53.287 ms 37.258 ms 39.688 ms
Note: Some part of the output is not shown here to save space.
Netstat: The Netstat utility shows network statistics. It prints network connections, routing tables, interface statistics and other useful information. To display routing table information, execute the following commands:
$ netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface default _gateway 0.0.0.0 UG 0 0 0 wlp2s0 link-local 0.0.0.0 255.255.0.0 U 0 0 0 wlp2s0 172.16.208.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlp2s0 192.168.195.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8
Generating a random password: Systems administrators often do user management. As a part of this, one has to generate one-time random passwords. We can use the openssl utility to generate random passwords, as follows:
$ openssl rand -base64 12
Making a file immutable: Some files are so important that we cannot afford to lose them even by accident. GNU/Linux provides the chattr command, with which we can make a file immutable. Once a file is marked as immutable, it cannot be deleted even by the root user. Let us look at this with an example:
$ touch immutable.txt $ sudo chattr +i immutable.txt
In the above command, the +i flag sets the immutable attribute to a given file. Let us try to delete this file with root privileges:
$ sudo rm immutable.txt rm: cannot remove ‘immutable.txt’: Operation not permitted
The result is as expected. We cannot remove this file unless we remove the immutable attribute. Let us try to delete it by removing the immutable attribute:
$ sudo chattr -i immutable.txt $ rm immutable.txt
Converting the DOS file format to UNIX: Users often develop scripts on one machine and copy it over to multiple machines for testing/production purposes. If a script is stored in the DOS file format, then it adds the addition control-M (^M) character as a line ending character, which makes the script unusable on GNU/Linux. To make it usable on GNU/Linux, we have to convert its file format to UNIX. We can easily do this using the dos2unix command.
Let us take a file in the DOS format, as shown below:
$ file output.txt output.txt: ASCII text, with CRLF, LF line terminator
Now convert it to UNIX and verify its file format:
$ dos2unix output.txt $ file output.txt output.txt: ASCII text
Flush caches: GNU/Linux uses RAM as a caching mechanism to improve system performance. This cache is flushed periodically. However, we can flush the cache explicitly at any time. We can either flush it using Page cache and inode cache, or both. Let us look at this with an example.
To drop Page cache, execute the following command as a root user:
# sync && echo 1 > /proc/sys/vm/drop_caches
To drop inode and dentry cache, execute the following command as a root user:
# sync && echo 2 > /proc/sys/vm/drop_caches
To drop both caches, execute the following command as a root user:
# sync && echo 3 > /proc/sys/vm/drop_caches