5
7

More than 3 years have passed since last update.

ぼくのラズパイ最強初期設定

Last updated at Posted at 2020-09-08

記事の構成

初めてラズパイを触る人向けに書いています。

  1. raspbianのインストール
  2. 初期設定とセキュリティ
  3. VSCodeでssh接続

0. 用意するモノ

デスクトップ HDMI マウス キーボード 電源ケーブル wi-fi(DHCP)

1. raspbianのインストール

raspbianには主に三つのバージョンが存在します

  1. Raspbian Buster Lite
    CUI(コマンドで操作)のみ。上級者用。
  2. Raspbian Buster with desktop
    GUI,CUIともに可能。初心者はこれ一択。
  3. Raspbian Buster with desktop and recommended software
    2に公式オススメのソフトウェアが入ったバージョン。

ダウンロード

国内サーバで運営されているミラーサイトからOSをダウンロードします。(私は2を選択)
lite http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_lite/images/

with desktop http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian/images/

recommended software http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_lite/images/

特別な理由がなければ日時が最新のものを選択します。
image.png
ZipFile(1.1GB)を選択します。
image.png

インストール

  1. balenaEtcherSD Card Formatterをインストールします。
  2. SD Card Formatterを開き、SDcardをフォーマットします。

    *アプリを開く際に確認画面が表示されますが恐れずに進みましょう。
    image.png

  3. Etcherを開き、先ほどダウンロードしたzipFileをSDcardに書き込みます。
    image.png
    Flash!を押します。

10分くらい待つと完了します。

出来上がったSDcard本体をアダプタから外し、raspberryPiに挿します。
画像が見えれば成功です。
image.png

セットアップに
1.set country
2.change password
3.set up screeen///画面に余白がある場合直す
4.select Wifi Network
5.update softwareと聞かれます。

2の国の設定と(画面に余白が生じている場合)3のスクリーンの設定を行ってください。
それ以外の設定はデフォルトのままnextを押してください。後にやります。

2. 初期設定とセキュリティ

デフォルトではラズパイにrootユーザ(pi)でログインします。そのままでは悪用される可能性が存在するので自分専用の管理者ユーザを作成し、rootユーザを削除しましょう。

LXTerminalを開きます。

管理者権限ユーザの作成

1.rootユーザのパスワードを更新します

LXTerminal
$ sudo passwd root
新しいパスワード: 任意のパスワード
パスワードの確認: 任意のパスワード
passwd: パスワードは正しく更新されました。

2.Userを作成します

LXTerminal
$ sudo adduser [任意のユーザー名]
作成するユーザの新しいパスワード: 任意
パスワードの確認: 任意
passwd: パスワードは正しく更新されました。
フルネーム[]:Enterkeyを押す
部屋番号[]:同上
職場電話番号[]:同上
自宅電話番号[]:同上
その他[]:同上
以上で正しいですか?[Y/n] y

3.Userに管理者権限を付与します。

LXTerminal
$ groups pi
pi : pi adm dialout cdrom sudo ... gpio  ///piUserが属しているグループ
$ sudo usermod -G pi,adm,dialout,...,gpio [新しいユーザ名]  ///新しいUserを上記のグループに追加
$ groups [ユーザ名]
[ユーザ名] :  adm dialout ...pi...gpio  ///追加できた確認

4.作成したUserでLogin

LXTerminal
$ sudo rasppi-config///設定画面を表示
3 Boot Option を選択
B1 Desktop / CLI を選択
B3 Desktop を選択
Finish を選択
///再起動しますか?と表示される。
Yes

これで再起動するたびにLoginするUserを選べるようになりました。作成したUserでログインしてください。

5.Userがパスワードなしでsudo(管理者権限で実行)を実行できるようにする

LXTermianl
$ sudo nano /etc/sudoers
///nanoで/etc/sudoersを開く///

# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
[User] ALL=(ALL) NOPASSWD: ALL ←付け加える

6.rootUserを消します。

LXTerminal
$ sudo userdel -r pi

セキュリティ

公開鍵認証を設定します。
ラズパイでの作業を止めてPCで作業をします。

1.SSH Keyの作成

本稿ではwindowsで作成した例を載せます。
Git Bashをインストールします

Gitbash
$ cd C:\Users\User\.ssh
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/User/.ssh/id_rsa): test(file)
Enter passphrase (empty for no passphrase): パスワード
Enter same passphrase again: パスワード

本稿ではtest.pubという公開鍵とtestという秘密鍵を生成しました。
秘密鍵は流出すると危険なので絶対に流出しないように保存してください。

C:\Users\User\.sshに保存された公開鍵test.pubをラズパイに送信します。
本稿ではUSBディスクに移してコピーしました。

2.SSH Keyの登録

ラズパイに戻ります。
authorized_keysの作成

LXTerminal
$ cp /[USB:path]/test.pub ~/.ssh
$ chmod 700  ~/.ssh
$ cat ~/.ssh/test.pub >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ rm ~/.ssh/test.pub

SSH serverのenabled

LXTerminal
$ sudo raspi-config
5 Interfacing Options
P2 SSH
///SSH serverを有効化しますか?
→はい
→了解
Finish

sshd_configファイルの設定

LXTerminal
$ sudo nano /etc/ssh/sshd_config

///

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

#Port 22 #変更推奨 
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin no #変更必須
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile     .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no #変更必須
PermitEmptyPasswords no #変更必須

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Kerberos options

port番号が気になる人もここで変更しましょう。unknown port : 49152 ~ 65535
ctrl + Xで保存したら、SSHの再起動をします。

LXTerminal
$ sudo /etc/init.d/ssh restart
[ ok ] Restarting ssh (via systemctl): ssh.service.

セキュリティの設定が終了です。

次にラズパイをwi-fiに接続します。

LXTerminal
sudo raspi-config
2 Network Option
N2 wi-fi
wi-fiの設定と接続をします。

ラズパイに割り振られたipアドレスを確認します。

LXTerminal
ifconfig

///
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.x.x  netmask 255.255.255.0  broadcast 192.168.x.255
///

inet 192.168.x.xがipアドレスです。

3. VSCodeでssh接続

ようやくここまで来ました。あともうひと踏ん張りです。
最初にC:\Users\User.ssh\configファイルを作成します。

\Users\User\.ssh\config
Host 192.168.x.x #ラズパイのip
  HostName 192.168.x.x
  User [user]
  IdentityFile C:\Users\User\.ssh\test(秘密鍵file)
  Port 22

PCにVSCodeをインストールします。
appを開いたら拡張機能タブからRemote-SSHをインストールします。

image.png
タブにこのマークが追加されるはずです
image.png

クリックして

image.png

+マークのadd new からipアドレスを入力するとssh接続ができるはずです。

お疲れ様でした。

参考サイト
お前らのSSH Keysの作り方は間違っている
ラズパイでやらなければいけない4つのセキュリティ対策!
Visual Studio Code で Remote SSH する。

5
7
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
5
7