history 30 of 36
Check Running Processes, Login History, Services and Open Ports
Quick commands to see what a Linux server is doing: ps aux for processes, last for login history and netstat or ss for listening ports and services.
Written
From my Linux study notes, 2017. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.
Three commands I used to check what was happening on a server.
Before you start
- Some output (for example the process name for each port in
netstat -p) is only shown in full when you run as root or withsudo.
My original notes
-
To check all running processes:
Terminal window ps auxLists every process on the system.
ashows processes of all users,ushows a user-oriented format (owner, CPU %, memory %, start time, command), andxincludes processes not attached to a terminal, such as services. -
To check logs:
Terminal window lastClarification:
lastdoes not show general system logs. It shows the login history: who logged in, from where, and when, plus reboots. It reads the/var/log/wtmpfile. For system logs, see “Notes for today” below and my post on common Linux log files. -
To check list of ports:
Terminal window netstat -tupanShows network connections and listening ports:
-tTCP-uUDP-pthe process (PID and program name) using each port-aall sockets, both listening and connected-nnumbers instead of names (faster, and shows real port numbers)
Useful variations
Find one process by name, for example Apache:
ps aux | grep httpdShow only listening ports (services waiting for connections):
netstat -tulpnShow the most recent 10 logins only:
last -n 10Show failed login attempts (needs root; reads /var/log/btmp):
sudo lastbNotes for today
-
netstatis old. It comes from thenet-toolspackage, which is not installed by default on many current distributions. The modern replacement isss, which accepts the same options used here:Terminal window sudo ss -tupansudo ss -tulpn -
Live view of processes:
topshows processes updating in real time, sorted by CPU use (pressqto quit).htopis a friendlier version but usually has to be installed. -
Checking services: on older CentOS (5 and 6),
service --status-alllists services and their status. On systemd systems (CentOS 7 and later, Rocky Linux, AlmaLinux, current Debian and Ubuntu):Terminal window systemctl list-units --type=servicesystemctl status sshdOn Debian and Ubuntu the SSH service is called
sshrather thansshd. -
Reading logs on systemd systems:
journalctlreads the systemd journal. For example,journalctl -u sshdshows logs for one service andjournalctl -ffollows new messages as they arrive. -
Currently logged-in users:
whoorwshow who is logged in right now, whereaslastshows history.