Anime-Sanctuary

Connect your NAS

Your photos and backups sleep on a NAS at home, and your server is far away. Let's connect a NAS to your server through the WireGuard tunnel, so the server sees that folder as its own.

Course
Tutorial 27 of 35
Time
40 min to read
Tested with
Samba 4.22, cifs-utils 7.4, nfs-kernel-server 2.8.3, wireguard-tools 1.0.20210914 (Debian 13.7)
What you will be able to do
  • Understand what your home router changes: upload speed, dynamic IP, CGNAT.
  • Link the NAS to the server's WireGuard tunnel.
  • Share a folder from the NAS over SMB, limited to the server.
  • Mount that folder on the server, at boot and without freezes.

Before you start: WireGuard, What is a server?.

Contents
  1. What your router changes: the NAS makes the call
  2. Link the NAS to the tunnel
  3. Share a folder on the NAS
  4. Mount the folder on the server
  5. A NAS at home or storage at the host
  6. If it fails
  7. What's next
  8. Sources

To connect a NAS to your server, the NAS calls the server through the WireGuard tunnel. Then the server mounts one of the NAS folders. Your photos, documents and backups stay at home, and the server reads them as if they were on its own disk. They are your own files, nothing else. Plan on forty minutes, ten of them spent choosing a password.

What your router changes: the NAS makes the call

Your router, the box from your Internet provider, is built to get out to the Internet, not to receive calls. The NAS (network attached storage) is a small computer at home, with disks to store your files. So it joins the tunnel from the article WireGuard as one more machine. A tunnel is a private, encrypted link between two machines. Here is what a home connection changes:

  • Upload speed is how fast your router sends data out to the Internet. It is often much lower than the download speed. Everything the server reads from the NAS leaves your home, so it goes through the router.
  • Dynamic IP: your provider can change the public IP address of your router, the number that identifies it on the Internet. A server looking for the NAS at a fixed address would one day knock on the wrong door.
  • CGNAT means your provider makes several customers share one public address. No call from the Internet then reaches your router, even with the best settings. To find out, open your router's status page (or its app) and look for the IP address on the Internet side. If it falls between 100.64.0.0 and 100.127.255.255, you are behind CGNAT, because that range is reserved for it.

The last two problems disappear if the NAS makes the call. Your server has a fixed address, 203.0.113.10 in our examples. The NAS dials that number from your home, like phoning someone. The server answers inside the tunnel the NAS opened, and the router needs no special rule. If the router's address changes, the NAS simply calls again. Upload speed doesn't go away, though. We come back to it in the chapter on where to store files.

The path of a file between the NAS and the server The NAS, at your home, calls the server through an encrypted tunnel. The server then reads the shared folder through the same tunnel. NAS, at your home 10.77.0.3 mon-serveur 10.77.0.1 tunnel 1. the NAS calls 2. the server reads the folder
Text version: the NAS, at your home, calls mon-serveur through the tunnel. Once the call is through, the server reads the NAS's shared folder through the same tunnel.

The path is drawn. To exchange files, the NAS and the server will speak SMB, the file sharing that Windows, macOS and Linux all know. I recommend it, because its password keeps the folder closed to the other machines in the tunnel. NFS, the other way to share (also a protocol), is described in a section you can open with a click at the end of step 3. Let's start by linking the NAS to the tunnel.

1Link the NAS to the tunnel

The NAS joins the tunnel with the address 10.77.0.3, and the server keeps 10.77.0.1. In this article, your NAS is a machine running Debian 13, for example a small PC or a Raspberry Pi with a disk. A store-bought box isn't covered here. Every block, whether commands or a file, carries a label that says which machine to type it on: "On the NAS" or "On the server". Open a terminal on each machine and follow the label. The word sudo in front of a command runs it with administrator rights, known as root.

The first command installs WireGuard. The second makes two keys for the NAS. One is a private key (nas.key), a secret you show to nobody. The other is a public key (nas.pub), which you can hand out, like a mailbox address. The umask 077 prefix means only root can read the files created afterwards. The third command prints the public key.

