متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 81Security · Linux

How to see who logged in and when

"Who was on this server last night?", "Did that leaver still log in?", "Are bots guessing passwords?" — Linux keeps several login records: who is logged in now, the login history, failed attempts, and the SSH and sudo lines in the journal. This how-to reads all of them and shows what to look for in an incident.

  • 5 الدروس
  • 12 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

81.1

The quick answer

w
last -n 20
lastb -n 20
journalctl -u sshd --since today | grep -E 'Accepted|Failed'
journalctl _COMM=sudo --since today

On Ubuntu the SSH unit is ssh (journalctl -u ssh).

81.2

Step by step

  1. 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
  1. Login history (from /var/log/wtmp) — user, source IP, start, end, duration:
last -n 20 -i
last alice
last reboot | head -5

still logged in = the session is open now. On the newest distributions last may read the new wtmpdb database — the output looks the same.

  1. Failed logins (from /var/log/btmp, root only):
lastb -n 20 -i
lastb -i | awk '{print $3}' | sort | uniq -c | sort -rn | head

The second line counts failures per source IP — bots show up at the top.

  1. 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.

  1. 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
  1. 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.

81.3

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.

81.4

Common problems

ProblemCauseFix
last shows nothing oldwtmp was rotated.Read rotated files: last -f /var/log/wtmp.1.
lastb: /var/log/btmp: Permission deniedRoot only.Use sudo.
No SSH lines in the journalWrong unit name (ssh on Ubuntu) or the journal is not persistent.journalctl -u ssh; make the journal persistent (Storage=persistent).
Thousands of failed loginsNormal 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 keyA 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 rootLogs only on the server.Ship logs to a central system in near real time.

81.5

Go further

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.

كل الأوامر في هذا الفصل