====== OpenSSH サーバのインストール ======
SSHサーバの設定を変更してセキュリティを強化する。
===== インストール環境 =====
* Ubuntu 18.04.3 LTS 64bit
* お名前.com VPS (KVM) 1GB プラン
^ パッケージ名 ^ バージョン ^
| openssh-server | 1:7.6p1-4ubuntu0.3 |
===== インストールと設定 =====
SSHサーバがインストールされていない場合はインストールする。
$ sudo apt-get install openssh-server
設定ファイルを編集する前にオリジナルの設定ファイルを保存しておく。
$ cd /etc/ssh
$ sudo cp ./sshd_config ./sshd_config.original
$ sudo chmod a-w ./sshd_config.original
/etc/ssh/sshd_config
を編集して設定を変える。
$ sudo vi ./sshd_config
==== 変更点 ====
* Port 5555
* ポートを変更してセキュリティ強化
* PermitRootLogin no
* rootの直接ログインを拒否
* PasswordAuthentication no
* パスワードのみのログインを拒否
* AllowUsers foo
* SSHログインを許可するユーザを限定
$ diff ./sshd_config.original ./sshd_config
13a14
> Port 37784
32a34
> PermitRootLogin no
56a59
> PasswordAuthentication no
123c126,131
< PasswordAuthentication yes
---
> #PasswordAuthentication yes
>
> AllowUsers foo
>
sshd を再起動して設定を有効化する。
% sudo systemctl restart sshd.service
===== ログイン用公開鍵の設置 =====
ローカルのコンピュータで SSH ログイン用の鍵を作成する。
Linux や Mac では以下のコマンドで。Windows は Putty 等を利用して作成する。
鍵長が短くても良い場合は "-b 4096" は不要。
$ ssh-keygen -t rsa -b 4096 -C "Comment of Key" -f ./ssh_key
秘密鍵から公開鍵をエクスポート。
$ ssh-keygen -y -f ./ssh_key > ./id_rsa.pub
エクスポートした公開鍵をサーバーのホームディレクトリにコピーして利用可能にする。
$ cd ~/
$ mkdir ./.ssh
$ cat ./id_rsa.pub >> ~/.ssh/authorized_keys
セキュリティのためパーミッションを変更しておく。
$ chmod 700 ./.ssh/
$ chmod 600 ./.ssh/authorized_keys
===== ファイアフォールの設定 =====
SSH 用のポートへのアクセスを許可する。
$ sudo ufw allow 5555/tcp
===== 参考 =====
* [[https://help.ubuntu.com/lts/serverguide/openssh-server.html|公式ドキュメント OpenSSH Server]]