On the NAS
sudo apt install -y wireguard wireguard-tools
sudo sh -c 'umask 077; wg genkey | tee /etc/wireguard/nas.key | wg pubkey > /etc/wireguard/nas.pub'
sudo cat /etc/wireguard/nas.pub
Expected output
xhdhdOJylFb3P58x+f8Ss9TcXD2v+XLCg+D1+qM0+BE=

The last line is the NAS's public key. The keys in this article are throwaway examples, and yours will be different. Copy yours, because the server must know it to accept the NAS.

Now the server has to accept the NAS. On the server, open the settings file with nano, a text editor for the terminal. The arrow keys move the cursor, Ctrl+O then Enter saves, and Ctrl+X quits.

On the server
sudo nano /etc/wireguard/wg0.conf

Go to the very bottom of the file, leave an empty line, then add these three lines. A [Peer] is a machine at the other end of the tunnel. Replace only the key after PublicKey = with your NAS's key, the one the NAS printed a moment ago. AllowedIPs is the list of addresses this machine may use in the tunnel. Here there is only one, the NAS's.

On the server, to add at the bottom of wg0.conf
[Peer]
PublicKey = COLLEZ_ICI_LA_CLE_PUBLIQUE_DU_NAS
AllowedIPs = 10.77.0.3/32

Save and quit. The tunnel rereads this file only when told to. The first command reloads it without cutting off the devices that are already connected. The second prints the server's public key.

On the server
sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'
sudo cat /etc/wireguard/server.pub
Expected output
nt8JMZFWwcfG3jyPleI6Ol5polJlUV5i2wcEoDDiX1c=

The last line is the server's public key, the one from the article WireGuard. The NAS needs it to make its call. The NAS also needs its own private key, the secret that proves its identity. Go back to the NAS and print it, to copy it into its settings file. Send it to nobody.

On the NAS
sudo cat /etc/wireguard/nas.key
Expected output
2EusYFR5Evo9DsIApIyh0GhbdLMu4ns7prbAcxFt+kM=

Now open the NAS's settings file, which doesn't exist yet. The first command creates an empty file with 600 permissions: read and write for root alone. So the file is protected before the private key goes in. The second command opens it.

On the NAS
sudo install -m 600 /dev/null /etc/wireguard/wg0.conf
sudo nano /etc/wireguard/wg0.conf

Write these lines. [Interface] describes the NAS itself, and Address is its address in the tunnel. Three values are yours: the NAS's private key, the server's public key and your server's IP address, here 203.0.113.10. Two lines deserve a word. Endpoint is the address and port where the server can be reached (a port is a door number on a machine, and 51820 is WireGuard's). PersistentKeepalive sends a small sign every 25 seconds, so the router keeps the call open. Here, AllowedIPs holds only the server, so the NAS doesn't talk to your other devices.

On the NAS, content of wg0.conf
[Interface]
Address = 10.77.0.3/24
PrivateKey = COLLEZ_ICI_LA_CLE_PRIVEE_DU_NAS

[Peer]
PublicKey = COLLEZ_ICI_LA_CLE_PUBLIQUE_DU_SERVEUR
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.77.0.1/32
PersistentKeepalive = 25

Save and quit. Check that the file is still reserved for root, then switch the tunnel on. The enable --now option starts it right away and at every NAS boot. Without it, a NAS reboot would drop the call.

On the NAS
sudo ls -l /etc/wireguard/wg0.conf
sudo systemctl enable --now wg-quick@wg0
Expected output
-rw------- 1 root root 237 Nov  6 10:03 /etc/wireguard/wg0.conf

The NAS's call stays open in both directions, so the server can talk back. Let's check from the server with ping, which sends small signals and waits for the reply. The -c 3 option limits the test to three sends.

On the server
ping -c 3 10.77.0.3
Expected output
PING 10.77.0.3 (10.77.0.3) 56(84) bytes of data.
64 bytes from 10.77.0.3: icmp_seq=1 ttl=64 time=0.645 ms
64 bytes from 10.77.0.3: icmp_seq=2 ttl=64 time=1.56 ms
64 bytes from 10.77.0.3: icmp_seq=3 ttl=64 time=1.94 ms

