Ikkyu's Tech Blog

技術系に関するブログです。

Raspberry PiでNASを作る

[更新] TimeMachineがうまく動作しなかったことなどあり、やり直してみました。そこで色々と変更や追記を行いました!(2020/03/30)

この記事のターゲットは、ある程度ITの知識を持っている人向けに書いています。
わかりにくい部分あれば、お手数ですが他の記事で補完してください。🙇‍♂️


先日タイムセールでRaspberry Piが安くなっていたので、購入しました。
何に使おうかと悩んでいたけど、Mac Miniとかのバックアップとりたいなって思っていたので、Raspberry Piを使ってNASを作成することにしました。

ということで、どうやったのか紹介しようと思います。

やりたいこと

  • Time Machineとして利用したい
  • ファイルサーバとして利用したい

用意したもの

Mac

Raspberry Pi セット

まとめてセットで買った方が楽だったので、こちらを購入

SSD

SSDでやってみたかったので、こちらを購入

その他

  • 有線キーボード
  • 有線マイク
  • モニター

用意したものは以上です。

OSインストール

セットについてるmicroSDにNOOBSがインストールされていたので、それを使いRaspbian Buster with desktop and recommended softwareをインストールしました。

その他の方法として、公式のダウンロードページからイメージを取得して、microSDに書き込む必要があります。

やり方はここを見たり、

www.raspberrypi.org


[追記]

Raspberry Pi Imagerって便利なもんあるんですね!シンプルで使いやすくとても便利です👏


OSがインストールされたら、ラズパイを起動します。

設定

Welcome to Raspberry Pi

  1. Set Country ... Japanを選択してNextボタンを押下
  2. Change Password ... 任意のパスワードを入力してNextボタンを押下
  3. Set Up Screen ... 画面周りの黒色のふちが見えてる人はチェックしてNextボタンを押下
  4. WiFi ... 使用するWiFiを選んでパスワードを入力してNextボタンを押下
  5. Update Software ... そのままNextボタンを押下
  6. Setup Complete ... Restartを押下

再起動したら今度はターミナルを開いて作業をします。

ターミナルでの作業

vimをインストール

設定ファイルを変更する時にvi慣れてなく辛いのでvimをインストールします!!なくていい人はスキップしてください。

$ sudo apt install vim

Raspberry PiIPアドレスを固定

routeコマンドを使用して、ゲートウェイIPアドレスを調べます。
以下、ゲートウェイのIPを192.168.86.1とします。

$ route -n

ipコマンドを使用して、現在ラズパイの無線に割り当てられているIPアドレスを調べます。
以下、ゲートウェイのIPを192.168.86.23とします。

$ ip a

新しいターミナルを開いてdhcpcd.confを変更します。1

$ sudo vim /etc/dhcpcd.conf

今回は無線のみ使用します。このような設定を追加しました。 # Example static Ip configuration:以下を変更します。

# Example static Ip configuration:
interface wlan0
# static ip6_address...
static ip_address=192.168.86.23/24 # ラズパイのIP
static routers=192.168.86.1 # ゲートウェイのIP
static domain_name_servers=192.168.86.1 # ゲートウェイのIP

設定追加後、再起動します。

$ reboot

設定したIPアドレスが割り振られているか確認します。上の設定を行なった場合、wlan0のIPアドレスがに192.168.86.23になっていればOKです。

$ ip a

sshの設定

IPアドレスが固定できたので、sshの設定を行います。

www.raspberrypi.org

デフォルトではsshは無効になっているので、以下のコマンドを実行して有効にします。

$ sudo systemctl enable ssh
$ sudo systemctl start ssh

PCからRaspberryPiを操作して設定ができるようになりました🎉

それでは次にRaspberry Piのセキュリティ設定を行います🔑

セキュリティ設定

以下の記事を参考にしました。

qiita.com

rootのパスワード変更
$ sudo passwd root
ユーザを追加

初めからあるpiユーザは削除しますので、新しいユーザを作成します。

$ sudo adduser ユーザ名

ユーザを作成する時色々聞かれますが、必要があれば入力して下さい。

追加したユーザにpiユーザと同じグループ権限を追加

piユーザのgroupを確認します。

$ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users netdev input spi gpio

追加したユーザにpiユーザのグループを追加します。

$ sudo usermod -G pi,adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,netdev,input,spi,gpio ユーザ名

グループが追加されているか確認します。

$ groups ユーザ名
Rootユーザのオートログインを無効化
$ sudo vim /etc/lightdm/lightdm.conf

autologin-user=piコメントアウトします。126行目ぐらい

$ sudo vim /etc/systemd/system/autologin@.service 

26行目ぐらいにの ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM
ExecStart=-/sbin/agetty --autologin ユーザ名 --noclear %I $TERM
に変更します。

その後、再起動してください。

$ reboot
piユーザを削除

先ほど作成したユーザでログインして、piユーザを削除します。

$ sudo userdel -r pi

もしuserdel: user pi is currently used by process 525が出た場合、PIユーザがログインしているようです。自動ログイン設定されているため、設定をオフします。

  1. sudo raspi-config設定ツールを起動
  2. 3 Boot Opstions -> B1 Desktop / CLI -> B4 Desktop Autologin Desktop GUI, automatically logged in as 'ユーザ名' user
  3. finishで再起動されます

削除されたか確認します。

$ id -a pi

id: pi: no such user
sshのポート番号を変更

デフォルトは22ですが、攻撃されやすいので、ポート番号を変更します。

$ sudo vim /etc/ssh/sshd_config 

ポート番号は「49152〜65535」から選べます。
13行目あたりに#Port 22とあるので、コメントを外してポート番号を設定します

Port 50505

SSHを再起動します。

