VSP SSHのポート番号を変更
- 環境
- VSP SSHのポート番号を変更
- 手順
環境
VSP SSHのポート番号を変更
- SSH設定ファイル変更
- /etc/ssh/sshd_config(SSHの設定)ファイル編集
- ファイアウォール設定ファイル変更
- /usr/lib/firewalld/services/ssh.xmlファイルも編集
手順
1.ファイルのバックアップ (SSHの設定)
フォルダ移動してファイルをコピー
[root@□□-□□□□ ~]# cd /etc/ssh [root@□□-□□□□ ssh]# cp sshd_config sshd_config.old
3.sshのポートを変更する
ポート番号:未使用で動的・プライベート ポート番号 (49152–65535)から選ぶこと。
他のウェルノウンポート番号 (0–1023) 登録済みポート番号 (1024–49151)はNG
4.新旧ファイルを比較して編集箇所の確認
[root@□□-□□□□ ~]# diff /etc/ssh/sshd_config.old /etc/ssh/sshd_config 17c17 // ←17行目と7行目 < #Port 22 //←旧ファイルの相違行 --- > Port XXXXX //←新ファイルの相違行 [root@□□-□□□□ ~]#
5.構文確認
[root@□□-□□□□ ~]# sshd -t
非表示でOK!
6.ファイルのバックアップ (ファイヤーウォールの設定)
設定ファイルをコピーしてフォルダ移動してコピー
[root@□□-□□□□ ~]# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ [root@□□-□□□□ ~]# cd /etc/firewalld/services/ [root@□□-□□□□ services]# cp ssh.xml ssh.xml.org
7.設定ファイルのポート番号を変更する
Vim編集
[root@□□-□□□□ services]# vim /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="22"/> //※←のポート番号編集 </service>
8.新旧ファイルを比較して編集箇所の確認
[root@□□-□□□□ services]# diff ssh.xml ssh.xml.org 5c5 // ←17行目と7行目 < <port protocol="tcp" port="22"/> //←旧ファイルの相違行 --- > <port protocol="tcp" port="XXXXX"/> //←新ファイルの相違行 [root@□□-□□□□ services]#
11.sshdで新ポート番号でログインできるか確認
A.IPアドレス入力
[root@□□-□□□□ services]# ssh -p ポート番号 ユーザー名@XXX.XXX.XXX.XXX ←VPSのIPV4のIPアドレス
※下記のように聞かれたら「yes」入力
The authenticity of host '[XXX.XXX.XXX.XXX]:XXXXX ([XXX.XXX.XXX.XXX]:XXXXX)' can't be established. ECDSA key fingerprint is XXXXXXXXXXXXXXXXXXXXXXXXXXXX ECDSA key fingerprint is XXXXXXXXXXXXXXXXXXXXXXXXXXXX Are you sure you want to continue connecting (yes/no)? yes //←ここで「yes」入力
B.ユーザーパスワードを入力
Warning: Permanently added '[XXX.XXX.XXX.XXX]:XXXXX)' (ECDSA) to the list of known hosts. ユーザー名@XXX.XXX.XXX.XXX's password:
C.ログイン確認
下記表示でログイン成功!
Last login: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX SAKURA Internet [Virtual Private Server SERVICE] [ユーザー名@□□-□□□□ ~]$
12.sshdで旧ポートでログイン拒否を確認
[root@□□-□□□□ services]# ssh -p 22 ユーザー名@XXX.XXX.XXX.XXX ←VPSのIPV4のIPアドレス
ログイン許可でOK!
ssh: connect to host XXX.XXX.XXX.XXX port 22: Connection refused