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