Home Audience Admin The Top Ten Commands for Linux Systems and Network Admins

The Top Ten Commands for Linux Systems and Network Admins

0
5887

What makes Linux superior to other operating systems is a rich set of commands that make it highly configurable via the CLI. Linux is heavily loaded with commands and one can manage the entire operating system from the terminal alone. That’s why it is so popular with systems administrators. In this article we will give a brief on the top ten commands for Linux systems and network administrators.

Of the four sections in this article, the first one discusses networking commands, the second covers process management commands, the third section looks at Docker commands and the final one discusses miscellaneous commands.

Networking commands

1. curl
This is a tool to transfer data from or to a server. It supports many protocols, like FTP, SCP, HTTP, HTTPS and the list goes on. A popular use of this command is to execute a HTTP request.
Let us execute the HTTP GET request using the curl command as follows:

$ curl https://reqres.in/api/users/2

When you execute the above command, it generates the following output:

{
“data”: {
“avatar”: “https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg”,
“email”: “janet.weaver@reqres.in”,
“first_name”: “Janet”,
“id”: 2,
“last_name”: “Weaver”
}
}

We can also execute other HTTP requests with the curl command. Let us execute the POST request. We will use the following JSON body as a payload:

{
“first_name”: “John”,
“last_name”: “Doe”,
“email”: “johndoe@reqres.in”
}’

Let us create a POST request with the above payload:

$ curl -X POST https://reqres.in/api/users/2 -H ‘Content-Type: application/json’ -d ‘{“first_name”: “John”, “last_name”: “Doe”, “email”: “johndoe@reqres.in”}’

In the above command:

  • -X is used to specify the request type. In this case, it is POST.
  • -H option is used to specify the HTTP headers.
  • -d option is used to specify data. In this case, it is JSON payload.

When you execute the above command, it displays the same JSON payload to indicate successful command execution.

2. wget
We often download files from the Internet through a browser or download manager. Wouldn’t it be great if we could do the same using the command line interface (CLI)? We can easily do this with the wget command, as a non-interactive network download. To download any file, just provide its URL to the wget command as follows:

$ wget http://softwares.eng.acme.com:8000/ubuntu-18.04.iso

We can also download multiple files using this command. To do this, first store all the URLs in a text file as follows:

$ cat downloads.txt
http://softwares.eng.acme.com:8000/ubuntu-18.04.iso
http://softwares.eng.acme.com:8000/ubuntu-18.10.iso
http://softwares.eng.acme.com:8000/ubuntu-19.04.iso
http://softwares.eng.acme.com:8000/ubuntu-19.10.iso

Now use the -i option with the wget command:

$ wget -i downloads.txt

The above command will download all the files one by one in a sequence.

3. lsof
As the name suggests, the lsof command is used to list open files. It lists information about the files that are used by various processes. In the Linux world, everything is a file; hence, sockets, directories and pipes are all treated as files. Using this command, we get information about any opened file object.
For example, we can use the -i flag to list all the open network connections. Shown below is the sample output of the same:

$ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chrome 7302 jarvis 153u IPv4 276767 0t0 UDP *:mdns
chrome 7302 jarvis 154u IPv6 276768 0t0 UDP *:mdns
chrome 7302 jarvis 155u IPv4 276769 0t0 UDP *:mdns
chrome 7302 jarvis 156u IPv6 276770 0t0 UDP *:mdns
chrome 7302 jarvis 157u IPv4 276771 0t0 UDP *:mdns
chrome 7302 jarvis 158u IPv6 276772 0t0 UDP *:mdns
chrome 7302 jarvis 174u IPv4 869833 0t0 TCP jarvis-machine:33736->104.25.57.103:https (ESTABLISHED)
chrome 7302 jarvis 229u IPv4 120340 0t0 TCP jarvis-machine:57138->stackoverflow.com:https (ESTABLISHED)
chrome 7302 jarvis 252u IPv4 868116 0t0 TCP jarvis-machine:56792->56.b6.7434.ip4.static.sl-reverse.com:https (ESTABLISHED)
chrome 7302 jarvis 254u IPv4 276773 0t0 UDP *:mdns
chrome 7302 jarvis 285u IPv4 871409 0t0 TCP jarvis-machine:36198->104.244.42.136:https (ESTABLISHED)
chrome 7302 jarvis 288u IPv4 868120 0t0 TCP jarvis-machine:50274->server-13-35-202-12.hyd50.r.cloudfront.net:https (CLOSE_WAIT)

We can also list all the network files in use by a specific process. For instance, the command below shows all the network files used by Chrome.