$ sudo /etc/init.d/ssh restart

SSHで接続できるか確認します。

$ ssh ユーザ名@192.168.86.23 -p 50505

ユーザ名@raspberrypi:~ $

これでセキュリティ設定は終わりです👌

SSDext4にフォーマット&パーミッション作成

さてここからsshで作業を行います。

以下の記事を参考にしました。

RaspberryPi3にUSBメモリを付けてストレージとして使用する : DevLife 【 fdisk 】コマンド――パーティションを作成、削除する:Linux基本コマンドTips(187) - @IT fdiskを学ぼう!~拡張、論理パーティション編~ - JTP Technology Port - 日本サード・パーティ株式会社

パーティションはTime Machine用(500G)とファイルサーバ用(残り)に2つ作成します。

SSDをアンマウントする

SSDを接続し、ディスクを確認します。

$ df -h

ファイルシス   サイズ  使用  残り 使用% マウント位置
...
/dev/sda1        894G   39M  894G    1% /media/ユーザ名/SSD名

接続されています。
このままだとフォーマットに失敗するので、アンマウントします。

$ umount /media/ユーザ名/SSD名

その後df -hで確認し、アンマウントされていることを確認します。

ext4にフォーマット

/dev/sdaext4でフォーマットします。

sudo mkfs.ext4 /dev/sda
パーティションを作成

次にパーティションを作成します。

$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n # パーティション作成
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): e # 拡張パーティション
Partition number (1-4, default 1):
First sector (2048-1875385007, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1875385007, default 1875385007):

Created a new partition 1 of type 'Extended' and of size 894.3 GiB.

Command (m for help): n # パーティション作成
All space for primary partitions is in use.
Adding logical partition 5 # 論理パーティションを作成
First sector (4096-1875385007, default 4096):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4096-1875385007, default 1875385007): +500G # Time Machine用に500G

Created a new partition 5 of type 'Linux' and of size 500 GiB.

Command (m for help): n # パーティション作成
All space for primary partitions is in use.
Adding logical partition 6 # 論理パーティションを作成
First sector (1048582144-1875385007, default 1048582144):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1048582144-1875385007, default 1875385007):

Created a new partition 6 of type 'Linux' and of size 394.3 GiB. # 残りはバックアップ用

Command (m for help): p # 確認
Disk /dev/sda: 894.3 GiB, 960197124096 bytes, 1875385008 sectors
Disk model: SSPH-UA
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7d6bca42

Device     Boot      Start        End    Sectors   Size Id Type
/dev/sda1             2048 1875385007 1875382960 894.3G  5 Extended
/dev/sda5             4096 1048580095 1048576000   500G 83 Linux
/dev/sda6       1048582144 1875385007  826802864 394.3G 83 Linux

Command (m for help): w # OKなら書き込み
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

2つパーティションを作成しました。

  • /dev/sda5 ... Time Machine用,
  • /dev/sda6 ... ファイルサーバ用
パーティションごとにフォーマット
$ sudo mkfs.ext4 /dev/sda5
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 131072000 4k blocks and 32768000 inodes
Filesystem UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
    102400000

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

$ sudo mkfs.ext4 /dev/sda6
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 103350272 4k blocks and 25888032 inodes
Filesystem UUID: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
    102400000

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

Filesystem UUIDはマウント時に必要になりますので、メモしておきます。

メモしなくてもblkidでUUIDを確認できます。

www.atmarkit.co.jp

マウント

マウントするディレクトリを作成します。

$ sudo mkdir /time-machine
$ sudo chown ユーザ名:ユーザ名 /time-machine
$ sudo mkdir /public
$ sudo chown ユーザ名:ユーザ名 /public

マウントの設定を行います。

$ sudo vim /etc/fstab

以下の二行を追加します。

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /timemachine   ext4 defaults,noatime 0 0

UUID=yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy /public   ext4 defaults,noatime 0 0

一度rebootします。

$ sudo reboot

マウントされているか確認します。

$ df -h

ファイルシス   サイズ  使用  残り 使用% マウント位置
...
/dev/sda6        388G   73M  368G    1% /public
/dev/sda5        492G   73M  467G    1% /time-machine

マウントできました!

Samba

以下の記事を参考にしました。

ideal-reality.com wiki.samba.org www.samba.gr.jp

Sambaのインストール

$ sudo apt install samba

versionを確認

$ smbd -V
Version 4.9.5-Debian

Sambaにユーザを登録

$ sudo smbpasswd -a ユーザ名

パスワードを入力して、ユーザを登録します。

Sambaの設定ファイルを変更

sudo vim /etc/samba/smb.conf

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

min protocol = SMB2
vfs objects = catia fruit streams_xattr  
fruit:metadata = stream
fruit:model = MacSamba
fruit:posix_rename = yes 
fruit:veto_appledouble = no
fruit:wipe_intentionally_left_blank_rfork = yes 
fruit:delete_empty_adfiles = yes

末尾に以下の設定を追加します。

[public]
   comment = Public
   path = /public
   read only = no

[timemachine]
   comment = Time Machine
   path = /time-machine
   read only = no
   vfs objects = catia fruit streams_xattr
   fruit:time machine = yes
   fruit:time machine max size = 500G

Sambda再起動

sudo service smbd restart

フォルダに接続

  1. Finder -> メニューバーの「移動」 -> 「サーバへ接続...」を選択します。
  2. smb://192.168.86.23と入力し、接続ボタンを押します。
  3. ユーザ名とパスワードを入力し接続します。
  4. マウントするボリュームを聞かれますので「public」と「time-machine」を選択します。

あとはTimeMachineやiPadのファイルから接続して使えます。 長くなりましたが、以上となります。✌️