はじめに
CentOSサーバのsshポート番号を22番から別の番号(今回の例では60022番)へ変更するワンライナーです。ワンライナーといいつつ長いですが、以下を実行しますと、CentOSサーバのsshログイン時のポート番号を22番から60022番へ変更出来ます。
SSH_PORT=60022 && cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y%m%d_%H%M%S"` && sed -i "s/^#Port 22/Port ${SSH_PORT}/g" /etc/ssh/sshd_config && cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables.`date +"%Y%m%d_%H%M%S"` && sed -i "s/tcp --dport 22/tcp --dport ${SSH_PORT}/" /etc/sysconfig/iptables && /etc/init.d/sshd restart && /etc/init.d/iptables restart
免責事項
本内容に書かれているコマンドを使用して発生した問題につきまして、当方は一切責任を負いません。恐れ入りますが、コマンドをご利用される場合は自己責任でお願い致します。
概要
CentOSをはじめLinuxサーバのセキュリティを高める為、ssh接続を受け付けるポート番号をデフォルトの22番から別の番号へ変更したいという事がよくあると思います。
CentOSサーバにrootユーザでログインし、このワンライナーを実行すると、以下のようにサーバへのssh接続時のポートを22番から60022番へ変更可能です。
sshポート変更時の詳細な動作確認手順については、後述をご参照下さい。
# SSH_PORT=60022 && cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y%m%d_%H%M%S"` && sed -i "s/^#Port 22/Port ${SSH_PORT}/g" /etc/ssh/sshd_config && cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables.`date +"%Y%m%d_%H%M%S"` && sed -i "s/tcp --dport 22/tcp --dport ${SSH_PORT}/" /etc/sysconfig/iptables && /etc/init.d/sshd restart && /etc/init.d/iptables restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
#
# grep 60022 /etc/ssh/sshd_config
Port 60022
#
# grep 60022 /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 60022 -j ACCEPT
#
ワンライナー実行を試したサーバ
CentOS 6.3インストール直後のサーバを対象にして、ワンライナーによるsshポート番号変更を確認しました。
・CentOS release 6.3 (2.6.32-279.el6.x86_64)
sshポート変更前の状態例
sshポート変更前のサーバの状態が以下のようになっているとします。
[root@example-CentOS-6-3 ~]# cat /etc/redhat-release
CentOS release 6.3 (Final)
[root@example-CentOS-6-3 ~]# grep Port /etc/ssh/sshd_config
# Port 22
# GatewayPorts no
[root@example-CentOS-6-3 ~]# grep 22 /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
[root@example-CentOS-6-3 ~]#
[root@example-CentOS-6-3 ~]# cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables.ORG
[root@example-CentOS-6-3 ~]# diff /etc/sysconfig/iptables /etc/sysconfig/iptables.ORG
[root@example-CentOS-6-3 ~]#
[root@example-CentOS-6-3 ~]# cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.ORG
[root@example-CentOS-6-3 ~]# diff /etc/ssh/sshd_config /etc/ssh/sshd_config.ORG
[root@example-CentOS-6-3 ~]#
初期状態の/etc/sysconfig/iptablesは以下のようになっているとします。
[root@example-CentOS-6-3 ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@example-CentOS-6-3 ~]#
初期状態ではsshで22番ポートへ接続出来る事を確認します。
[root@example-CentOS-6-3 ~]# ssh root@[sshポート番号変更サーバのIPアドレスを指定] -p 22
root@[sshポート番号変更サーバのIPアドレス]'s password:
初期状態ではsshで60022番ポートへ接続出来ない事を確認します。
[root@example-CentOS-6-3 ~]# ssh root@[sshポート番号変更サーバのIPアドレスを指定] -p 60022
ssh: connect to host [sshポート番号変更サーバのIPアドレス] port 60022: Connection refused
[root@example-CentOS-6-3 ~]#
sshポート変更コマンド実行例
sshポート番号を変更したCentOSサーバにrootユーザでログインします。以下のようにコマンドを実行します。
[root@example-CentOS-6-3 ~]# SSH_PORT=60022 && cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y%m%d_%H%M%S"` && sed -i "s/^#Port 22/Port ${SSH_PORT}/g" /etc/ssh/sshd_config && cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables.`date +"%Y%m%d_%H%M%S"` && sed -i "s/tcp --dport 22/tcp --dport ${SSH_PORT}/" /etc/sysconfig/iptables && /etc/init.d/sshd restart && /etc/init.d/iptables restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
[root@example-CentOS-6-3 ~]#
sshポート変更後の動作確認
sshポート番号を変更したサーバについて、sshポート番号が22番から60022番へ変わった事を確認する場合の例です。
[root@example-CentOS-6-3 ~]# diff /etc/sysconfig/iptables /etc/sysconfig/iptables.ORG
10c10
< -A INPUT -m state --state NEW -m tcp -p tcp --dport 60022 -j ACCEPT
---
> -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
[root@example-CentOS-6-3 ~]#
[root@example-CentOS-6-3 ~]# diff /etc/ssh/sshd_config /etc/ssh/sshd_config.ORG
13c13
< Port 60022
---
> #Port 22
[root@example-CentOS-6-3 ~]#
[root@example-CentOS-6-3 ~]# grep 60022 /etc/ssh/sshd_config
Port 60022
[root@example-CentOS-6-3 ~]# grep 60022 /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 60022 -j ACCEPT
[root@example-CentOS-6-3 ~]#
sshで22番ポートへ接続出来ない事を確認します。
[root@example-CentOS-6-3 ~]# ssh example@[sshポート番号変更サーバのIPアドレスを指定] -p 22
ssh: connect to host [sshポート番号変更サーバのIPアドレス] port 22: Connection refused
[root@example-CentOS-6-3 ~]#
sshで60022番ポートへ接続出来る事を確認します。
[root@example-CentOS-6-3 ~]# ssh example@[sshポート番号変更サーバのIPアドレスを指定] -p 60022
example@[sshポート番号変更サーバのIPアドレス]'s password:
リモートサーバからsshポート番号変更したサーバに対して、22番をsshログイン60022番を指定してsshログイン出来る事を確認します。
[user@example-ssh-client ~]$ ssh example@[sshポート番号変更サーバのIPアドレスを指定] -p 22
ssh: connect to host XXX.XX.XX.XXX port 22: No route to host
[user@example-ssh-client ~]$
[user@example-ssh-client ~]$ ssh example@[sshポート番号変更サーバのIPアドレスを指定] -p 60022
example@172.31.13.148's password:
[example@example-CentOS-6-3 ~]$
[example@example-CentOS-6-3 ~]$ id
uid=502(example) gid=502(example) groups=502(example) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[example@example-CentOS-6-3 ~]$
以上になります。