By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 homemade-nas.html

View raw Download
text/html • 12.75 kiB
HTML document, Unicode text, UTF-8 text
        
            
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<title>
6
Cheap, 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
49
more than €120 for all the components (assuming you've got a network you can plug it into). It also
50
offers more flexibility than a commercial NAS, because you can install any software you want on it.
51
And if you already use the Raspberry Pi for something else, you can just add this to it and not
52
worry 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,
54
since 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
56
a Raspberry Pi.
57
</p><p>I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that;
58
if 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
61
the 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;
64
it'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
69
SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one
70
that 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
72
as 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
76
computer 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
86
Raspberry 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
88
certainly 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
90
have 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,
100
it 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
104
and 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>,
106
but there are other methods as well. Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu
107
Server or plain Debian. Make sure to enable SSH and preset the Wi-Fi settings if you're going
108
to 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
111
scanner. Now is a good time to set up a static IP address for the computer, forward its ports
112
and get dynamic DNS if you want to access it from the internet. <a href="https://ydns.io/">YDNS</a> is a
113
really 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>
115
client. 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 &amp;&amp; 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
119
this, 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
123
only 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
128
case) with another one, and the filesystem to match the one you formatted the drive with. Change
129
the 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
133
users can't use the SD card, and you may have some other apps to run on the server that you want
134
to 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>,
136
like <code>/etc/systemd/system/setup_storage_dirs.service</code>
137
</p><pre data-language="ini">[Unit]
138
Description=Update private user storage spaces
139
140
[Service]
141
Type=oneshot
142
ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py
143
144
[Install]
145
WantedBy=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
149
import os
150
import subprocess
151
from pathlib import Path
152
153
homes = Path("/home").glob("*")
154
storage_path = Path("/storage") # change this if you changed the mount point
155
156
for home in homes:
157
if home.is_dir():
158
user = home.name
159
storage = storage_path/user
160
bound = home/"Storage" # change this if you'd like a different name
161
if not storage.exists():
162
os.makedirs(storage)
163
os.chown(storage, home.stat().st_uid, home.stat().st_gid)
164
os.chmod(storage, 0o700)
165
if not bound.is_dir():
166
os.makedirs(bound)
167
os.chown(bound, home.stat().st_uid, home.stat().st_gid)
168
os.chmod(bound, 0o700)
169
170
# Using bind mounts; some file managers don't like symlinks
171
subprocess.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
173
users; 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)
175
because it's plug-and-play, the speed difference is negligible, it integrates with system users,
176
it'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
178
servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem,
179
it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP
180
add-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
182
GUI 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
185
classic <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
190
cases) or by finding an option to connect to a server.
191
</p><h4>Nemo (GNU/Linux)</h4><p>File &gt; Connect to server &gt; select type <code>SSH</code>
192
</p><h4>Material Files (Android)</h4><p>Menu &gt; Add storage &gt; SFTP server
193
</p><h4>Amaze (Android)</h4><p>Plus button &gt; Cloud connection &gt; 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
195
software.
196
</p><h4>Samsung phone file manager (Android)</h4><p>Storage space section &gt; Network storage space &gt; Update the app when prompted &gt; Plus &gt; 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>