homemade-nas.html
HTML document, Unicode text, UTF-8 text
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4<meta charset="UTF-8"> 5<title> 6Cheap, homemade NAS with Raspberry Pi 7</title> 8<link rel="stylesheet" href="/static/style.css"> 9<meta name="viewport" content="width=device-width, initial-scale=1.0"> 10</head> 11<body> 12<header> 13<a href="#main" id="skip-link">Skip navigation</a> 14<nav> 15<ul> 16<li><a href="/">Home</a></li> 17<li><a href="/projects">Projects</a></li> 18<li><a href="/index">Index</a></li> 19<li><a href="/about">About</a></li> 20<li><a href="https://roundabout-host.com/roundabout">Roundabout-host</a></li> 21</ul> 22<ul> 23<li><a href="mailto:root@roundabout-host.com" id="mail-link">root@roundabout-host.com</a></li> 24</ul> 25</nav> 26</header> 27<main id="main"> 28 29<h1>Cheap, homemade NAS with Raspberry Pi</h1> 30<div id="article-date">2024-05-21</div> 31<p class="tags"> 32 33<a href="/index/hardware.html" class="tag">hardware</a> 34 35<a href="/index/server.html" class="tag">server</a> 36 37<a href="/index/networking.html" class="tag">networking</a> 38 39<a href="/index/raspberry pi.html" class="tag">raspberry pi</a> 40 41<a href="/index/nas.html" class="tag">nas</a> 42 43<a href="/index/gnu/linux.html" class="tag">gnu/linux</a> 44 45</p> 46 47<article class="content-area"> 48<p>This is a very simple, cheap and quick way to get networked storage at home. It should not cost 49more than €120 for all the components (assuming you've got a network you can plug it into). It also 50offers more flexibility than a commercial NAS, because you can install any software you want on it. 51And if you already use the Raspberry Pi for something else, you can just add this to it and not 52worry about an extra device you need power, networking, space and maintenance for. 53</p><p>A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives, 54since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each. 55</p><p>However, for large-scale use, this method should be done with a different type of computer, not 56a Raspberry Pi. 57</p><p>I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that; 58if you don't know these things read some material when you need it. 59</p><p>If you think this is for you, keep reading. 60</p><h2>Materials</h2><ul><li><p>Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using 61the Pi 4 with 8GB of RAM, but you can use any model. 62</p></li><li><p>microSD card of at least 16GB (this won't be your primary storage but a boot device) 63</p></li><li><p>SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer; 64it's easier that way. Choose ext4 as the filesystem. 65</p></li><li><p>Suitable adapter(s) for the SATA drive(s). 66</p><ul><li><p>For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient. 67</p></li><li><p>If your other kind of small computer has SATA, you can use that. 68</p></li><li><p>Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a 69SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one 70that can take power from a separate source if you're going to use 3.5" drives. 71</p></li></ul></li><li><p>A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed 72as fast charging) is enough. 73</p></li><li><p>If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one. 74</p><ol><li><p>5" drives need more power than USB can provide. 75</p></li></ol></li><li><p>If you want to use wired networking, a cable. If you want to use wireless networking and your 76computer doesn't have Wi-Fi, some Wi-Fi adapter. 77</p></li><li><p>If you have an SBC you should have a case, but it's not required. You can also use lego. 78</p></li><li><p>Some other computer. An Android phone is fine; just install <a href="https://termux.com/">Termux</a>. 79</p></li></ul><h3>My setup</h3><ul><li><p>Raspberry Pi 4 with 8GB of RAM (€80) 80</p></li><li><p>No-name 3.5" SATA to USB adapter with power supply (€18) 81</p></li><li><p>One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC. 82</p></li><li><p>Raspberry Pi 15W power adapter (€9) 83</p></li><li><p>A 1.5m Cat6 cable (€3) 84</p></li><li><p>SanDisk Extreme 128GB microSD card (€15) 85</p></li><li><p>Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget 86Raspberry Pi is not Apple. 87</p></li></ul><p>Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can 88certainly have a cheaper setup: 89</p><h3>Low-cost setup</h3><ul><li><p>Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't 90have wired networking, only one USB port, and too little RAM. 91</p></li><li><p>Cheaper no-name 3.5" SATA to USB adapter with power supply (€10) 92</p></li><li><p>One Toshiba P300 1TB 3.5" SATA drive (€50) 93</p></li><li><p>Phone charger (€6, if not already owned) 94</p></li><li><p>No cable, use Wi-Fi (I assume you have Wi-Fi) 95</p></li><li><p>SanDisk Ultra 32GB microSD card (€7) 96</p></li><li><p>No case 97</p></li></ul><p>Total: €113, with drive included! 98</p><p>What about a "real" NAS? 99</p><p>Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else, 100it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive. 101(The brand is Synology.) 102</p><p>No, it doesn't come with drives. Using the same drive as above, it would reach €230. 103</p><p>Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty 104and professional support. However, it's not the kind of NAS a business would use either. 105</p><h2>Setup (skip if you already have the computer set up)</h2><ol><li><p>Flash your microSD card. Raspberry Pi recommends <a href="https://www.raspberrypi.org/software/">their own (free) software</a>, 106but there are other methods as well. Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu 107Server or plain Debian. Make sure to enable SSH and preset the Wi-Fi settings if you're going 108to use Wi-Fi. 109</p></li><li><p>Put the card in the computer, plug in the network and start it. 110</p></li><li><p>Find out the IP address of the computer. You can use your router's web interface, or a network 111scanner. Now is a good time to set up a static IP address for the computer, forward its ports 112and get dynamic DNS if you want to access it from the internet. <a href="https://ydns.io/">YDNS</a> is a 113really simple and free service for that. Their official client is also free software. 114</p></li><li><p>Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled <code>ssh</code> 115client. On Windows, you can use <a href="https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html">PuTTY</a>. 116</p></li><li><p>Set a root password with <code>sudo passwd</code>. Then <code>su</code>. 117</p></li><li><p>Update the system with <code>apt update && apt -y upgrade</code>. 118</p></li></ol><h2>Set up the drives</h2><p>You must edit the <code>/etc/fstab</code> to automatically mount the drives on boot. If you know how to do 119this, you can do it yourself. For the rest of the tutorial the mount point will be <code>/storage</code>. 120</p><ol><li><p>Turn off the computer and plug in the drive(s). 121</p></li><li><p>Turn on the computer. 122</p></li><li><p><code>lsblk</code> to find the drive(s). They should be <code>/dev/sd</code> followed by a letter. For now we'll 123only take car of one drive, but you can do the same for multiple drives. 124</p></li><li><p>Find the drive's UUID with <code>blkid /dev/sdX1</code>. 125</p></li><li><p>Add the drive to the <code>/etc/fstab</code> as such: 126</p><pre data-language="">UUID=00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2 127</pre></li></ol><p>Replace the UUID with the one you found and, if you want, the mount point (<code>/storage</code> in this 128case) with another one, and the filesystem to match the one you formatted the drive with. Change 129the last column to <code>0</code> to disable fsck for that drive. 130</p><ol><li><p><code>mkdir /storage</code> to create the mount point. 131</p></li><li><p>Reboot. 132</p></li></ol><h2>Give each user their private directory</h2><p>We'll mirror the home layout. However, we won't move the existing home directories, because then 133users can't use the SD card, and you may have some other apps to run on the server that you want 134to be separate from the mass storage. 135</p><p>Write a systemd service to create the directories on boot. Give it a name inside <code>/etc/systemd/system/</code>, 136like <code>/etc/systemd/system/setup_storage_dirs.service</code> 137</p><pre data-language="ini">[Unit] 138Description=Update private user storage spaces 139 140[Service] 141Type=oneshot 142ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py 143 144[Install] 145WantedBy=multi-user.target 146</pre><p>And the script <code>/usr/local/bin/setup_storage_dirs.py</code>: 147</p><pre data-language="python">#!/bin/python3 148 149import os 150import subprocess 151from pathlib import Path 152 153homes = Path("/home").glob("*") 154storage_path = Path("/storage") # change this if you changed the mount point 155 156for home in homes: 157if home.is_dir(): 158user = home.name 159storage = storage_path/user 160bound = home/"Storage" # change this if you'd like a different name 161if not storage.exists(): 162os.makedirs(storage) 163os.chown(storage, home.stat().st_uid, home.stat().st_gid) 164os.chmod(storage, 0o700) 165if not bound.is_dir(): 166os.makedirs(bound) 167os.chown(bound, home.stat().st_uid, home.stat().st_gid) 168os.chmod(bound, 0o700) 169 170# Using bind mounts; some file managers don't like symlinks 171subprocess.run(["mount", "--bind", str(storage), str(bound)]) 172</pre><p>Reboot. Now each user has a private directory on your drive at <code>~/Storage</code>. You can add more 173users; each will get their own directory only they can access. 174</p><h2>Using the server</h2><p>This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH) 175because it's plug-and-play, the speed difference is negligible, it integrates with system users, 176it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted. 177</p><p>Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP 178servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem, 179it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP 180add-on. Not like Next"cloud"¹ is more integrated. 181</p><p>If you want to mount it there is the <code>sshfs</code> program as well. This is useful if you don't like 182GUI file managers or want easier access to the files. 183</p><h3>SFTP in the terminal</h3><pre data-language="sh">sftp -oPort=22 user@server 184</pre><p>If you use port 22, you can omit the oPort option. The commands are identical to the ones in the 185classic <code>ftp</code> client. 186</p><h3>SSHFS</h3><pre data-language="sh">sshfs user@server:/storage/user ~/server 187</pre><p>Second argument is the "mount point". If you want to unmount it: 188</p><pre data-language="sh">fusermount -u ~/server 189</pre><h3>SFTP in the file manager</h3><p>Graphically, you can access SFTP by typing <code>sftp://user@server</code> in the address bar (works in most 190cases) or by finding an option to connect to a server. 191</p><h4>Nemo (GNU/Linux)</h4><p>File > Connect to server > select type <code>SSH</code> 192</p><h4>Material Files (Android)</h4><p>Menu > Add storage > SFTP server 193</p><h4>Amaze (Android)</h4><p>Plus button > Cloud connection > SCP/SFTP Connection 194</p><h4>Windows</h4><p>Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free 195software. 196</p><h4>Samsung phone file manager (Android)</h4><p>Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server 197</p> 198</article> 199 200</main> 201<footer> 202<p>Page generated on Sunday, 4 May 2025 at 15:06:42</p> 203<p xmlns:cc="http://creativecommons.org/ns#" >This work is marked with <a href="https://creativecommons.org/publicdomain/zero/1.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC0 1.0 Universal</a> (🄍). No rights reserved.</p> 204<p>Hosted at <a href="https://roundabout-host.com/roundabout">Roundabout-host</a> using the static site service, and generated with <a href="/projects/ampoule.html">Ampoule</a>.</p> 205<a href="#">Back to top</a> 206</footer> 207</body> 208</html>