Using Top, Du, and Netstat as Tools to Monitor Server Resources

using top du and netstat to monitor server resources

In this tutorial, we will explain the usage of the top, du, and netstat as tools to monitor server resources.

The du is a shortcut of disk usage, the top is used for showing the running processes, and netstat is for printing network connections, routing tables, interface statistics, masquerade connections, etc. We will explain these commands with real examples, and we will use Ubuntu 20.04 as OS in this tutorial, but you can choose any Linux distro per your choice. Let’s get started!

Prerequisites

  • A server with Ubuntu 20.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

After a fresh installation of Ubuntu 20.04, it is recommended for the packages be updated to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Once the system is updated, we are ready to show you the top, du, and netstat commands with real examples in Linux.

Top command

The top command in Linux is for displaying the processes. It can be executed with or without options per our needs. Let’s execute the top command without options:

top - 11:24:59 up 7 days, 11:27,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 109 total,   1 running, 108 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :   3931.4 total,   2473.6 free,    139.8 used,   1318.0 buff/cache
MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   3475.6 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
     10 root      20   0       0      0      0 I   0.3   0.0  19:20.53 rcu_sched
     11 root      rt   0       0      0      0 S   0.3   0.0   0:04.84 migration/0
      1 root      20   0  169784  11300   8340 S   0.0   0.3   0:48.82 systemd
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.26 kthreadd
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd
      8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq
      9 root      20   0       0      0      0 S   0.0   0.0   0:00.43 ksoftirqd/0

As you can see, there is information about the uptime of the system, system load, percent usage of CPU cores, available memory, and the processes with all this information. In this scenario, only one process is active, and 108 are sleeping. That means that this process is using some CPU and RAM resources, and the other 108 are using nothing.

The top command can be executed with options such as -i for idle processes, -H for information about threads, -n number of iterations, and many more that can be seen with detailed explanation by executing the man top command.

TOP(1)                                                                     User Commands                                                                     TOP(1)

NAME
       top - display Linux processes

