CentOS7でsshを使う
2015/12/21
CentOS7インストール直後からSSH接続は可能ですが、セキュリティをより強固に設定していきたいと思います。
環境
ルートでのログイン禁止
一般ユーザーのみログインを許可し、ルートでのログインを禁止します。
vim /etc/ssh/sshd_config
PermitRootLogin yes
↓
PermitRootLogin no
鍵認証によるログインの許可
RSAAuthentication RSA認証の許可するかどうか。yes / no で設定。
PubkeyAuthentication 公開鍵認証の許可するかどうか。yes / no で設定。
AuthorizedKeysFile 公開鍵の保存場所を指定。
vim /etc/ssh/sshd_config
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
↓
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
鍵作成
ルートではなく、ログインしたいユーザーになります。
su - daichan
ホームディレクトリに移動
cd /home/daichan
鍵作成のコマンド実行
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/daichan/.ssh/id_rsa):←鍵のできる場所。特に変更なければEnter
Created directory '/home/daichan/.ssh'.
Enter passphrase (empty for no passphrase):鍵のパスワード。不要な人はEnter
Enter same passphrase again:
Your identification has been saved in /home/daichan/.ssh/id_rsa.
Your public key has been saved in /home/daichan/.ssh/id_rsa.pub.
The key fingerprint is:
14:0e:86:07:ed:e3:e3:9b:fa:01:33:9e:9c:a0:7b:68 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .+o . |
| ..oo . |
| o o |
| o. |
| . +. .S |
| . + *o |
|.. =... |
|.E. .o |
|o. .o+. |
+-----------------+
以上で、公開鍵と秘密鍵が作成されました。
公開鍵(id_rsa.pub)の名前を変更します。
mv /home/daichan/.ssh/id_rsa.pub /home/daichan/.ssh/authorized_keys
.ssh authorized_keysの権限変更
chmod 700 /home/daichan/.ssh
chmod 600 /home/daichan/.ssh/authorized_keys
接続確認
id_rsaをローカル環境に持って来ます。(ftp,scpなどで)
teratermなどからログインします。
接続できたら、サーバにある秘密鍵は削除しましょう
rm /home/daichan/.ssh/id_rsa
パスワード認証の禁止
鍵認証でログインができるようになれば、パスワードのみでのログインを禁止します。
vim /etc/ssh/sshd_config
PasswordAuthentication yes
以上で作業は完了です。