Introduction

Every computer system benefits from proper administration and monitoring. Keeping an eye on how your system is running will help you discover issues and resolve them quickly.

There are plenty of command line utilities created for this purpose. This guide will introduce you to some of the most helpful applications to have in your toolbox

Prerequisites

To follow along with this guide, you will need access to a computer running a Linux-based operating system. This can either be a virtual private server which you’ve connected to with SSH or your local machine. Note that this tutorial was validated using a Linux server running Ubuntu 20.04, but the examples given should work on a computer running any version of any Linux distribution.

If you plan to use a remote server to follow this guide, we encourage you to first complete our Initial Server Setup guide. Doing so will set you up with a secure server environment — including a non-root user with sudo privileges and a firewall configured with UFW — which you can use to build your Linux skills

Step 1 – How To View Running Processes in Linux

You can see all of the processes running on your server by using the top command:

$ top

Output

top - 15:14:40 up 46 min, 1 user, load average: 0.00, 0.01, 0.05

Tasks: 56 total, 1 running, 55 sleeping, 0 stopped, 0 zombie

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

Mem: 1019600k total, 316576k used, 703024k free, 7652k buffers

Swap: 0k total, 0k used, 0k free, 258976k cached

The remainder of the displayed output shows the running processes and their usage statistics. By default, top automatically sorts these by CPU usage, so you can see the busiest processes first. top will continue running in your shell until you stop it using the standard key combination of Ctrl+C to exit a running process. This sends a kill signal, instructing the process to stop gracefully if it is able to

An improved version of top, called htop, is available in most package repositories. On Ubuntu 20.04, you can install it with apt:

$ htop

Output

Mem[||||||||||| 49/995MB] Load average: 0.00 0.03 0.05

CPU[ 0.0%] Tasks: 21, 3 thr; 1 running

Swp[ 0/0MB] Uptime: 00:58:11

PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command

1259 root 20 0 25660 1880 1368 R 0.0 0.2 0:00.06 htop

Here are some keyboard shortcuts that will help you use htop more effectively:

  • M: Sort processes by memory usage
  • P: Sort processes by processor usage
  • ?: Access help
  • k: Kill current/tagged process
  • F2: Configure htop. You can choose display options here.
  • /:: Search processes
  • There are many other options that you can access through help or setup. These should be your first stops in exploring htop’s functionality. In the next step, you’ll learn how to monitor your network bandwidth.

    Step 2 – How To Monitor Your Network Bandwidth

    If your network connection seems overutilized and you are unsure which application is the culprit, a program called nethogs is a good choice for finding out.

    On Ubuntu, you can install nethogs with the following command:

    $ sudo apt install nethogs

    After that, the nethogs command will be available:

    $ nethogs

    Output

    NetHogs version 0.8.0

    PID USER PROGRAM DEV SENT RECEIVED

    3379 root /usr/sbin/sshd eth0 0.485 0.182 KB/sec

    820 root sshd: root@pts/0 eth0 0.427 0.052 KB/sec

    ? root unknown TCP 0.000 0.000 KB/sec

    TOTAL 0.912 0.233 KB/sec

    nethogs associates each application with its network traffic.

    There are only a few commands that you can use to control nethogs:

  • M: Change displays between “kb/s”, “kb”, “b”, and “mb”.
  • R: Sort by traffic received.
  • S: Sort by traffic sent
  • Q: quit
  • On Ubuntu, you can install iptraf-ng with the following command:

    $ sudo apt install iptraf-ng

    iptraf-ng needs to be run with root privileges, so you should precede it with sudo:

    $ sudo iptraf-ng

    You’ll be presented with a menu that uses a popular command line interface framework called ncurses.

    iptraf-ng main interface

    For example, to get an overview of all network traffic, you can select the first menu and then “All interfaces”. It will give you a screen that looks like this:

    iptraf-ng Traffic View

    If you would like to have those IP addresses resolved into domains, you can enable reverse DNS lookup by exiting the traffic screen, selecting Configure and then toggling on Reverse DNS lookups.

    With both of these options enabled, the display may look like this:

    iptraf-ng Named Traffic View

    netstat is installed by default on most modern systems, but you can install it yourself by downloading it from your server’s default package repositories. On most Linux systems, including Ubuntu, the package containing netstat is net-tools:

    $ sudo apt install net-tools

    By default, the netstat command on its own prints a list of open sockets:

    $ netstat

    If you add an -a option, it will list all ports, listening and non-listening:

    $ netstat -a

    If you’d like to filter to see only TCP or UDP connections, use the -t or -u flags respectively:

    $ netstat -at

    See statistics by passing the “-s” flag:

    $ netstat -s

    In the next step, you’ll learn some useful ways of monitoring your disk usage.

    Step 3 – How To Monitor Your Memory Usage

    You can check the current memory usage on your system by using the free command

    When used without options, the output looks like this:

    $ free

    Output

    total used free shared buff/cache available

    Mem: 1004896 390988 123484 3124 490424 313744

    Swap: 0 0 0

    To display in a more readable format, you can pass the -m option to display the output in megabytes:

    $ free -m

    Output

    total used free shared buff/cache available

    Mem: 981 382 120 3 478 306

    Swap: 0 0 0

    The Mem line includes the memory used for buffering and caching, which is freed up as soon as needed for other purposes. Swap is memory that has been written to a swapfile on disk in order to conserve active memory.

    Finally, the vmstat command can output various information about your system, including memory, swap, disk io, and cpu information.

    You can use the command to get another view into memory usage:

    $ vmstat

    Output

    procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----

    r b swpd free buff cache si so bi bo in cs us sy id wa

    1 0 0 99340 123712 248296 0 0 0 1 9 3 0 0 100 0

    You can see this in megabytes by specifying units with the -S flag:

    $ vmstat -S M

    Output

    procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----

    r b swpd free buff cache si so bi bo in cs us sy id wa

    1 0 0 99340 123712 248296 0 0 0 1 9 3 0 0 100 0

    To get some general statistics about memory usage, type:

    $ vmstat -s -S M

    Output

    495 M total memory

    398 M used memory

    252 M active memory

    119 M inactive memory

    96 M free memory

    120 M buffer memory

    242 M swap cache

    0 M total swap

    0 M used swap

    0 M free swap

    ....

    This will give you details about what kind of information is stored in the cache.

    Conclusion

    Using these tools, you should begin to be able to monitor your server from the command line. There are many other monitoring utilities that are used for different purposes, but these are a good starting point.