[CentOS][7.2]sshdの待ち受けポート変更

環境

今回の環境は、以下をインストールした状態となります。

  • さくらのVPS
  • CentOS 7.2.1511

sshdの設定

/etc/ssh/sshd_config の修正

sshdが待ち受けするポート番号の設定は「/etc/ssh/sshd_config」に記載されています。
このファイルを書き換えて、sshdを再起動することで待ち受けポートが変更できます。

同じディレクトリに「ssh_config」という似たようなファイルがありますので、間違わないように注意しましょう。
こちらは、sshクライアントが使用する設定ファイルになります。

いきなり変更してしまうと、ログインできなくなってしまうかもしれません。
そうなると非常に困るので、まずは通常の22ポートでの待ち受け設定はそのままに、新しく待ち受けさせたいポートの設定を追加し、ログインできることを確認してから元の待ち受けポートの設定を削除するのがいいと思います。

sshd_configのディフォルト状態は以下のような感じでした。


# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Ciphers and keying
#RekeyLimit default none

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

初期状態では、「# Port 22」(17行目)となっています。
以下のような形でこれをアンコメントしておきつつ、新しく待ち受けさせたいポート番号(以下のサンプルでは「65022」を指定)も追加しておきます。

# vi /etc/ssh/sshd_config

(17行目)
# Port 22
↓
Port 22
Port 65022

このように記述すると、22ポートと65022ポートの2つで待ち受けできます。
もちろん、22ポートはコメントアウトした状態にして、65022ポートのみで待ち受けすることもできます。

sshd_config の修正は以上です。

sshdの再起動

sshd_config を修正したら、sshdプロセスを再起動して修正内容を反映します。

# ps -ef | grep sshd | grep -v grep
root  14693   1  0 00:42 ?  00:00:00 /usr/sbin/sshd -D
# systemctl restart sshd
# ps -ef | grep sshd | grep -v grep
root  14941   1  0 01:05 ?  00:00:00 /usr/sbin/sshd -D

restart後に、プロセスID(左から2カラム目の数字)が変わっていることを確認しておきましょう。

接続確認

sshd_configの修正・反映が終わったら、新しい待ち受けポートで接続できるか確認してみましょう。

もし、ネットワークの経路上でアクセス制限を行っている場合は、新しい待ち受けポートへの通信を許可しておく必要がありますので、注意が必要です。

ここでは、新しい待ち受けポートへの通信は許可されていると過程して、localhostでの接続確認を行ってみます。

$ ssh -l ユーザ名 -p 22 localhost
ユーザ名@localhost's password:
Last login: Sun Dec 11 02:14:45 2016 from localhost

SAKURA Internet [Virtual Private Server SERVICE]

$ exit
logout
Connection to localhost closed.
$
$ ssh -l ユーザ名 -p 65022 localhost
ユーザ名@localhost's password:
Last login: Sun Dec 11 02:14:45 2016 from localhost

SAKURA Internet [Virtual Private Server SERVICE]

$

旧待ち受けポートの設定削除

新しい待ち受けポートでssh接続できることを確認したら、古い待ち受けポートの設定はコメントアウトしておきます。

# vi /etc/ssh/sshd_config

(17行目)
Port 22
Port 65022
↓
# Port 22
Port 65022

sshd_configを修正したら、sshdを再起動しておきましょう。

# ps -ef | grep sshd | grep -v grep
root  14693  1  0 00:42 ?  00:00:00 /usr/sbin/sshd -D
# systemctl restart sshd
# ps -ef | grep sshd | grep -v grep
root  14941  1  0 01:05 ?  00:00:00 /usr/sbin/sshd -D

これで、sshdの待ち受けポートの変更は完了です。

スポンサーリンク

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

スポンサーリンク