--- 10.77.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2014ms

Three replies prove that the tunnel works. Your times will be longer, because they include the trip to your home. The pipe exists, but it is empty. Now you need a folder to put in it.

2Share a folder on the NAS

The NAS shares a folder with Samba, the software that speaks SMB on Linux. The folder in this article is /srv/nas/photos, and its share will be called photos. Keep these two names to follow along without errors. These three commands install Samba, create the folder and drop a test file in it.

On the NAS
sudo apt install -y samba
sudo mkdir -p /srv/nas/photos
echo 'vacances 2026' | sudo tee /srv/nas/photos/note.txt
Expected output
vacances 2026

Samba needs a system account to know who may open the folder. Let's create alice, an account with no home folder and no way to log in. It exists only for the share. It lives on the NAS and has nothing to do with your alice account on the server. The command prints nothing when it works.

On the NAS
sudo useradd --no-create-home --shell /usr/sbin/nologin alice

Now declare the share in /etc/samba/smb.conf. Open this file with nano (same keys as in step 1), go to the very bottom, add the block below, then save and quit.

On the NAS
sudo nano /etc/samba/smb.conf
On the NAS, to add at the bottom of smb.conf
[photos]
   path = /srv/nas/photos
   valid users = alice
   hosts allow = 10.77.0.1
   read only = yes

Every line has its job:

  • [photos]: the name of the share, the one the server will ask for.
  • path: the shared folder on the NAS.
  • valid users: the only account allowed, here alice, created just before.
  • hosts allow: the only address admitted, the server's in the tunnel. Any other device at home gets refused.
  • read only: the server reads without being able to delete anything.

Samba has its own passwords, separate from the system's. Create alice's. The command asks for it twice and doesn't display it. Choose a long one, because you will type it again on the server.

On the NAS
sudo smbpasswd -a alice
Expected output
New SMB password:
Retype new SMB password:
Added user alice.

If it answers Failed to add entry for user alice, the system account is missing, so rerun the useradd command.

Then restart smbd, the service that runs Samba, so it rereads its settings file.

On the NAS
sudo systemctl restart smbd

The folder is shared, but nobody has asked for it yet. That happens on the server side.

3Mount the folder on the server

To mount a folder is to make it appear at a spot on the server, as if it were a local folder. The server first needs two tools. cifs-utils can mount an SMB share (CIFS is the old name of SMB). smbclient tests without mounting anything. The mount also needs alice's password, kept in a file that only root can read. We install the tools, create that file empty and reserved for root, then open it with nano. That way the password stays out of every command history.

On the server
sudo apt install -y smbclient cifs-utils
sudo install -m 600 /dev/null /etc/nas-credentials
sudo nano /etc/nas-credentials

Write two lines, with the Samba password you just created. Save and quit, then show the file's permissions.

On the server, content of nas-credentials
username=alice
password=motdepasse-exemple
On the server
sudo ls -l /etc/nas-credentials
Expected output
-rw------- 1 root root 43 Nov  5 07:05 /etc/nas-credentials

The -rw------- permissions say that only root reads this file. Before mounting, let's ask the NAS to list the folder through the tunnel. In this command, //10.77.0.3/photos gives the NAS's address then the name of the share, -A points to the password file, and -c ls asks for the folder's list.

On the server
sudo smbclient //10.77.0.3/photos -A /etc/nas-credentials -c ls
Expected output
  .                                   D        0  Thu Nov  5 07:04:55 2026
  ..                                  D        0  Thu Nov  5 07:04:55 2026
  note.txt                            N       14  Thu Nov  5 07:04:55 2026

		19452567344 blocks of size 1024. 3739655992 blocks available

The note.txt line proves that the server reads the NAS's folder through the tunnel. The dates and the two numbers at the bottom will be those of your disk. Now mount it for good, with one line in /etc/fstab, the file that lists the disks to mount at boot. A mistake in this file can get in the way of booting. So we back it up first, then check the line.

