LoginSignup
12
24

More than 5 years have passed since last update.

Raspberry Piを使ってファイルサーバーを立てる

Last updated at Posted at 2017-07-16

備忘録。(参考)

環境

  • Raspberry Pi3 Model B
    • raspbian jessie 4.9.35-v7+ #1014
  • MicroSD 8GB
  • AirMac Timecapsule

手順

Sambaのインストールと設定

sudo apt-get install samba

smb.confの設定

sudo vim /etc/samba/smb.conf

[global]に以下を追記します。

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

interfacesのIP(192.168.1.)およびIF名(wlan0)は環境の構成に合わせて変更してください。
今回はRaspberry Pi本体が無線LANに接続しているのでwlan0を指定しています。(IF名はifconfigで確認してください。)
hosts allowの記述は手元のMacがIPv6で接続しようとしたらしく、それが原因で弾かれていたようなのでfe80::/10を追記しました。

次にsmb.confの末尾に以下を追記します。

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

force userのユーザーは適宜Raspberry Piで使用しているログインユーザー名に変更してください。

共有ディレクトリの作成

sudo mkdir /home/samba
sudo mkdir /home/samba/public
sudo chown pi /home/samba/public

chownのユーザーは適宜Raspberry Piで使用しているログインユーザー名に変更してください。

Sambaの再起動

参考サイトではserviceを使っていますが、Raspbianではうまくいかないのでsystemctlを使用して個別に再起動してください。

sudo systemctl restart smbd
sudo systemctl restart nmbd

ufwのインストールと設定(任意)

sudo apt-get install ufw

ufwの設定

インターネットに接続しているのにファイアウォールを設定していないというノーガード状態だったのでついでに実施しました。
(同一ネットワーク内からのアクセスのみ許可。)

ufw allow from 192.168.1.0/24 to any port 137
ufw allow from 192.168.1.0/24 to any port 138
ufw allow from 192.168.1.0/24 to any port 39
ufw allow from 192.168.1.0/24 to any port 445

homebridgeなど、他にポートを使っている機能があればallowするのを忘れずに!

ufw allow from 192.168.1.0/24 to any port 22

また、下記のように再設定用のシェルを用意しておくと楽かもしれません。

reconfig_ufw.sh
#!/bin/sh
# 初期化
ufw reset
# SSHの許可
ufw allow from 192.168.1.0/24 to any port 22
# Sambaの許可
ufw allow from 192.168.1.0/24 to any port 137
ufw allow from 192.168.1.0/24 to any port 138
ufw allow from 192.168.1.0/24 to any port 39
ufw allow from 192.168.1.0/24 to any port 445
# Homebridgeの許可
ufw allow from 192.168.1.0/24 to any port 51826
# 定義再読み込み
ufw enable
ufw reload
pi@raspberrypi:~ $ sudo ./script/reconfig_ufw.sh 
Resetting all rules to installed defaults. This may disrupt existing ssh
connections. Proceed with operation (y|n)? y
Backing up 'user6.rules' to '/lib/ufw/user6.rules.20170717_050548'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20170717_050548'
Backing up 'user.rules' to '/lib/ufw/user.rules.20170717_050548'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20170717_050548'
Backing up 'before.rules' to '/etc/ufw/before.rules.20170717_050548'
Backing up 'after.rules' to '/etc/ufw/after.rules.20170717_050548'

Rules updated
Rules updated
Rules updated
Rules updated
Rules updated
Rules updated
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
Firewall reloaded

ufwの再起動

sudo systemctl restart ufw

接続

Macの場合

  1. Finderを開いてcommand + K
  2. サーバーアドレスにsmb://Raspberry PiのIPを入力して「接続」をクリック。(Avahiが入っているのならばsmb://raspberrypi.localでもOK)
  3. ユーザー認証を求められるので「ゲスト」を選択
  4. ディレクトリが表示されるので「public」を選択
  5. ディレクトリが表示されたらOK

Windowsの場合

  1. Win + Eでエクスプローラーを開く
  2. 「ネットワーク」を選択
  3. 「RASPBERRYPI」をダブルクリック
  4. 「public」をダブルクリック
  5. フォルダの中身が表示されればOK

転送速度

Rasberry Piの接続形式がIEEE802.11b/g/nなので転送速度は1.2MB/s程度が限界のようです。
まあメインはAirMac側のHDDなので十分ではありますが。
Raspberry Piのボードが11ac/USB3に対応したらUSB-HDD/SSDを繋げて本格的なNASも作れそうです。

12
24
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
12
24