Sub topic 13
Opening the firewall
A correct smb.conf or /etc/exports does nothing if the server's firewall drops the client's packets. This chapter covers which ports each protocol needs, how to open them with firewalld and ufw, how to limit them to your own network, and how to test the result.
This chapter assumes you can run systemctl, know what a TCP port and a subnet are, and have a working Samba share or NFS export from the earlier chapters.
The working model
A firewall filters traffic by port and source address, before any file-sharing daemon sees it. Four things must all be true for a mount to work:
- The daemon is running and listening on the port.
- The firewall allows the client's address to reach that port.
- The share or export allows the client and user.
- The file permissions and SELinux labels allow the access.
This chapter covers step 2. Firewall failures tend to look like timeouts or "No route to host", not "permission denied". That difference is a useful clue.
Which ports each service uses
| Service | Ports | Notes |
|---|---|---|
| SMB2/SMB3 (Samba) | TCP 445 | The only port modern clients need. |
| NetBIOS over TCP | TCP 139, UDP 137 and 138 | Old name and session service. Needed only for legacy clients and network browsing. |
| NFSv4 | TCP 2049 | A single port. Locking, mounting and ID mapping all travel over it. |
| NFSv3 | TCP and UDP 111, 2049, plus mountd, statd and lockd | Several helper daemons, which often use random ports unless you pin them. |
The firewalld samba service opens 137/udp, 138/udp, 139/tcp and 445/tcp. The nfs service opens only 2049/tcp, so it is enough for NFSv4. NFSv3 needs the nfs3, rpc-bind and mountd services as well.
Opening ports with firewalld
firewalld keeps two configurations: the runtime one, which is active now, and the permanent one, which is loaded at boot. The --permanent flag edits only the permanent one, so a reload is needed to activate it.
sudo firewall-cmd --permanent --add-service=samba
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --reload
sudo firewall-cmd --list-all--list-all shows the services and ports that the default zone allows. Check that samba or nfs appears in the services: line.
> On a machine where only some of these services run, open only those. Every extra open port is more attack surface.
Limiting access to your network
Adding a service to the default zone, often public, opens it to every address that can reach the server. A safer approach is to bind your trusted subnet to a stricter zone such as internal and open the services only there:
sudo firewall-cmd --permanent --zone=internal --add-source=192.168.1.0/24
sudo firewall-cmd --permanent --zone=internal --add-service=samba
sudo firewall-cmd --permanent --zone=internal --add-service=nfs
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zonesPackets from 192.168.1.0/24 now match the internal zone. Packets from anywhere else fall into the interface's zone, which does not allow these services. If you opened the services in public earlier, remove them there with --remove-service.
A rich rule does the same job for a single service without creating a zone assignment:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="nfs" accept'
sudo firewall-cmd --reloadPinning NFSv3 helper ports
If you must serve NFSv3 clients, the helper daemons need fixed ports before you can write firewall rules for them. On Red Hat-family systems you set mountd, statd and lockd ports in /etc/nfs.conf. Then you open those ports plus 111. Most new deployments should use NFSv4 only, which avoids this work. The NFS architecture chapter covers how to disable v3.
Opening ports with ufw
Debian and Ubuntu systems often use ufw. It accepts a source subnet in the rule, so you can limit access in one command:
sudo ufw allow from 192.168.1.0/24 to any port 445 proto tcp
sudo ufw allow from 192.168.1.0/24 to any port 2049 proto tcp
sudo ufw status numberedIf ufw is not yet enabled, add an SSH rule first. Enabling ufw without one can lock you out of a remote server.
sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp
sudo ufw enableThe Samba package also installs a ufw application profile named Samba. sudo ufw allow Samba opens the NetBIOS ports as well as 445, and it is not limited to a subnet. Prefer the explicit rule for 445 unless you need legacy clients.
Verifying from both sides
Test in this order. It separates the daemon problem from the firewall problem.
On the server, confirm the daemon listens:
sudo ss -tlnp | grep -E ':(445|2049)\b'On the client, test whether the port is reachable:
nc -zv fileserver 445
nc -zv fileserver 2049The result tells you where to look:
| Client sees | Likely cause |
|---|---|
| `succeeded` or `open` | The firewall allows the port. Look at share, export, user or permission settings. |
| `Connection refused` | The packet reached the server, but nothing listens on that port. Check the service with `systemctl status`. |
| `No route to host` | firewalld rejected the packet. This is its default behavior. Check zones and services. |
| Hangs until timeout | A firewall silently dropped the packet. This may be ufw's default, a cloud security group, or a network device between the hosts. |
For NFS, showmount -e fileserver asks the mountd service, which belongs to the NFSv3 protocol. With only port 2049 open it can fail or hang, even though an NFSv4 mount works. Test an NFSv4 setup with the mount itself.
Common errors
- Forgetting
--reload. The permanent rule exists, butfirewall-cmd --list-alldoes not show it. Reload, or add the rule without--permanentto test. - Wrong zone. The service is allowed in
public, but the client's traffic arrives in a different zone. Runfirewall-cmd --get-active-zonesand read the zone that holds the client's interface or source. - Opening
sambaand expecting NFS to work. Each protocol needs its own rule. - Outside the host. A cloud security group or a router ACL can block the port even when the host firewall is correct. The host's rules cannot fix that.
- Opening too wide. Never expose 445 or 2049 to the internet. SMB in particular is a frequent target for attacks.
Exercise
A client runs sudo mount -t nfs4 fileserver:/srv/nfs/projects /mnt/projects and gets "No route to host". On the server, ss -tlnp shows something listening on port 2049. What is the most likely cause, and which commands fix it for a subnet of 192.168.1.0/24?
Show answer
firewalld is rejecting the packets, because the daemon is listening but the client never reaches it. Check the active zones with sudo firewall-cmd --get-active-zones. Then allow the nfs service for that subnet, for example sudo firewall-cmd --permanent --zone=internal --add-source=192.168.1.0/24, then sudo firewall-cmd --permanent --zone=internal --add-service=nfs, then sudo firewall-cmd --reload. Confirm from the client with nc -zv fileserver 2049.