When a server is slow, a site is down, or a service refuses to start, you don’t have time to search the internet for commands. You need a small set you know well enough to type without thinking.

This post covers that set: the Linux commands I use most when troubleshooting a real Ubuntu server. Memory, ports, processes, disk, inodes and links, logs, user sessions and basic network checks. Each section shows the command, how to read its output, and the real situation where you’d reach for it.

1. Memory and swap: free

free -h

-h means human-readable (MiB/GiB instead of raw kilobytes). You’ll see something like:

               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       1.2Gi       420Mi        12Mi       2.2Gi       2.3Gi
Swap:          2.0Gi       150Mi       1.9Gi

The column beginners misread is free. Linux deliberately uses spare RAM as disk cache (buff/cache) to speed things up, so “free” is often low on a perfectly healthy server. The number that matters is available: how much memory applications can actually get, because cache is released when needed.

What swap is telling you

Swap is disk space used as overflow memory. When RAM fills up, the kernel moves memory pages that haven’t been used for a while, typically from idle processes, out to swap so active processes can use the RAM.

  • A little swap usage that stays flat is normal.
  • Swap usage that keeps growing, together with low available memory, means memory pressure. The server will feel slow, because disk is far slower than RAM.

To see whether the system is actively swapping right now, watch the si/so (swap in / swap out) columns:

vmstat 2

2. Ports and connections: ss and netstat

sudo ss -tunlp
  • -t TCP, -u UDP
  • -n show port numbers instead of service names
  • -l only listening sockets
  • -p show the process using each socket (needs sudo)

netstat -tunlp gives the same view. It comes from the older net-tools package (sudo apt install net-tools), while ss is installed by default on modern distributions and is faster.

A real port conflict: Apache vs Nginx

Here’s a situation you will hit sooner or later. Nginx is already running on port 80, and you install Apache:

sudo apt install -y apache2
sudo systemctl status apache2

Apache fails to start, and the logs say something like:

(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

Only one process can listen on a given port and address. Find out who owns it:

sudo ss -tlnp | grep ':80 '
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1234,fd=6))

Now you have the process name and PID. The tempting move is sudo kill 1234. Don’t. Nginx is a systemd service. Killing the process skips its clean shutdown, and if the service has a restart policy, systemd simply starts it again and the port is taken again a moment later. Manage services through systemd instead:

sudo systemctl stop nginx       # free port 80
sudo systemctl start apache2
# or, to go back:
sudo systemctl stop apache2
sudo systemctl start nginx
sudo systemctl restart nginx    # after config changes

If you actually want both, the real fix is to move one of them to another port (for example Apache on 8080) or put one behind the other as a reverse proxy.

3. Processes: ps, top and htop

ps aux
ps -ef

Both list every process. They’re two styles of the same command: ps aux (BSD style) shows CPU and memory percentages, and ps -ef (System V style) shows the parent process ID (PPID), which helps when you want to know what started a process.

Reading the TTY column

  • ? means the process has no terminal attached. These are daemons, background services like nginx, sshd, cron or dockerd.
  • pts/0, pts/1 are pseudo-terminals, meaning processes started from someone’s SSH session.

That alone tells you a lot. A strange process attached to a pts you don’t recognise deserves a closer look.

Find what’s eating memory or CPU

ps aux --sort=-%mem | head    # top memory users
ps aux --sort=-%cpu | head    # top CPU users

The - before the field means descending order, and head keeps the first ten lines. This is usually the first thing I run on a slow server.

Live view: top and htop

top
sudo apt install -y htop && htop

In top, press M to sort by memory, P for CPU, 1 to see each CPU core, and q to quit. Also look at load average in the first line. If it stays above the number of CPU cores, processes are queuing for CPU. htop shows the same information with colours, bars and mouse support, and it’s easier to read.

4. Disk space: df, du and lsblk

df -h

Shows each mounted filesystem, its size, used space and Use%. If / is at 100%, expect odd failures: databases stop writing, logins fail, and services won’t start.

lsblk

Shows the block devices (disks and partitions) and where they’re mounted. It’s useful on EC2 when you attach a new EBS volume and need to find it (often nvme1n1) before formatting and mounting it.

Find what’s using the space

df tells you a disk is full. du tells you what filled it:

