breakfixlearn.com

SEC-04Cybersecurity

Linux security commands

Use this when: you check a Linux box for who has access, what is running and listening, what changed, and how to lock it down.

SEC-04 Linux security commands cheat sheet. Every table on it is repeated as text below.
Tap the sheet for the full-size image. The text version below is screen-reader friendly.

Text version

Everything on the sheet, as plain HTML: handy for screen readers, copy-paste and search.

Read a permission line

Example: -rwxr-x--- alice ops script.sh. Add r = 4, w = 2 and x = 1 for each group: owner rwx = 7, group r-x = 5, others --- = 0, so chmod 750 script.sh sets exactly that. The first character is the type: - file, d directory, l link. chmod u+x adds execute for the owner and chown alice:ops sets owner and group.

ModeUse for
644Normal files
755Directories, scripts
600Private keys, secrets
700~/.ssh
4000SUID: runs as the file owner

Who has access?

CommandShows
id, whoamiMy user and groups
sudo -lWhat I can run with sudo
w, lastWho is on, recent logins
sudo lastbFailed logins
awk -F: '$3==0' /etc/passwdAccounts with UID 0 (root)

What is running and listening?

CommandShows
ss -tulpnListening ports and their processes
sudo lsof -i :22Who uses port 22
ps aux --sort=-%cpu | headTop CPU processes
systemctl --state=runningRunning units

What changed? Find risky files

CommandFinds
find / -xdev -perm -4000 -type fSUID files
find / -xdev -perm -0002 -type fWorld-writable files
find /etc -mtime -1Files changed in the last 24 hours
sha256sum file.isoA hash to compare with the publisher's

Read the logs

CommandShows
journalctl -u ssh --since todayToday's SSH log
journalctl -p err -bErrors since this boot
grep "Failed password" /var/log/auth.logSSH password guessing

Ubuntu and Debian shown. On RHEL use /var/log/secure and the unit sshd.

Lock it down

  • Keys first, from your PC: ssh-keygen -t ed25519, then ssh-copy-id admin@192.0.2.10
  • Firewall (Ubuntu ufw): sudo ufw default deny incoming, sudo ufw limit 22/tcp (SSH, rate-limited), sudo ufw enable, sudo ufw status verbose
  • In /etc/ssh/sshd_config: PermitRootLogin no and PasswordAuthentication no
  • Test and apply: sudo sshd -t, then sudo systemctl reload ssh
  • Patches: sudo apt update and sudo apt upgrade (RHEL: sudo dnf upgrade)

Before you press Enter

Danger. chmod -R 777 and rm -rf on the wrong path can't be undone.

Caution. Allow SSH before ufw enable, or a remote session locks you out.

Caution. Set up SSH keys and keep a second session open before you turn off passwords.

Authorized use only. Run these only on systems you administer.

Remember

  • Use sudo per command, not a root shell
  • Run sshd -t before every reload
  • Patch on a schedule, not when you remember

Sources

Go deeper on the blog