Writing Appropriately

職務経歴

CentOS7 OpenSSHポート変更@お名前.comのKVM

お名前.comのKVMのCentOS7にした設定の覚書

作業

  • OpenSSH
    • ポートを22番から変更
    • 認証方式を公開鍵のみとする
    • エージェント転送の有効化
  • Firewalldポート開放

OpenSSHの設定

空いているポートの確認

ss -antup
#netstatなどはRHEL7などで非推奨
netstat -ltup4
lsof -i

sshd_configの編集

sshd_config編集

vim /etc/ssh/sshd_config
  • ポートを追加 (22は後で消すのでそのまま)
Port 22
Port 114514
  • ルートログイン無効化
PermitRootLogin no
  • パスワード認証,チャレンジレスポンス認証,GSSAPI認証無効化
PasswordAuthentication no
ChallengeResponseAuthentication no
GSSAPIAuthentication no
  • プロトコルssh2のみの記述はOpenSSH_7.4にはssh1の既に存在しないため不要
Protocol 2

sshクライアントで-vオプションを付けてプロトコル1で接続をするとわかる

debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
Protocol major versions differ: 1 vs. 2
  • エージェント転送 yes ssh-agent,pagentを使用してパススルーために必要
AllowAgentForwarding yes

設定しておくとsshでログイン時に秘密鍵もついてくる

% ssh-add -l
2048 SHA256:bckpUEWtPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX rsa-key1 (RSA)
2048 SHA256:LyE24+keTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX rsa-key2 (RSA)

Noにするとssh-agentとconnectionを開けない

% ssh-add -l
Could not open a connection to your authentication agent.
  • UsePam 公開鍵以外をnoにしているため使われないがWarningが出るためyesのまま
 UsePAM yes

sshd_configの形式が正しいかチェック

/sbin/sshd -- c

sshd_configを適応した場合の状態チェック(形式チェックもされる

/sbin/sshd -T -f /etc/ssh/sshd_config

公開鍵以外全てnoである事を確認

/sbin/sshd -T -f /etc/ssh/sshd_config | grep 'authentication ' | sort
    challengeresponseauthentication no
    gssapiauthentication no
    hostbasedauthentication no
    kbdinteractiveauthentication no
    kerberosauthentication no
    passwordauthentication no
    pubkeyauthentication yes

再起動して適用する

追加したポート114514でLISTENになっている事を確認

systemctl restart sshd.service
lsof -i | grep sshd
    sshd     17877    root    3u  IPv4  74920       0t0  TCP *:114514 (LISTEN)
    sshd     17877    root    4u  IPv6  74922       0t0  TCP *:114514 (LISTEN)

Firewalldポート開放

現在の状態確認

firewall-cmd --list-all --zone=public --permanent
    services: ssh dhcpv6-client

portを追加

設定xmlを/usr/libから/etcへコピーして編集(etc側にある方が優先される)

cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

vim /etc/firewalld/services/ssh.xml

22をそのままで114514を追加

    <port protocol="tcp" port="22"/>
    <port protocol="tcp" port="114514"/>

設定反映

firewall-cmd --reload

別ターミナルで接続テストをする

  • -vオプションを付けてAuthenticationsがpublickeyのみか確認
ssh -v -2 -p 114514 -i privatekey takeshi@xxx.xxx.xxx.xxx
  • "Authentications that can continue"を確認する(パスワードを入力する行の8行目ほど上にある GSSAPI認証のgssapi-keyex,パスワード認証のpasswordなどがpublickeyの後ろに続く場合は駄目
    debug1: Authentications that can continue: publickey
  • sudoとsuが使えるか確認
sudo vim
su - root

ポート22番を閉じる

ssh.xmlから22を削除

vim /etc/firewalld/services/ssh.xml
        <!-- <port protocol="tcp" port="22"/> -->
        <port protocol="tcp" port="114514"/>

変更を反映させる

firewall-cmd --reload

OpenSSH ポート22番を無効化

sshd_config編集

vim /etc/ssh/sshd_config
#Port22
Port114514

sshd_configの形式が正しいかチェック

/sbin/sshd -- c

再起動して適用する

systemctl restart sshd.service

確認

ターミナルはそのままもう一度ユーザーで114514で接続できるか確認