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