On the server
sudo mkdir -p /mnt/nas-photos
sudo cp -n /etc/fstab /etc/fstab.avant-nas
echo '//10.77.0.3/photos /mnt/nas-photos cifs credentials=/etc/nas-credentials,ro,_netdev,nofail,x-systemd.automount,x-systemd.idle-timeout=60 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo findmnt --verify
Expected output
//10.77.0.3/photos /mnt/nas-photos cifs credentials=/etc/nas-credentials,ro,_netdev,nofail,x-systemd.automount,x-systemd.idle-timeout=60 0 0
Success, no errors or warnings detected

The second command copies fstab to fstab.avant-nas, without overwriting a copy that is already there. The third adds the line at the end. The fourth tells systemd, the program that starts and watches Debian's services, to reread fstab. Last, findmnt --verify rereads the whole file without mounting anything. Success means it found nothing wrong. An [E] (error) or [W] (warning) line points to the mistake. In that case, put the original back with sudo cp /etc/fstab.avant-nas /etc/fstab and start again at the echo line. If all is well, start the automatic mount, then list the folder.

On the server
sudo systemctl restart remote-fs.target
ls /mnt/nas-photos
Expected output
note.txt

The folder should show note.txt: the mount worked. The options in the fstab line keep a missing NAS from blocking the server:

  • credentials: the password file, so nothing secret sits in fstab.
  • ro: read-only, like the share.
  • _netdev: the disk is at the end of a network, so systemd waits for the network before mounting it.
  • nofail: if the NAS doesn't answer, the server boots anyway.
  • x-systemd.automount: the folder is mounted only on first access. x-systemd.idle-timeout=60 unmounts it after a minute of no use.

If the tunnel drops, a program that reads this folder gets an error after about ten seconds instead of freezing. That is the default behavior of an SMB mount. To check the boot, type sudo reboot on the server. Even with the NAS off, it restarts normally. Once the NAS is back on, the folder answers within the next minute.

Let the server write to the folder

For backups that the server must drop off, make three changes. On the NAS, in smb.conf, replace yes with no on the read only line. Still on the NAS, give the folder to alice: chown changes the owner of a folder. On the server, remove ro, from the fstab line, then run sudo systemctl daemon-reload.

On the NAS
sudo chown alice /srv/nas/photos

The folder unmounts by itself after a minute of no use. It then mounts again in read and write mode, and the files you drop belong to alice on the NAS.

The NFS variant, in full

NFS does the same job as SMB, but it checks identity differently. Each user has a number, the UID, and NFS compares those numbers. The table compares the two.

