When managing a Linux server, monitoring system resources such as CPU usage, memory consumption, disk space, and network activity is crucial for optimal performance and security. Three essential Linux commands that help server administrators track these resources are top, du, and netstat. These commands provide real-time information on how the system is performing, and they are invaluable for troubleshooting and performance tuning.
In this comprehensive guide, we will explore these three key commands in detail, providing both basic and advanced usage examples. By the end of this tutorial, you will understand how to effectively monitor your server’s resources on Ubuntu 20.04, although the concepts can be applied to any Linux distribution.
Table of Contents:
- Prerequisites
- Updating the System
- Using the
topCommand: Process Management and Performance Monitoring - Using the
duCommand: Disk Usage Analysis - Using the
netstatCommand: Network Connections and Statistics - Conclusion
1. Prerequisites
Before diving into the command usage, ensure that you have the following prerequisites:
- Server: A server running Ubuntu 20.04 (the commands also work on other Linux distributions).
- User Permissions: You need either root access or sudo privileges to execute certain commands.
For simplicity, we will assume you have a fresh Ubuntu 20.04 server setup for this guide. If you’re using a different version or Linux distribution, the process remains largely the same.
2. Updating the System
When you first install Ubuntu, it is always a good idea to ensure that your system is up-to-date. To do this, run the following commands:
sudo apt update -y && sudo apt upgrade -yThese commands will update your package index and upgrade the installed packages to the latest versions. After updating, you’re ready to start exploring the resource-monitoring commands.
3. Using the top Command: Process Management and Performance Monitoring
The top command is one of the most commonly used tools for monitoring system performance in real-time. It shows the processes running on the server and provides detailed statistics on CPU usage, memory usage, load averages, and more.
Basic Usage
To run top, simply open your terminal and type:
topYou will see a continuously updated list of processes with various statistics, including CPU usage, memory usage, load average, and process IDs. Here’s an example output:
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 MemExplanation of Output
- Load Average: Shows the system load for the past 1, 5, and 15 minutes.
- CPU Usage: Displays the CPU time percentage for user processes, system processes, idle time, etc.
- Memory Usage: Shows the total, free, and used memory, including buffer and cache memory.
- Swap Usage: Indicates swap space usage.
Advanced Usage
The top command has several options that can be used to refine the output and focus on specific aspects of system performance.
- -i: Ignore idle processes (only show active processes).
- -n [number]: Repeat the command for a specified number of updates.
- -H: Display threads of processes, rather than just the processes themselves.
For example, to run top and update the display five times before exiting, you would use:
top -n 54. Using the du Command: Disk Usage Analysis
The du (disk usage) command is used to check the amount of disk space used by files and directories. It’s especially useful for identifying which directories and files are consuming the most disk space.
Basic Usage
To see the disk usage of all files and directories in the root (/) directory, use:
du -sh /*The -s flag summarizes the total usage of each directory, and the -h flag provides human-readable output (e.g., KB, MB, GB). The output might look something like this:
7 bin
301M boot
12K dev
3.0M etc
2.4G usr
710M varAdvanced Usage
The du command also supports a variety of options that allow you to analyze disk usage more effectively:
- -d [depth]: Limits the depth of the directory tree displayed.
- –max-depth=1: Only shows the total usage of directories at the specified level.
For instance, to check disk usage in the /var directory and its immediate subdirectories, use:
du -d 1 /varThis will list the sizes of the directories under /var without displaying their contents recursively.
5. Using the netstat Command: Network Connections and Statistics
The netstat command is used to display network connections, routing tables, and other network-related information. It’s crucial for monitoring active network connections and diagnosing networking issues.
Basic Usage
To see active network connections and the associated status, simply type:
netstatYou’ll see a list of connections and listening ports, along with information about the protocol (TCP/UDP), local and foreign addresses, and the state of the connection.
Example output:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 240 24625403.test:7022 ctel-78-157-7-19.:55913 ESTABLISHEDAdvanced Usage
netstat offers a variety of options to filter and refine the output:
- -t: Show only TCP connections.
- -u: Show only UDP connections.
- -n: Show numerical addresses (instead of resolving hostnames).
- -l: Display only listening sockets (ports waiting for connections).
- -p: Show the process ID (PID) that owns each connection.
For example, to list all TCP and UDP connections, including listening ports and process information, use:
netstat -tunlpThis command will show active connections and the associated process IDs, which is useful for diagnosing which applications are using which ports.
Conclusion
In this guide, we explored three essential Linux commands for monitoring server resources: top, du, and netstat. Each command serves a unique purpose—top for process and performance monitoring, du for disk usage analysis, and netstat for network connections and statistics.
By mastering these commands, you can ensure your server is running smoothly, optimize resource usage, and quickly diagnose performance or network issues. Additionally, we covered both basic and advanced usages of these commands, helping you get the most out of each tool.
Whether you’re managing a web server, a database server, or any other type of system, these commands will provide you with the insights needed to maintain optimal performance.