How to Share Files Over the Network Using NFS or Samba
NFS suits Linux-to-Linux sharing, Samba suits networks with Windows and Mac. How to choose, and the steps to set up each on Ubuntu or Debian.
3 min readBy Soumya
Reviewed by the AmazingRDP technical team

A server of your own to practise on. A Linux VPS gives you root access. See Linux VPS from $6.99
Linux has two standard ways to share a folder with other computers on a network. NFS is the native Unix way. Samba speaks SMB, the protocol Windows uses for its shared folders. Both work well; which one fits depends on who needs to reach the files.
Which one?
| NFS | Samba (SMB) | |
|---|---|---|
| Best for | Linux and Unix machines sharing with each other | Networks with Windows or Mac computers |
| Access control | By the client's IP address; file rights by user number | By user name and password |
| Windows clients | Possible, awkward | Built in |
| Setup | One line per share | A few lines and a password per user |
| Port | 2049 | 445 |
Linux servers only: NFS. Anything with Windows or Mac: Samba. You can run both on the same folder.
NFS: the server
sudo apt update
sudo apt install nfs-kernel-server
sudo mkdir -p /srv/share
sudo chown nobody:nogroup /srv/share
Shares are listed in /etc/exports, one per line: the folder, who may connect, and options.
sudo nano /etc/exports
/srv/share 192.168.1.0/24(rw,sync,no_subtree_check)
That shares the folder with every machine in the network 192.168.1.x, for reading and writing. Use a single address in place of the range to allow one machine. Apply it:
sudo exportfs -ra
sudo exportfs -v
NFS: the client
sudo apt install nfs-common
sudo mkdir -p /mnt/share
sudo mount -t nfs 192.168.1.10:/srv/share /mnt/share
To mount it at every start, add a line to /etc/fstab:
192.168.1.10:/srv/share /mnt/share nfs defaults,_netdev 0 0
More on that file in how to mount a drive in Linux.
Two things that surprise people
- Users are matched by number. A file owned by user 1001 on the server belongs to whoever is user 1001 on the client. Keep the numbers the same on both sides.
- root on the client is not root on the share. By default it is treated as the user
nobody. That is a safety feature; leave it on.
Samba: the short version
sudo apt install samba
sudo nano /etc/samba/smb.conf
Add at the end:
[share]
path = /srv/share
read only = no
valid users = anna
sudo smbpasswd -a anna
sudo systemctl restart smbd
The user must exist on the server and have rights on the folder. From Windows, open \\192.168.1.10\share in File Explorer; on a Mac, Go, Connect to Server, smb://192.168.1.10/share. Our full guide with groups, guest access and errors: how to set up Samba on Ubuntu.
Firewall
sudo ufw allow from 192.168.1.0/24 to any port 2049 proto tcp
sudo ufw allow from 192.168.1.0/24 to any app Samba
Open these for your own network only. See open or block a port in the Ubuntu firewall.
Not over the open internet
Neither protocol should face the internet. NFS in its normal setup has no encryption and trusts IP addresses; SMB ports are attacked constantly, and many internet providers block port 445. Between servers in different places, share through a VPN or a private network, or use SFTP: see FTP or SFTP.
If it does not work
| Sign | Look at |
|---|---|
| mount: access denied by server | The client's address is not covered by the line in /etc/exports, or exportfs -ra was not run |
| Mount hangs | A firewall blocks port 2049 |
| Permission denied when writing (NFS) | User numbers differ, or the folder's rights do not allow it: see setfacl |
| Windows asks for the password again and again | The Samba password was never set with smbpasswd -a |
To practise on a server of your own, see our Linux VPS plans.
Questions
What is the difference between NFS and Samba?
NFS is the native file sharing of Linux and Unix and controls access by IP address. Samba speaks the Windows protocol SMB and uses user names and passwords.
Which is better, NFS or Samba?
NFS between Linux machines; Samba when Windows or Mac computers need the files.
How do I share a folder with NFS on Ubuntu?
Install nfs-kernel-server, add the folder to /etc/exports and run sudo exportfs -ra.
Can I use NFS over the internet?
Not directly. Standard NFS is unencrypted and trusts IP addresses. Use a VPN or SFTP between distant machines.