CriterionSMB (this article's path)NFS
Who uses itAll your devicesMostly Linux machines
Who is recognizedA name and a passwordThe calling computer and its user numbers
Setup on the NASA block in smb.conf and a passwordOne line in /etc/exports

NFS assumes that the server and the NAS number their users the same way, as the exports(5) page points out. If you still choose it, these steps replace steps 2 and 3 (sharing and mounting over SMB). If you already did step 2, stop Samba with sudo systemctl disable --now smbd, because NFS doesn't need it.

On the NAS, one line in /etc/exports gives the folder to the server alone, read-only. The exportfs -ra command rereads that file, and exportfs -v shows what is shared.

On the NAS
sudo apt install -y nfs-kernel-server
echo '/srv/nas/photos 10.77.0.1(ro,sync,no_subtree_check)' | sudo tee -a /etc/exports
sudo exportfs -ra
sudo exportfs -v
Expected output
/srv/nas/photos 10.77.0.1(ro,sync,no_subtree_check)
/srv/nas/photos
		10.77.0.1(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)

The root_squash option strips the server's root of its powers on the NAS. On the server, if the folder is mounted, unmount it first with sudo umount /mnt/nas-photos. Then install nfs-common and create the mount folder. The first cp keeps a backup of fstab without overwriting the one from step 3. The second puts that backup back, which removes the SMB line. Add the NFS line, check it, then list the folder.

On the server
sudo apt install -y nfs-common
sudo mkdir -p /mnt/nas-photos
sudo cp -n /etc/fstab /etc/fstab.avant-nas
sudo cp /etc/fstab.avant-nas /etc/fstab
echo '10.77.0.3:/srv/nas/photos /mnt/nas-photos nfs4 ro,_netdev,nofail,x-systemd.automount,x-systemd.idle-timeout=60 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo findmnt --verify
sudo systemctl restart remote-fs.target
ls /mnt/nas-photos
Expected output
note.txt

If alice doesn't have the same UID on both machines, ls -l shows her as a number. By default, NFS also makes a program wait forever when the NAS stops answering. If the tunnel drops, the terminal freezes.

The folder is in place. One question remains: is this NAS at your home really the right place for these files?

A NAS at home or storage at the host

The host is the company that rents you your server. Keep the NAS at home for files that are rarely read, like photo archives. For files that many people read all the time, like a website, use the server's disk. The cause is your router's upload speed: the server reads from the NAS, and everything it reads leaves your home. The table compares the two places.

CriterionNAS at homeServer disk
Read speedLimited by your router's upload speedThe server's own, without going through your home
When it stopsPower, router or tunnel failureServer failure only
CostDisks you already own, no extra rentA rent that grows with capacity
Where the files areAt home, within reachAt the host

A little math gives the order of magnitude. An upload speed of 100 Mbit/s (a hundred million bits per second) gives at best 12.5 MB/s, megabytes per second. It takes eight bits to make a byte, the unit for file size, and a megabyte is a million of them. A hundred divided by eight is 12.5. So 10 GB, gigabytes of photos (a GB is a thousand MB), leave in 800 seconds, a little over thirteen minutes. To learn your upload speed, run an online speed test and read the "upload" line, or check your customer account. Real life stays below this ceiling. If that time is too long for a file you open often, it goes on the server's disk.

If it fails

These messages appear in the server's terminal, in place of the expected output. Find yours, then do the action next to it:

  • tree connect failed: NT_STATUS_ACCESS_DENIED: the NAS refuses the server. On the NAS, grep -n 'hosts allow' /etc/samba/smb.conf prints that line of the share with its number (grep searches a file for a word). It must end with 10.77.0.1. Also check that the server aims at //10.77.0.3/photos, the tunnel address.
  • NT_STATUS_LOGON_FAILURE: wrong name or password. Reread /etc/nas-credentials on the server. On the NAS, sudo pdbedit -L, which lists the Samba accounts, must show alice. And sudo smbpasswd -a alice changes her password.
  • NT_STATUS_BAD_NETWORK_NAME: the share name is wrong. It must be photos, as in [photos].
  • NT_STATUS_CONNECTION_REFUSED: the NAS can be reached, but Samba doesn't answer. On the NAS, sudo systemctl start smbd starts it again.
  • NT_STATUS_IO_TIMEOUT, or cannot open directory ... No such device on ls: the server can no longer reach the NAS. On the server, ping -c 3 10.77.0.3 confirms it. Check that the NAS is on, then wait a minute, because its call repeats by itself. Meanwhile, ping may answer Destination address required, which means the server doesn't know the NAS's address yet. On the server, sudo wg show prints latest handshake once the tunnel is back.
  • ls shows an empty folder: the automatic mount hasn't started. Type sudo systemctl restart remote-fs.target on the server, then run ls again.
  • A permissions error on ls: first rerun the smbclient command. If it works, compare the fstab line letter by letter with the one in step 3.
  • An error from findmnt --verify: put the backup back with sudo cp /etc/fstab.avant-nas /etc/fstab then retype the line from step 3.
  • The folder answers slowly: the most likely cause is the upload speed discussed above.

What's next

Your server now reads your NAS's files through a private tunnel between your two machines. This server can do even more: it can run an artificial intelligence assistant. Later in the course, An AI assistant on your server installs one, now that your files are within reach.