$ lsof -i -a -p 7302
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chrome 7302 jarvis 153u IPv4 276767 0t0 UDP *:mdns
chrome 7302 jarvis 154u IPv6 276768 0t0 UDP *:mdns
chrome 7302 jarvis 155u IPv4 276769 0t0 UDP *:mdns
chrome 7302 jarvis 156u IPv6 276770 0t0 UDP *:mdns
chrome 7302 jarvis 157u IPv4 276771 0t0 UDP *:mdns
chrome 7302 jarvis 158u IPv6 276772 0t0 UDP *:mdns
chrome 7302 jarvis 174u IPv4 869833 0t0 TCP jarvis-machine:33736->104.25.57.103:https (ESTABLISHED)
chrome 7302 jarvis 229u IPv4 120340 0t0 TCP jarvis-machine:57138->stackoverflow.com:https (ESTABLISHED)
chrome 7302 jarvis 252u IPv4 868116 0t0 TCP jarvis-machine:56792->56.b6.7434.ip4.static.sl-reverse.com:https (CLOSE_WAIT)
chrome 7302 jarvis 254u IPv4 276773 0t0 UDP *:mdns
chrome 7302 jarvis 285u IPv4 871409 0t0 TCP jarvis-machine:36198->104.244.42.136:https (ESTABLISHED)

We can also list the processes that are listening on a particular port by using the -i option with ‘:’. For example, the command below lists the processes that are listening on Port 8080:

$ lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 25737 jarvis 3u IPv4 901409 0t0 TCP *:http-alt (LISTEN)

The above output shows that Python is listening on Port 8080.

Process management commands

1. nohup
There are often times when we want to execute long running jobs that are immune to log outs, or wish to terminate an unplanned login session. For such cases, the nohup command comes to the rescue. It runs a command and makes it immune to hangups, with the output to a non-tty device. nohup does not disconnect a command from the terminal; it makes the command ignore SIGHUP, and redirects stdout/stderr to a file nohup.out, so that the command can continue running in the background even when the user logs out.

To understand this, let us create a simple script that prints numbers from 1 to n in a loop:

$ cat counter.sh
#!/bin/bash

cnt=1

while [ 1 ];
do
echo “Current counter value = ${cnt}”
let ++cnt
sleep 1
done

Now make this script executable and run it in the background with the nohup command:

$ chmod +x counter.sh
$ nohup ./counter.sh& # Please note ampersand(&) symbol at the end of command. This will run script in background

After the successful execution of this command, the following output is shown on the terminal:

[1] 3745
ignoring input and appending output to ‘nohup.out’

In the above command, 3745 is the PID of the counter.sh script. Now, exit from the terminal, open a new terminal and check whether counter.sh script is still running.

$ pgrep counter.sh
3745

The above output shows that the script is still running with the same PID.

2. pidof
The pidof command finds the process IDs (pids) of the named programs. It prints these IDs on the standard output. For instance, if we want to find the process ID of bash, then we can execute the command as follows:

$ pidof bash
28711 20899 20891 5960

I have four instances of bash, and hence there are four process IDs listed.
To list the process ID of a script, we have to use the -x option. Let us first create a script as follows:

$ cat pidof-demo.sh
#!/bin/bash

while [ 1 ];
do
echo “script for demo of pidof command”
sleep 1
done

Now, make it executable and run it in a terminal as follows:

$ chmod +x pidof-demo.sh
$ ./pidof-demo.sh

Now execute the pidof command from another terminal with the -x option, as follows:

$ pidof -x pidof-demo.sh
22211

The above output shows 22211 as a process ID of the pidof-demo.sh shell script.

Docker commands
Container technologies are getting more popular these days. We often see containers in production as well. Docker is one of the most mature and popular container technologies in the market. Hence it is worthwhile to discuss a few Docker related commands in this article. Let us begin with a few useful ones.

1. docker exec
docker exec allows us to execute commands inside a running container. Given below is the syntax of the same:

docker exec [OPTIONS] CONTAINER COMMAND

Let us execute the hostname command inside a running container. But before that, let us find the container name, as follows:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
905464421cb3 alpine “tail” About a minute ago Up About a minute pedantic_kepler

In my case, the container name is pedantic_kepler. Now let us execute the hostname command inside it, as follows:

$ docker exec pedantic_kepler hostname
905464421cb3

The above output shows the hostname of a container which is the same as a container ID.
This approach is good when you want to execute a single command. But what if you want to execute multiple commands? In that case, you can create an interactive session with a container. For instance, the command below creates an interactive session with a container using the sh shell:

$ docker exec -it pedantic_kepler /bin/sh
/ # hostname
905464421cb3
/ #
/ # uname -a
Linux 905464421cb3 4.18.0-10-generic #11-Ubuntu SMP Thu Oct 11 15:13:55 UTC 2018 x86_64 Linux
/ # exit

In the above command:

a) -i option keeps STDIN open even if it is not attached.
b) -t option allocates a pseudo-TTY.

2. docker cp
We often need to copy files inside a container from the host machine and vice versa. We can do that easily with the docker cp command.
First, let us look at how to copy a file from the host machine to a Docker container. Shown below is the syntax for the same:

docker cp [OPTIONS] <SRC_PATH> <CONTAINER>:<DEST_PATH>

Now let us copy the source file—file-from-host.sh’ inside ‘pedantic_kepler’ container, as follows:

$ docker cp file-from-host.sh pedantic_kepler:/tmp

