Contents
はじめに
ISOイメージからインストールしたCentOS7.4にSSHの設定をした時の備忘録です。
OSインストール直後でユーザ登録までしていることを想定しています。
SSH鍵認証の設定
SSHログインを公開鍵認証のみに設定します。
パスワード認証の確認
デフォルトで有効になっていると思います。ConoHa 管理画面のコンソール操作になります。
/etc/ssh/sshd_config PasswordAuthentication yes
MacからSSHでログイン
管理画面のコンソールではやはり勝手が悪いのでSSHでログインして操作します。サーバのインターフェースがアップしていることを忘れずに。
$ ssh ユーザ名@VPSのIPアドレス
公開鍵と秘密鍵の作成
途中で表示されるメッセージは全てデフォルト設定にするため、全てEnterキー入力。
$ ssh-keygen -t rsa -b 2048 Generating public/private rsa key pair. Enter file in which to save the key (/home/*****/.ssh/id_rsa): Created directory '/home/*****/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/*****/.ssh/id_rsa. Your public key has been saved in /home/*****/.ssh/id_rsa.pub. The key fingerprint is: SHA256:***************************************** *****@***** The key's randomart image is: +---[RSA 2048]----+ | o*+.| | .+.o.| | o E .. | | o + Bo | | S + Ooo. | | * B *+ | | o B B++ | | . + +o++. | | . o++*= | +----[SHA256]-----+
公開鍵の置き換え
公開鍵をauthorized_keysファイルに格納します。
$ cd ~/.ssh/ $ cat id_rsa.pub >> authorized_keys $ chmod 600 authorized_keys
秘密鍵の取得
サーバから一度ログアウトして、Macから取りに行く感じでコピー。
$ scp ユーザ名@VPSのIPアドレス:~/.ssh/id_rsa ~/.ssh/id_rsa_cnh
SSH鍵認証ログイン
コピーした秘密鍵を使ってログインできるか確認。
$ ssh -i ~/.ssh/id_rsa_cnh ユーザ名@VPSのIPアドレス
SSHの設定変更
サーバ側でSSHログインを鍵認証のみに変更する。(ついでにrootのSSHログイン禁止と空パスワード禁止)
# vi /etc/ssh/sshd_config PermitRootLogin no PasswordAuthentication no PermitEmptyPasswords no
SSH設定の反映
反映後、Mac等からパスワード認証でSSHログインできないことを確認する。
# systemctl restart sshd.service
SSHポートの変更
セキュリティ的な効果はほとんどないかもしれませんが、それでも無駄なログイン履歴はなくなりますし。
sshdの設定変更
使用可能なポートは事前に確認しておくこと。
# vi /etc/ssh/sshd_config
#Port 22
Port *****
反映
# systemctl restart sshd
firewalldのSSHポート確認
デフォルトは22です。
firewall-cmd --info-service ssh ssh ports: 22/tcp protocols: source-ports: modules: destination:
firewalldのSSHポート変更
firewalldのデフォルトのテンプレートは/usr/lib/firewalld/services/配下に保存されており、サービスの追加や変更を行う場合はデフォルトのテンプレートを弄らず、ファイルを/etc/firewalld/services/配下にコピーして変更します。ポートのところはタグの部分をコピーして、複数指定することもできます。
# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh.xml
# vi /etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="*****"/>
</service>
反映
# firewall-cmd --reload
以上となります。