Menu

Security

Step 2 of 3 LPIC-1 110.2weight 3

Setup host security

“How shadow passwords work, how to switch off services you don't use, and what TCP wrappers do.”

View .md

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:

bash
grep alice /etc/passwd
console
alice: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:

bash
ls -l /etc/passwd /etc/shadow
console
-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.

bash
sudo grep alice /etc/shadow
console
alice:$y$j9T$Fq0eD2sB6kq3/x1Ylm7cR.$4xZ0p8bJ1mQ9uE2wR7tVh3aKcN5dL6sG0yF1iH8oP2.:20785:0:99999:7:::
FieldExampleMeaning
1aliceUser name
2$y$j9T$Fq0…$4xZ…Hashed password
320785Last change, in days since 1 Jan 1970 (here: 28 Nov 2026)
40Minimum days between changes
599999Maximum days the password is valid
67Warning 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:

PrefixAlgorithm
$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 *.

Groups have the same split: /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:

bash
echo "Maintenance until 14:00. Back soon." | sudo tee /etc/nologin

While 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:

bash
sudo rm /etc/nologin

systemd 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:

bash
sudo ss -tulpn

Then, 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)#

bash
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/Fedora

disable --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):

bash
systemctl list-sockets
console
LISTEN                       UNIT          ACTIVATES
/run/cups/cups.sock          cups.socket   cups.service
/run/dbus/system_bus_socket  dbus.socket   dbus.service
[::]:22                      ssh.socket    ssh.service
/usr/lib/systemd/system/cups.socketini
1[Unit]
2Description=CUPS Scheduler
3PartOf=cups.service
4
5[Socket]
6ListenStream=/run/cups/cups.sock
7
8[Install]
9WantedBy=sockets.target

The [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:

bash
sudo systemctl disable --now cups.socket cups.service

Ubuntu 24.04 starts SSH this way too (ssh.socket).

With SysV init (legacy)#

Older systems start services with shell scripts in /etc/init.d/:

bash
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 boot

The 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:

/etc/inittabtext
# 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 tty2

Each 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/:

/etc/xinetd.conftext
defaults
{
    instances       = 60
    log_type        = SYSLOG authpriv
    log_on_success  = HOST PID
    log_on_failure  = HOST
    cps             = 25 30
}

includedir /etc/xinetd.d
/etc/xinetd.d/telnettext
service 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:

  1. /etc/hosts.allow is checked first. If a rule matches, access is granted.
  2. Otherwise /etc/hosts.deny is checked. If a rule matches, access is denied.
  3. 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:

/etc/hosts.allowtext
# 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
/etc/hosts.denytext
# deny everything that wasn't allowed above
ALL: ALL
PatternMatches
ALLEvery service or every client
LOCALHost names without a dot
192.168.1.Every address starting with 192.168.1.
.example.comEvery host name ending in .example.com
ALL EXCEPT 10.0.0.5Everything 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:

bash
ldd /usr/sbin/sshd | grep libwrap

No 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/passwd is world readable and has x in 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/nologin blocks every login except root and shows its text. /usr/sbin/nologin is a shell for one account.
  • Find listeners with ss -tulpn, then systemctl disable --now (and mask) what you don’t need. Disable the .socket unit too.
  • SysV: scripts in /etc/init.d/, init config in /etc/inittab (id:runlevels:action:process), reload with telinit q.
  • xinetd: defaults in /etc/xinetd.conf, one file per service in /etc/xinetd.d/; disable = yes turns one off.
  • TCP wrappers: hosts.allow first, then hosts.deny, first match wins, no match means allowed. Only for libwrap programs.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. Why are password hashes kept in /etc/shadow instead of /etc/passwd?
  2. A host is listed in both /etc/hosts.allow and /etc/hosts.deny. What happens?
  3. What happens when the file /etc/nologin exists?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy