Sub topic 15
Hardening both services
This chapter assumes you can already export an NFS directory, define a Samba share, and mount both from a client. It also assumes you know the firewalld basics from the earlier chapters. Here you tighten what you built. The aim is that a stranger on the network, a stolen password, or a careless client gains as little as possible.
Think in layers
No single setting secures a file server. Each layer below narrows what the next one has to trust.
Work from the outside in. If a layer is weak, make sure the next layer still holds.
Hardening Samba
Start with [global]. The goals are to refuse guests, refuse old protocol dialects, and listen only where needed.
[global]
workgroup = WORKGROUP
server role = standalone server
map to guest = never
restrict anonymous = 2
# Network exposure
interfaces = lo eth0
bind interfaces only = yes
hosts allow = 127.0.0.1 192.168.1.0/24
hosts deny = ALL
# Protocol strength
server min protocol = SMB3
server signing = mandatory
ntlm auth = ntlmv2-only
server smb encrypt = required
[projects]
path = /srv/samba/projects
read only = no
guest ok = no
valid users = @staff
force group = staff
create mask = 0660
directory mask = 0770What each group of settings does:
map to guest = neverandrestrict anonymous = 2stop unauthenticated sessions from listing shares or users.interfaceswithbind interfaces only = yeskeepssmbdoff interfaces that face other networks. Use your real interface name in place ofeth0.hosts allowandhosts denyfilter clients inside Samba. They are a second line behind the firewall, not a replacement for it.server min protocol = SMB3rejects SMB1 and SMB2 clients. Check that your clients support SMB3 first. Windows 8 and later, and currentcifs-utils, do.server signing = mandatorymakes each packet verifiable, so a machine in the middle cannot alter traffic.server smb encrypt = requiredencrypts all traffic. This needs SMB3 and costs some CPU. You can set it on a single share instead if only some data is sensitive.ntlm auth = ntlmv2-onlydrops the weak LM and NTLMv1 hashes.
The share itself tightens too. Masks of 0660 and 0770 remove all access for "other", so only members of staff can read the files. Setting guest ok = no explicitly documents the intent.
Always validate and restart afterwards:
sudo testparm -s
sudo systemctl restart smbd
sudo smbstatussmbstatus shows each connection's protocol version and whether it is signed or encrypted. This is how you confirm that clients are really using the strong settings.
Samba account hygiene
- Give Samba users
-s /usr/sbin/nologinso a leaked Samba password does not give shell access. - Run
sudo smbpasswd -d aliceto disable an account without deleting it. Usesudo smbpasswd -x aliceto remove it from the Samba database. - Use a different password for Samba than for the system login. Samba keeps its own hash database, so they are independent anyway.
Hardening NFS
NFS's classic weakness is that, with the default sec=sys, the server trusts the user IDs the client sends. Anyone who controls a client machine, or can impersonate its IP address, can claim any UID. So most NFS hardening is about limiting who can be a client, and about what that client can do.
Edit /etc/exports so the export is narrow:
# /etc/exports
/srv/nfs/projects 192.168.1.10(rw,sync,no_subtree_check,root_squash)
/srv/nfs/reference 192.168.1.0/24(ro,sync,no_subtree_check,all_squash,anonuid=65534,anongid=65534)- List single hosts when you can, and a subnet only when you must. Never use
*. - Keep
root_squash(the default). It maps client root to the unprivilegednobodyuser. Avoidno_root_squashexcept in a tightly controlled case such as a diskless boot server. - Use
rofor data that clients should not change. Useall_squashfor read-only public data, so every client user becomes the anonymous user. - Keep
syncso the server confirms writes only after they reach disk.asyncis faster but can lose acknowledged data in a crash.
Next, disable the old protocol versions. NFSv3 needs several extra helper services (rpcbind, mountd, statd, lockd) on varying ports. NFSv4 needs only TCP 2049. Fewer services means a smaller attack surface and a simpler firewall.
# /etc/nfs.conf
[nfsd]
vers2=n
vers3=n
vers4=y
vers4.0=y
vers4.1=y
vers4.2=yApply and verify:
sudo systemctl restart nfs-server
cat /proc/fs/nfsd/versions
sudo exportfs -vThe versions file should show -2 -3 +4 +4.1 +4.2. A minus means disabled and a plus means enabled.
Real authentication with Kerberos
For data that matters, use Kerberos. Add sec=krb5p to the export options and the mount options. Then the server verifies who the user really is, and krb5p also encrypts the traffic. Plain krb5 authenticates only. krb5i adds integrity checks. Kerberos needs a working realm, host keytabs, and synchronized clocks, so it is a larger project than the other steps here. Without Kerberos, assume that NFS is safe only on a network where you trust every client.
Hardening the clients
The client also decides what a mounted share may do. Add these options in /etc/fstab:
fileserver:/srv/nfs/projects /mnt/nfs-projects nfs4 defaults,_netdev,nosuid,nodev,noexec 0 0
//fileserver/projects /mnt/smb-projects cifs credentials=/root/.smbcreds,vers=3.1.1,seal,nosuid,nodev,noexec,uid=1000,gid=1000,_netdev 0 0nosuidignores setuid bits on files from the server, so a compromised server cannot hand you a root-owned program.nodevignores device files.noexecblocks running programs from the share, where that is acceptable.sealrequests SMB3 encryption on the client side, andvers=3.1.1refuses anything older.
Protect the credentials file, because it holds a plain-text password:
sudo chown root:root /root/.smbcreds
sudo chmod 600 /root/.smbcredsRestrict the firewall by source
Opening the samba or nfs service in firewalld allows every source on the zone. Instead, remove the general service and allow only your subnet:
sudo firewall-cmd --permanent --remove-service=samba
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="samba" accept'
sudo firewall-cmd --permanent --remove-service=nfs
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="nfs" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --list-allSummary of the main controls
| Control | Samba | NFS |
|---|---|---|
| Limit clients | `hosts allow`, `interfaces` | Host or subnet in `/etc/exports` |
| Minimum version | `server min protocol = SMB3` | `vers3=n` in `/etc/nfs.conf` |
| Encrypt traffic | `server smb encrypt = required` | `sec=krb5p` |
| Identity | Samba password, `valid users` | Kerberos, `root_squash`, `all_squash` |
| Verify | `testparm`, `smbstatus` | `exportfs -v`, `/proc/fs/nfsd/versions` |
Exercise
A server exports /srv/nfs/data to 192.168.1.0/24 with rw,sync,no_root_squash. A user with root on one client creates a setuid-root shell on the export. Name two changes, one on the server and one on the client, that reduce this risk.
Show answer
On the server, remove no_root_squash so client root is mapped to nobody (root_squash is the default), then run sudo exportfs -ra. On the client, add nosuid to the mount options in /etc/fstab so setuid bits on the share are ignored. Narrowing the export to specific hosts also helps.
Exercise
After you set server min protocol = SMB3 and server smb encrypt = required, which command shows whether a connected client is actually using signing and encryption?
Show answer
sudo smbstatus. Its output lists each session's protocol version, encryption, and signing state.