sudo du -sh /var/* | sort -h
sudo du -sh /var/log/* | sort -h | tail

Logs are the usual culprit. To reclaim space safely:

sudo journalctl --vacuum-size=200M     # shrink the systemd journal
sudo truncate -s 0 /var/log/nginx/access.log   # empty a huge log without deleting it

Avoid rm on a log file that a running process still has open. The space isn’t freed until the process restarts, and the file disappears from view, which is confusing. For the long term, make sure logrotate is set up.

5. Inodes, file metadata and links

stat and inode numbers

stat notes.txt
ls -il

Every file on a Linux filesystem has an inode: a record holding its permissions, owner, size, timestamps and the location of its data. The file name is just a directory entry pointing to that inode. stat shows the access, modify and change times (and the birth time on filesystems that support it), and ls -i shows the inode number.

A filesystem can also run out of inodes before it runs out of space, for example when there are millions of tiny cache files. Check it with df -i.

Soft links (symbolic links)

ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite

A soft link is a shortcut: a small file that contains a path to another file. This is exactly how Nginx on Ubuntu enables sites. The real config lives in sites-available, and a symlink in sites-enabled switches it on. Remove the link and the site is disabled, while the config stays safe.

Because a soft link only stores a path, if you rename or delete the original, the link breaks. ls -l shows it in red, and reading it gives “No such file or directory”.

Hard links

ln original.txt hardlink.txt
ls -il original.txt hardlink.txt

Both names show the same inode number. A hard link is not a shortcut. It’s a second name for the same data. Delete original.txt and hardlink.txt still works, because the data is only removed when the last name pointing to the inode is gone. Hard links can’t point to directories or cross to a different filesystem; soft links can do both.

6. Reading logs: head and tail

head -n 20 /var/log/syslog
tail -n 50 /var/log/nginx/error.log
tail -f /var/log/nginx/access.log

tail -f (“follow”) streams new lines as they’re written. It’s the quickest way to see what’s happening right now. Open the site in your browser and watch your own requests arrive. Add a filter to cut the noise:

tail -f /var/log/nginx/access.log | grep " 500 "

For systemd services, the equivalent is journalctl -u nginx -f.

7. Who is logged in: who and w

who
w

These show every active session: the user, their terminal (pts/0, pts/1), the IP they connected from and when. w also shows what each one is running.

Old SSH sessions from a dropped Wi-Fi connection often hang around. If you see a session you don’t recognise, or a stale one of your own, end it by its terminal:

sudo pkill -KILL -t pts/1

If it’s genuinely someone you don’t know, treat it as a security incident: check /var/log/auth.log, rotate keys and passwords, and review who has access. My post on Linux user setup and non-interactive shells covers how to lock down accounts in the first place.

8. Connectivity checks: ping, nslookup, telnet

ping

ping -c 4 google.com

It checks whether a host replies to ICMP. But no reply doesn’t mean the server is down. Many servers and firewalls block ICMP, and EC2 security groups don’t allow it unless you add a rule. A host can fail ping and still serve websites perfectly well.

nslookup

nslookup example.com
nslookup -type=TXT example.com
nslookup -type=CNAME www.example.com

Use it to check what a domain resolves to, verify TXT records (for SSL or domain verification), and confirm CNAMEs. If you’re new to DNS, my post on DNS, Route 53, Let’s Encrypt and Nginx explains how these records fit together.

telnet to test a port

telnet 10.0.1.25 3306

Where ping tests the host, this tests a specific port, which is usually what you actually care about. “Connected” means the port is open and reachable. A timeout usually means a firewall or security group is blocking it, and “Connection refused” means you reached the host but nothing is listening on that port. If telnet isn’t installed, nc -zv 10.0.1.25 3306 does the same job.

A quick troubleshooting order

When a server misbehaves, this order covers most problems in a few minutes:

  1. df -h: is a disk full?
  2. free -h: is memory or swap under pressure?
  3. top or ps aux --sort=-%cpu | head: is a process hogging CPU?
  4. systemctl status <service> and journalctl -u <service>: is the service running, and if not, why?
  5. ss -tunlp: is it listening on the expected port?
  6. tail -f the logs while you reproduce the problem.
  7. telnet/nc and nslookup from outside: is it a network or DNS issue?

Wrapping up

None of these commands is complicated on its own. The skill is knowing which one to run first, and how to read what it tells you. Practise them on a small cloud server, break things on purpose (fill the disk, start two web servers on port 80), and fix them. That’s how they stick. If you need the basics first, start with Linux Zero to Hero.

Watch every command demonstrated live on my YouTube channel, Madhukar Reddy (@awsandevops).

Madhukar Reddy

DevOps engineer focused on AWS, Docker, Kubernetes, cloud infrastructure, and cyber security. Shares practical cloud and DevOps content based on hands-on deployments, infrastructure troubleshooting, and real-world projects.

$ This blog is currently running on AWS EC2 using Docker-based deployment.

Leave a response