Next, let us verify whether the file has indeed been copied:

$ docker exec pedantic_kepler ls -lrth /tmp
total 0
-rw-r--r-- 1 0 0 20 Dec 31 16:34 file-from-host.sh

Yes, it’s there.
Now, let us look at how to copy a file from a container to the host machine. The syntax for this command is as follows:

docker cp [OPTIONS] <CONTAINER>:<SRC_PATH> <DEST_PATH>

Next, copy the file-from-host.sh file from the pedantic_kepler container to the host machine as follows:

$ docker cp pedantic_kepler:/tmp/file-from-host.sh ${PWD}

Let us verify that the file has indeed been copied.

$ ls -lrth
total 0
-rw-r--r-- 1 jarvis jarvis 20 Dec 31 22:04 file-from-host.sh

Yes, it’s been copied.

3. docker inspect
The docker inspect command provides low-level information on Docker objects. For example, if we execute this command on a container, then it will provide useful information like its ID, status, image used to spawn the container, network related information, and so on. We can use this command with various Docker objects like image, container, volume, etc.
Let us execute this command on a running container. Shown below is the output for the same:

$ docker inspect pedantic_kepler
[
{
“Id”: “905464421cb38c84 f30b91febad1ef9 8026564fb2b7ada310480996a2a844081”,
“Created”: “2019-12-31T15:58:16.328534109Z”,
“Path”: “tail”,
“Args”: [],
“State”: {
“Status”: “running”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 2834,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2019-12-31T15:58:16.878991653Z”,
“FinishedAt”: “0001-01-01T00:00:00Z”
},
“Image”: “sha256:cc0abc535e36a7ede71978ba2bbd8159b8a5420b91f2fbc520cdf5f673640a34”,
“ResolvConfPath”: “/var/lib/docker/containers/905464421cb38c84f30b91febad1ef98026564fb2b7ada310480996a2a844081/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/905464421cb38c84f30b91febad1ef98026564fb2b7ada310480996a2a844081/hostname”,
“HostsPath”: “/var/lib/docker/containers/905464421cb38c84f30b91febad1ef98026564fb2b7ada310480996a2a844081/hosts”,
“LogPath”: “/var/lib/docker/containers/905464421cb38c84f30b91febad1ef98026564fb2b7ada310480996a2a844081/905464421cb38c84f30b91febad1ef98026564fb2b7ada310480996a2a844081-json.log”,
“Name”: “/pedantic_kepler”,
“NetworkSettings”: {
“SandboxID”: “50012f8d3457083e342 1a8f845c8d3971ecf8013b4307667136f08dcc0ed2031”,
“HairpinMode”: false,
“LinkLocalIPv6PrefixLen”: 0,
“SandboxKey”: “/var/run/docker/netns/50012f8d3457”,
“EndpointID”: “c4573bdaca28702bf0f c8fbe2e308179fcfd394b780990215c7a019cb5ce4b4c”,
“Gateway”: “172.17.0.1”,
“GlobalIPv6PrefixLen”: 0,
“IPAddress”: “172.17.0.2”,
“IPPrefixLen”: 16,
“MacAddress”: “02:42:ac:11:00:02”,
“Networks”: {
“bridge”: {
“NetworkID”: “f1f8d33abfc20712e 0ab2d666e0b736f4a6920f386fca91fe464450432ae87e2”,
“EndpointID”: “c4573bdaca28702bf0 fc8fbe2e308179fcfd394b780990215c7a019cb5ce4b4c”,
“Gateway”: “172.17.0.1”,
“IPAddress”: “172.17.0.2”,
“IPPrefixLen”: 16,
“GlobalIPv6PrefixLen”: 0,
“MacAddress”: “02:42:ac:11:00:02”,
}
}
}
}
]

Miscellaneous commands

1. cksum
Let us suppose that you have downloaded a file from the Internet and are about to use it. Wait! How are you so sure that the file is authentic and has not been tampered with by a hacker? Well, you need to verify the checksum of the file before using it. The checksum is used for verifying the integrity of the data. Most of the time, the checksum of a file is publicly available. For example, the checksum of the Ubuntu ISO image is available on its website. One can use it to verify the integrity of the ISO image.
Linux provides the cksum command to calculate checksum of any file. Given below is an example of this:

$ cksum /bin/ls
2784310137 137888 /bin/ls

The above command shows the checksum of the /bin/ls command.

2. watch
We often want to monitor the progress of a command. To do this, we execute certain commands repeatedly, manually. Wouldn’t it be great if this was done automatically? Yes, Linux is smart; it provides the watch command which does exactly this.

watch runs a command repeatedly, displaying its output and errors. This allows us to watch the program output change over time. By default, the command is run every two seconds and watch will run until interrupted. For example, the command below checks the size of the file with the watch command:

$ watch ls -lh /tmp/test.txt

There is a -n option, using which we can provide an interval in seconds. For instance, the command below uses five seconds as an interval instead of the default two seconds:

$ watch -n 5 ls-lh/tmp/test.txt

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here