悪霊にさいなまれる世界 -The Demon-Haunted World

30代独身機械系技術者が雑記/セキュリティ/資産運用なんかを書きます/PGP・GPG Key:BC884A1C8202081B19A7A9B8AB8B419682B02FF8

RaspberryPi(初期モデル 1 Model B)初期設定、Ed25519鍵使用メモ

 

今さらながらRaspberry Pi、それも長年使用している初期モデルのRaspberry Pi 1 Model B(メモリ512MB、ストレージはSDカード、USBポート2つ)を再セットアップしたので、メモがわりにセットアップの記録を残しておく。

昔togetterにまとめたものがあるけど、情報も古く更新もしにくいので記事を作成する。メモなので特に告知も無し。

Linuxとか触ったこと無いけどRaspberryPiを初期設定してTorリレーを立てる - Togetter

 

    1. Raspbian Stretch Liteを公式サイトからDL

      Download Raspbian for Raspberry Pi

    2. 母艦PCでSDカードに書き込み。書き込みツールはRufusがオススメ。

      Rufus - Create bootable USB drives the easy way

    3. ssh有効化のため、母艦PCでSDカードを読み込み、ルートに"ssh"と言う空ファイルを作成

      SSH (Secure Shell) - Raspberry Pi Documentation

    4. RaspberryPiにSDカードを挿入し起動。ルータからDHCPで割り当てられたIPアドレスを確認し、TeraTermSSH接続。ID/Passはデフォルトのpi/raspberry
    5. 最低限必要なツールをインストール。vim(エディタ)、ntpdate(時刻同期)、ntp(時刻同期デーモン)、rng-tools(ハードウェア乱数生成器)
      sudo apt install vim ntpdate ntp rng-tools -y

      Raspberry Pi/ハードウェア乱数発生器 - Berry Pie Lab

    6. セキュリティのためルートでのログオンを禁止、LAN内のみsshを許可
      sudo passwd -l root
      sudo vim /etc/ssh/hosts.allow
      sudo vim /etc/ssh/hosts.deny
      
      hosts.allowはsshd:192.168.11.0/24、hosts.denyはsshd:allを追加
    7. IPアドレスDHCP割当から固定に変更。
      sudo vim /etc/dhcpcd.conf
      下記行を追加
      interface eth0
      static ip_address=192.168.11.3/24
      static routers=192.168.11.1
      static domain_name_servers=192.168.11.1
    8. タイムゾーンを設定
      sudo raspi-config
      の4より、ASIA→TOKYOを選択。またクロックが内蔵されていないので、再起動時にNICTのNTPサーバに時刻を取りに行くように設定
      sudo vim /etc/rc.local
      に下記行を追加
      sudo ntpdate ntp.nict.jp
    9. NTPデーモンの設定。NTPはDDoSリフレクション攻撃にも使えるのでループバックでのみ使えるように設定。参照先はntpdateと同じくNICT。丸ごと書き換えるので元々の設定ファイルはリネーム。
      sudo mv /etc/ntp.conf /etc/ntp.conf.old
      sudo vim /etc/ntp.conf
      新規設定ファイルは下記サイトを参考に作成。

      Secure NTP Template - Team Cymru

      # by default act only as a basic NTP client
      restrict -4 default nomodify nopeer noquery notrap
      restrict -6 default nomodify nopeer noquery notrap
      # allow NTP messages from the loopback address, useful for debugging
      restrict 127.0.0.1
      restrict ::1
      # server(s) we time sync to
      server ntp.nict.jp
    10. SDカードの寿命を延ばすためにスワップファイルを無効にする。おまじない程度。
      sudo swapoff --all
      sudo systemctl disable dphys-swapfile
    11. ここで一旦再起動。後にパッケージ類を全てアップデートする。
      sudo apt update && sudo apt upgrade -y && sudo apt dist-upgrade -y && sudo apt autoremove -y && sudo apt autoclean -y

ここまでで最低限の設定は終了。ここからSSH接続をパスワード方式から意識高く公開鍵方式に変更する。TeraTermではおかしな挙動を示すので注意(後述)

SSH設定参考サイト:Secure Secure Shell

  1. SSHで鍵を作成。ここはセキュリティが高く処理も速いEd25519方式を指定する。ここで公開鍵のパスワードも指定。また公開鍵名を変更し保存、パーミッションを変更。

    SSH認証界☆最高☆の秘密鍵「Ed25519鍵」を生成する。GithubのSSHキーを変更する。 - Androidはワンツーパンチ 三歩進んで二歩下がる

    ssh-keygen -t ed25519
    sudo mkdir .ssh
    cd .ssh
    sudo mv id_ed25519.pub authorized_keys
    sudo chmod 700 ~/.ssh
    sudo chmod 600 ~/.ssh/authorized_keys
  2. Filezillaでアクセスし、秘密鍵"id_ed25519"を母艦PCに移動。ここでこの秘密鍵TeraTermのフォルダに入れると、なぜか認証に失敗するので適当な別フォルダに保存する。
  3. SSHの設定を変更。
    sudo vim /etc/ssh/sshd_config
    下記行を追加しルートでのログインを禁止、ポートを変更、キーファイルを指定、パスワードでのアクセスを禁止。
    PermitRootLogin no
    Port *****
    PubkeyAuthentication yes
    PasswordAuthentication no
    ChallengeResponseAuthentication no
    AuthorizedKeysFile /home/pi/.ssh/authorized_keys
    KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
    Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
    MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.com
    
  4. 変更を保存しSSHを再起動。ただしミスがあったときアクセスの方法がなくなるのでこのとき開いているTeraTermは閉じない。
    sudo systemctl restart ssh.service
    新たにTeraTermのウィンドウを開き、新しいポートでアクセス。パスフレーズではなく"RSA/DSA/ECDSA/ED25519鍵を使う"を選択し、2.で保存した秘密鍵を指定。アクセスできることを確認する。また、パスフレーズでのアクセスが通らないことも確認する。Filezillaも設定を変更する。

問題が無ければこれで初期設定は終了。

 

ここから完全に自分の設定。unboundとSoftEtherVPNサーバを入れる。追々やっていく

Unbound – 日本Unboundユーザー会

SoftEther VPN プロジェクト - SoftEther VPN プロジェクト

7.3 Linux へのインストールと初期設定 - SoftEther VPN プロジェクト

Raspberry Pi & SoftEther VPN で中国ネット規制回避 | Home Made Garbage

SoftEtherVPN超ダッシュメモその1

sudo mv softether-vpnserver-v4.25-9656-rtm-2018.01.15-linux-arm_eabi-32bit.tar.gz  /usr/local
cd /usr/local
sudo tar -xzvf softether-vpnserver-v4.25-9656-rtm-2018.01.15-linux-arm_eabi-32bit.tar.gz 
cd vpnserver
sudo make
cd /usr/local
sudo rm softether-vpnserver-v4.25-9656-rtm-2018.01.15-linux-arm_eabi-32bit.tar.gz 
./vpncmd
sudo vim /etc/init.d/vpnserver
sudo chmod 755 /etc/init.d/vpnserver
sudo apt install chkconfig -y
sudo chkconfig vpnserver on
sudo chkconfig vpnserver --list
sudo /etc/init.d/vpnserver restart

SoftEtherVPN超ダッシュメモその2(/etc/init.d/vpnserver)

#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Server
DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/subsys/vpnserver
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0