LoginSignup
36
46

More than 5 years have passed since last update.

Raspberry Pi 3 ModelB で外付けHDDをファイルサーバー化する

Last updated at Posted at 2018-04-21

目的

Raspberry Pi 3 ModelBにマウントした外付けHDDを外部PCからR/Wできるようにファイルサーバー化する

環境

作業メモ

外付けHDDをマウント

参考サイト
https://qiita.com/Cinosura/items/6ab435331ea2b3671a1d

  • 外付けHDDをRaspberry PiにUSB接続して起動
  • fdiskしてHDDが認識されればマウントはOK
$ sudo fdisk -l
...中略
Device     Boot Start        End    Sectors   Size Id Type
/dev/sda1        2048 1953520064 1953518017 931.5G  7 HPFS/NTFS/exFAT
  • 再起動時に自動的にマウントされるように設定
$ sudo blkid /dev/sda1
/dev/sda1: LABEL="EC-PHU3" UUID="A6761F43761F13A1" TYPE="ntfs" PARTUUID="7a469053-01"

/etc/fstab に以下を追記
ここでは適当に作った /mnt/hdd にマウントする

UUID="A6761F43761F13A1" /mnt/hdd ntfs-3g defaults,nofail 0       0

最後に再起動して /mnt/hdd にマウントされているのが確認できればOK

外付けHDDを接続する時は使用する電源に注意!*1

外付けHDDをファイルサーバー化する

参考サイト
https://qiita.com/ARBALEST000/items/78f459567e1e90de99e5

  • ファイルサーバー化するためにsambaをインストールする
$ sudo apt-get update
$ sudo apt-get install samba
  • smb.confの設定
$ sudo vi /etc/samba/smb.conf

[global]に以下を追記

smb.conf
interfaces 192.168.0. 127.0.0.1/8 wlan0
bind interfaces only = yes
hosts allow = 192.168.0. fe80::/10
security = user

interfacesのIP(192.168.0.)およびIF名(wlan0)は環境によって異なる

次にsmb.confの末尾に以下を追記

smb.conf
[public]
  comment = Public
  path = /mnt/hdd
  public = yes
  read only = no
  browsable = yes
  force user = pi

force userのユーザーはRaspberry Piで使用しているログインユーザー名を指定
pathはマウントされたHDDのpathを指定

<2018/4/29追記>
その後、こちらの投稿で少しセキュリティ面を強化

ここまで設定したらtestparmコマンドで設定が反映されているかを確認

$ testparm
  • sambaの再起動
$ sudo systemctl restart smbd
$ sudo systemctl restart nmbd
  • ネットワーク上で RASPBERRYPI が見えるか確認

エクスプローラーで \\RASPBERRYPI と選択して見えればOK
適当にファイル作成できることも確認(書き込みできることの確認)

セキュリティ関連の設定

ファイアウォール(iptables)の設定を行う
ローカルなネットワーク内のファイルサーバーなので、正直なところ必要性は微妙
ただ、せっかくなので設定をしてみる

参考サイト
https://knowledge.sakura.ad.jp/4048/
http://wiki.samba.gr.jp/mediawiki/index.php?title=Samba_%E3%82%92%E3%83%95%E3%82%A1%E3%82%A4%E3%82%A2%E3%82%A6%E3%82%A9%E3%83%BC%E3%83%AB%E8%B6%8A%E3%81%97%E3%81%AB%E5%88%A9%E7%94%A8%E3%81%99%E3%82%8B

  • iptablesを使用する準備
$ sudo apt-get install iptables-persistent
  • 現在の設定を確認
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

全てACCEPT、つまり何も制限していない状態になっている

  • 参考サイトの内容を参考にiptablesのルールを設定する*2
$ sudo vi /etc/iptables/rules.v4
/etc/iptables/rules.v4
*filter
:INPUT   DROP   [0:0]
:FORWARD DROP   [0:0]
:OUTPUT  ACCEPT [0:0]

# loopback
-A INPUT -i lo -j ACCEPT

# for established connection
-A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

# DNS
-A INPUT -p udp --sport 53 -j ACCEPT

# SSH
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# HTTP/HTTPS
-A INPUT -p tcp --dport 80   -j ACCEPT
-A INPUT -p tcp --dport 443  -j ACCEPT

# SAMBA
-A INPUT -p tcp --dport 139 -j ACCEPT
-A INPUT -p udp --dport 137 -j ACCEPT
-A INPUT -p udp --dport 138 -j ACCEPT
-A INPUT -p tcp --dport 445 -j ACCEPT
この設定で少なくとも以下はできることを確認済み
TeraTermでのSSH接続
Windows PCからのファイルサーバーのR/W
  • 新しいルールをiptablesに適用する
$ sudo iptables-apply /etc/iptables/rules.v4
Applying new iptables rules from '/etc/iptables/rules.v4'... done.
Can you establish NEW connections to the machine? (y/N) y
... then my job is done. See you next time.

適用してOKならば、途中でyを押す

  • 設定が適用されたか確認
$ sudo iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     udp  --  anywhere             anywhere             udp spt:domain
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:netbios-ssn
ACCEPT     udp  --  anywhere             anywhere             udp dpt:netbios-ns
ACCEPT     udp  --  anywhere             anywhere             udp dpt:netbios-dgm
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:microsoft-ds

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

INPUT,FORWARD に設定が適用されている

まとめ

当初の目的は達成!

Raspberry Pi 3 ModelBにマウントした外付けHDDを外部PCからR/Wできるようにファイルサーバー化する

ただ、sambaのアクセス制限には課題あり
今の設定だとローカルネットワーク内とはいえ guest ユーザーなら誰でもR/Wできてしまうので



  1. わざわざ環境に「Raspberry Pi用電源」を書いたのはここでハマったため。平たく書くと、HDDが認識されなかったので電源を変えてみたら認識してくれました、という話 

  2. iptablesの作法についてはここでは説明しないので、他サイトを参照ください。INPUT,FORWARDDROPにしているので、全く制限してないよりは効果があるはず 

36
46
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
46