Menu
Setup host security
“How shadow passwords work, how to switch off services you don't use, and what TCP wrappers do.”
A fresh Linux install often runs more than you need: a printing service, a mail server, maybe an old remote login tool. Every one of them is a possible way in. Host security means two simple habits: keep secrets (like password hashes) where only root can read them, and turn off everything that doesn’t need to run.
This byte covers how shadow passwords protect your hashes, how to stop and disable services on systemd and on old SysV systems (including the xinetd “super server” and systemd sockets), and how TCP wrappers decide who may connect.
Shadow passwords#
/etc/passwd lists every account. Many programs read it, for example ls -l to show alice instead of 1000, so it must be readable by everyone:
grep alice /etc/passwdalice:x:1000:1000:Alice Example:/home/alice:/bin/bash
The seven fields are: name, password, UID, GID, comment (GECOS), home directory, login shell. The x in the password field means “the real password is in /etc/shadow”.
Long ago the password hash sat right there in /etc/passwd. Anyone could copy all the hashes and guess passwords offline, as fast as their computer allowed. Shadow passwords fixed that by moving the hashes into a separate file that only root can read:
ls -l /etc/passwd /etc/shadow-rw-r--r-- 1 root root 1843 Nov 28 10:11 /etc/passwd
-rw-r----- 1 root shadow 1022 Nov 28 10:11 /etc/shadow
That’s Debian/Ubuntu: readable by root and the shadow group. On RHEL/Fedora the mode is ---------- (000): only root, which ignores permissions, can read it.
sudo grep alice /etc/shadowalice:$y$j9T$Fq0eD2sB6kq3/x1Ylm7cR.$4xZ0p8bJ1mQ9uE2wR7tVh3aKcN5dL6sG0yF1iH8oP2.:20785:0:99999:7:::
| Field | Example | Meaning |
|---|---|---|
| 1 | alice | User name |
| 2 | $y$j9T$Fq0…$4xZ… | Hashed password |
| 3 | 20785 | Last change, in days since 1 Jan 1970 (here: 28 Nov 2026) |
| 4 | 0 | Minimum days between changes |
| 5 | 99999 | Maximum days the password is valid |
| 6 | 7 | Warning days before expiry |
| 7 | (empty) | Inactive days after expiry before the account locks |
| 8 | (empty) | Account expiry date, in days since 1970 |
| 9 | (empty) | Reserved |
Fields 3 to 8 are exactly what chage changes.
How the hash works#
Linux never stores your password. It stores a hash: the result of a one-way function that can’t be turned back into the password. The hash field has the form $id$salt$hash:
| Prefix | Algorithm |
|---|---|
$1$ | MD5 (old, weak) |
$5$ | SHA-256 |
$6$ | SHA-512 (RHEL 9 default) |
$y$ | yescrypt (Debian 11+, Ubuntu 22.04+, Fedora 35+ default) |
The salt is random text added before hashing, so two users with the same password still get different hashes. When you log in, the system hashes what you typed with the same salt and compares the results.
A password field that starts with ! or * can’t match any password: the account is locked or has no password login at all. That’s what passwd -l does, and why system accounts like daemon show *.
/etc/group is public and /etc/gshadow holds group passwords. pwconv and grpconv move hashes into the shadow files, pwunconv and grpunconv move them back. You’ll almost never need them.Block logins with /etc/nologin#
During maintenance you may want to stop users from logging in. Create /etc/nologin:
echo "Maintenance until 14:00. Back soon." | sudo tee /etc/nologinWhile the file exists, the PAM module pam_nologin refuses every login except root’s, and shows the file’s text as the reason. Delete it to open up again:
sudo rm /etc/nologinsystemd creates /run/nologin by itself a few minutes before a scheduled shutdown, with the same effect.
Don’t mix this up with /usr/sbin/nologin (or /sbin/nologin): that’s a shell you give to a single account, like usermod -s /usr/sbin/nologin ftpuser, so that one account can’t get a shell.
Turn off network services you don’t use#
First see what’s listening:
sudo ss -tulpnThen, for each service, ask: do I need it? If not, stop it and keep it from starting at boot. Removing the package is even better.
With systemd (modern)#
systemctl list-units --type=service --state=running
sudo systemctl disable --now cups.service # stop now and at boot
sudo systemctl mask cups.service # make it impossible to start
sudo apt remove cups # Debian/Ubuntu
sudo dnf remove cups # RHEL/Fedoradisable --now stops the service and removes it from boot. mask links the unit to /dev/null, so even another service can’t start it. unmask undoes that.
systemd sockets: on-demand services#
systemd can listen on a port or a socket file for a service and only start the service when a connection comes in. That’s socket activation, set up with a .socket unit (documented in man systemd.socket):
systemctl list-socketsLISTEN UNIT ACTIVATES
/run/cups/cups.sock cups.socket cups.service
/run/dbus/system_bus_socket dbus.socket dbus.service
[::]:22 ssh.socket ssh.service
1[Unit]
2Description=CUPS Scheduler
3PartOf=cups.service
4
5[Socket]
6ListenStream=/run/cups/cups.sock
7
8[Install]
9WantedBy=sockets.targetThe [Socket] section says where to listen: ListenStream= for TCP or a stream socket, ListenDatagram= for UDP. Here’s the trap: if you only disable cups.service, the socket still listens and starts the service again on the next connection. Disable both:
sudo systemctl disable --now cups.socket cups.serviceUbuntu 24.04 starts SSH this way too (ssh.socket).
With SysV init (legacy)#
Older systems start services with shell scripts in /etc/init.d/:
sudo /etc/init.d/cups stop # stop it now
sudo service cups stop # same, through the service wrapper
sudo update-rc.d cups disable # Debian: don't start at boot
sudo chkconfig cups off # old RHEL/CentOS: don't start at bootThe SysV init process itself reads /etc/inittab. It sets the default runlevel and which programs init starts and restarts, like the login prompts on the text consoles:
# default runlevel
id:3:initdefault:
# what to do on Ctrl+Alt+Del: comment it out to disable reboots from the keyboard
ca:12345:ctrlaltdel:/sbin/shutdown -t3 -r now
# login prompts on tty1 to tty6: fewer lines, fewer consoles
1:2345:respawn:/sbin/getty 38400 tty1
2:2345:respawn:/sbin/getty 38400 tty2Each line is id:runlevels:action:process. To harden it you’d remove consoles you don’t need and disable the Ctrl+Alt+Del reboot, then reload it with sudo telinit q. systemd systems have no inittab: there you’d run sudo systemctl mask ctrl-alt-del.target.
inetd and xinetd: the super servers#
Before socket activation, Unix used a super server: one daemon listening on many ports that starts the real service (telnet, ftp, rsync…) only when someone connects. The classic one was inetd, replaced by xinetd (extended inetd). It’s rare today (RHEL 9 dropped it) but still on the exam.
/etc/xinetd.conf holds the defaults and includes one file per service from /etc/xinetd.d/:
defaults
{
instances = 60
log_type = SYSLOG authpriv
log_on_success = HOST PID
log_on_failure = HOST
cps = 25 30
}
includedir /etc/xinetd.dservice telnet
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
only_from = 192.168.1.0/24
log_on_failure += USERID
}To turn a service off, set disable = yes in its file and restart xinetd (sudo systemctl restart xinetd, or sudo /etc/init.d/xinetd restart on SysV). only_from and no_access limit which clients may connect. If no service is enabled any more, stop and disable xinetd itself.
Warning
telnet, rsh and plain ftp send passwords as readable text over the network. If you find them running, turn them off and use SSH instead.
TCP wrappers#
TCP wrappers are an old access control layer for network services. Before a wrapped service talks to a client, the libwrap library checks the client’s address against two files:
/etc/hosts.allowis checked first. If a rule matches, access is granted.- Otherwise
/etc/hosts.denyis checked. If a rule matches, access is denied. - If nothing matches in either file, access is granted.
The first match wins, and the files are read on every connection, so changes work at once without a restart. Each line is daemon_list : client_list:
# SSH only from the office network and this machine
sshd: 192.168.1.0/255.255.255.0
sshd: 127.0.0.1
# everything from local hosts (names without a dot)
ALL: LOCAL# deny everything that wasn't allowed above
ALL: ALL| Pattern | Matches |
|---|---|
ALL | Every service or every client |
LOCAL | Host names without a dot |
192.168.1. | Every address starting with 192.168.1. |
.example.com | Every host name ending in .example.com |
ALL EXCEPT 10.0.0.5 | Everything except that one client |
This “deny all, allow a few” setup is the usual pattern: ALL: ALL in hosts.deny, specific rules in hosts.allow.
TCP wrappers only protect programs built with libwrap, or started through the tcpd wrapper by inetd. xinetd has the support built in. To check a program:
ldd /usr/sbin/sshd | grep libwrapNo output means the files are ignored by that program. Upstream OpenSSH dropped TCP wrapper support in 2014 and RHEL 8 removed TCP wrappers completely, so on a modern system you’d use a firewall (nftables, firewalld or ufw) for the same job.
Exam checklist#
/etc/passwdis world readable and hasxin the password field; the hashes live in/etc/shadow, readable only by root.- Shadow fields: name, hash, last change, min, max, warn, inactive, expire (dates in days since 1970).
- Hash prefixes:
$1$MD5,$5$SHA-256,$6$SHA-512,$y$yescrypt. A leading!or*means no password login. /etc/nologinblocks every login except root and shows its text./usr/sbin/nologinis a shell for one account.- Find listeners with
ss -tulpn, thensystemctl disable --now(andmask) what you don’t need. Disable the.socketunit too. - SysV: scripts in
/etc/init.d/, init config in/etc/inittab(id:runlevels:action:process), reload withtelinit q. - xinetd: defaults in
/etc/xinetd.conf, one file per service in/etc/xinetd.d/;disable = yesturns one off. - TCP wrappers:
hosts.allowfirst, thenhosts.deny, first match wins, no match means allowed. Only forlibwrapprograms.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-110): Setup host security"
[main d352996] learn(lpic-110): Setup host security
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: