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