SYNOPSIS
       top -hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]

       The traditional switches `-' and whitespace are optional.

DESCRIPTION
       The  top program provides a dynamic real-time view of a running system.  It can display system summary information as well as a list of processes or threads
       currently being managed by the Linux kernel.  The types of system summary information shown and the types, order and size of information displayed for  pro‐
       cesses are all user configurable and that configuration can be made persistent across restarts.

       The program provides a limited interactive interface for process manipulation as well as a much more extensive interface for personal configuration  --  en‐
       compassing every aspect of its operation.  And while top is referred to throughout this document, you are free to name the program anything you wish.   That
       new name, possibly an alias, will then be reflected on top's display and used when reading and writing a configuration file.

Du command

The du command is used for disk usage of files and directories. It is very often used by the system administrator when disk usage needs to be checked. The same as the top command du command can be executed with or without arguments.

If we execute the du command in the root directory without arguments, we will get output like this:

root@host:~# du
4       ./.local/share/nano
8       ./.local/share
12      ./.local
4       ./.cache
4       ./.config/procps
8       ./.config
8       ./.ssh
56      .

As you can see, there is nothing explained, and there is no info about the size, whether it is in kilobytes, megabytes, or something else. Let’s execute the du command with some options in the / directory on your server:

cd /

du -bsh *

You should receive output like this:

7       bin
301M    boot
12K     dev
3.0M    etc
4.0K    home
7       lib
9       lib32
9       lib64
10      libx32
16K     lost+found
4.0K    media
4.0K    mnt
4.0K    opt
128T    proc
38K     root
40M     run
8       sbin
4.6K    snap
4.0K    srv
258M    sys
56K     tmp
2.4G    usr
710M    var

The output is clear, and the directories and file sizes are displayed on the left.

The command du -bsh * means that we need the file space usage of all (*) directory and files in the current directory in bytes (-b), summarized (-s) in human-readable format (-h)

More about the du command can be viewed with the command man du executed on your command line.

root@host:~# man du

       -d, --max-depth=N
              print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument;  --max-depth=0 is the same  as
              --summarize

       --files0-from=F
              summarize disk usage of the NUL-terminated file names specified in file F; if F is -, then read names from standard input

       -H     equivalent to --dereference-args (-D)

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

       --inodes
              list inode usage information instead of block usage

       -k     like --block-size=1K

       -L, --dereference
              dereference all symbolic links

Netstat command

The netstat command is used for printing network connections, routing tables and etc. As with previous commands, the netstat command can be executed with or without options.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
netstat

Executing the command netstat will give you similar output like this:

root@host:~# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0    240 24625403.test:7022 ctel-78-157-7-19.:55913 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    16576    /run/systemd/journal/syslog
unix  10     [ ]         DGRAM                    16586    /run/systemd/journal/dev-log
unix  2      [ ]         DGRAM                    24702    /run/user/0/systemd/notify
unix  7      [ ]         DGRAM                    16590    /run/systemd/journal/socket
unix  3      [ ]         DGRAM                    15146    /run/systemd/notify
unix  3      [ ]         STREAM     CONNECTED     20481
unix  3      [ ]         STREAM     CONNECTED     20501    /run/dbus/system_bus_socket

Let’s execute the netstat command with some parameters, which will be explained later:

netstat -tunlp

You will receive output similar like this if you have installed MySQL, Dovecot, Netdata and similar applications.

root@host:~# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:8125          0.0.0.0:*               LISTEN      88780/netdata
tcp        0      0 127.0.0.1:19999         0.0.0.0:*               LISTEN      88780/netdata
tcp        0      0 0.0.0.0:993             0.0.0.0:*               LISTEN      88311/dovecot
tcp        0      0 0.0.0.0:995             0.0.0.0:*               LISTEN      88311/dovecot
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      83830/mysqld
tcp        0      0 0.0.0.0:110             0.0.0.0:*               LISTEN      88311/dovecot
tcp        0      0 0.0.0.0:7022            0.0.0.0:*               LISTEN      657/sshd: /usr/sbin
tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN      88311/dovecot
tcp6       0      0 ::1:8125                :::*                    LISTEN      88780/netdata
tcp6       0      0 :::993                  :::*                    LISTEN      88311/dovecot
tcp6       0      0 :::995                  :::*                    LISTEN      88311/dovecot
tcp6       0      0 :::110                  :::*                    LISTEN      88311/dovecot
tcp6       0      0 :::7022                 :::*                    LISTEN      657/sshd: /usr/sbin
tcp6       0      0 :::143                  :::*                    LISTEN      88311/dovecot
tcp6       0      0 :::80                   :::*                    LISTEN      649/apache2
udp        0      0 127.0.0.1:8125          0.0.0.0:*                           88780/netdata
udp6       0      0 ::1:8125                :::*                                88780/netdata

Now, the output is more clear with protocols, addresses, states, and ports. Now let’s explain the parameters:

-t - is for tcp

-u - is for udp

-n - is for numeric

-l - is for listening sockets

-p - is for the process id and the name of the running program

More about the netstat command can be viewed with the command man netstat executed on your command line.

NETSTAT(8)                                                      Linux System Administrator's Manual                                                      NETSTAT(8)

NAME
       netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

SYNOPSIS
       netstat  [address_family_options]  [--tcp|-t] [--udp|-u] [--udplite|-U] [--sctp|-S] [--raw|-w] [--l2cap|-2] [--rfcomm|-f] [--listening|-l] [--all|-a] [--nu‐
       meric|-n] [--numeric-hosts] [--numeric-ports]  [--numeric-users]  [--symbolic|-N]  [--extend|-e[--extend|-e]]  [--timers|-o]  [--program|-p]  [--verbose|-v]
       [--continuous|-c] [--wide|-W]

       netstat {--route|-r} [address_family_options] [--extend|-e[--extend|-e]] [--verbose|-v] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users]
       [--continuous|-c]

       netstat {--interfaces|-i} [--all|-a] [--extend|-e[--extend|-e]] [--verbose|-v] [--program|-p] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-
       users] [--continuous|-c]

       netstat {--groups|-g} [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c]

       netstat {--masquerade|-M} [--extend|-e] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c]

       netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--udplite|-U] [--sctp|-S] [--raw|-w]

       netstat {--version|-V}

       netstat {--help|-h}

That’s it. We gave you some instructions on how to use these tools to monitor your server resources. If you have difficulties using these commands du, netstat, and top, feel free to contact our technical support. We are available 24/7

If you liked this about using top, du, and netstat as tools to monitor server resources, please share it with your friends on social networks or simply leave a reply below.

Leave a Comment