هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
w
last -n 20
lastb -n 20
journalctl -u sshd --since today | grep -E 'Accepted|Failed'
journalctl _COMM=sudo --since todayOn Ubuntu the SSH unit is ssh (journalctl -u ssh).
Step by step
- Who is logged in now and what are they doing:
w
14:05:11 up 12 days, 3:10, 2 users, load average: 0.20, 0.15, 0.10
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
alice pts/0 203.0.113.10 13:58 0.00s 0.04s 0.00s w
deploy pts/1 10.0.9.21 14:01 2:10 0.02s 0.02s -bash- Login history (from
/var/log/wtmp) — user, source IP, start, end, duration:
last -n 20 -i
last alice
last reboot | head -5still logged in = the session is open now. On the newest distributions last may read the new wtmpdb database — the output looks the same.
- Failed logins (from
/var/log/btmp, root only):
The second line counts failures per source IP — bots show up at the top.
- SSH details from the journal — key or password, which key, from where:
journalctl -u sshd --since "2026-11-18 20:00" --until "2026-11-19 08:00" | grep -E 'Accepted|Failed|Invalid user'
Nov 19 02:13:44 web1 sshd[4711]: Accepted publickey for deploy from 10.0.9.21 port 51544 ssh2: ED25519 SHA256:abc…The SHA256:… fingerprint tells you which key was used — compare with ssh-keygen -lf authorized_keys.
- What did they do with sudo?
journalctl _COMM=sudo --since today
Nov 19 14:02:07 web1 sudo[5120]: alice : TTY=pts/0 ; PWD=/home/alice ; USER=root ; COMMAND=/usr/bin/systemctl restart nginx- Last login per user (find old accounts that should be disabled):
lastlog | grep -v 'Never logged in'(On new systems the tool can be lastlog2.) For audits use ausearch -m USER_LOGIN -ts today and send logs to a central system (Loki, Elasticsearch, a SIEM like Wazuh) so an attacker can not delete them on the server.
Check that it worked
You can answer: who logged in (user), from where (IP), when, with which key, and what they ran with sudo — for the time you care about.
Common problems
| Problem | Cause | Fix |
|---|---|---|
last shows nothing old | wtmp was rotated. | Read rotated files: last -f /var/log/wtmp.1. |
lastb: /var/log/btmp: Permission denied | Root only. | Use sudo. |
| No SSH lines in the journal | Wrong unit name (ssh on Ubuntu) or the journal is not persistent. | journalctl -u ssh; make the journal persistent (Storage=persistent). |
| Thousands of failed logins | Normal internet noise on an open port 22. | Key-only SSH, Fail2ban, limit port 22 to known IPs. |
| A login from an unknown IP with a valid key | A stolen key — or a forgotten automation. | Find the key by fingerprint, remove it from authorized_keys, investigate. |
| Logs can be deleted by an attacker with root | Logs only on the server. | Ship logs to a central system in near real time. |
Go further
- Command pages: who, w, journalctl, ausearch.
- Next how-tos: Block brute-force logins with Fail2ban, Harden the SSH server, Read the logs of a service.
- Learn it: RHCSA — journalctl, RHCSA — Persistent journal, RH134 — journalctl.
- Central logs and SIEM: Wazuh, Loki. At work: auditd.
1Which command shows failed login attempts, and who may run it?
lastb — only root (it reads /var/log/btmp).
2How do you find out which SSH key was used for a login?
The journal's Accepted publickey … SHA256:… line shows the key fingerprint; compare it with ssh-keygen -lf authorized_keys.