안녕하세요

JP-Hosting 입니다.

오늘은 SSH 포트변경을 해보겠습니다.

리눅스에서 사용되는 SSH 기본포트는 22포트로 알려진포트입니다.

그만큼 해커로부터도 공격 위협이 있기 때문에, 다른 포트로 변경해서 사용하는 것이 보안에 좋습니다.

  • 테스트환경은 CENTOS 7.5 버전입니다.

[root@localhost ~]# rpm -qa *-release
centos-release-7-5.1804.el7.centos.2.x86_64

  • sshd_config 파일 백업 및 확인

변경전 설정파일을 백업해둡니다.


[root@localhost ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

[root@localhost ~]# cd /etc/ssh
[root@localhost ssh]# ls
moduli sshd_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
ssh_config sshd_config.backup ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub

  • sshd_config 파일 변경하기

파일중간에 주석처리되어있는 #Port 22 의 밑에

“Port 사용할포트” 를 입력해줍니다.


[root@localhost ssh]# vi sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy 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
Port 25852
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

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
  • 포트허용하기

[root@localhost ssh]# firewall-cmd --zone=public --permanent --add-port=25852/tcp
success
[root@localhost ssh]# firewall-cmd --reload
success

  • semanage로 포트 등록 및 sshd 서비스 재시작

[root@localhost ssh]# semanage port -a -t ssh_port_t -p tcp 25852
[root@localhost ssh]# systemctl restart sshd.service

  • 포트확인
[root@localhost ssh]# ss -tnlp | grep ssh
LISTEN     0      128          *:25852                    *:*                   users:(("sshd",pid=15097,fd=3))
LISTEN     0      128         :::25852                   :::*                   users:(("sshd",pid=15097,fd=4))

오늘은 sshd 포트 변경에대해 다루어 보았습니다.

기본포트를 이용하고 계시던 분들은 이번기회에 포트변경하여 해커로부터의 위협을 줄였으면 합니다.

서버문의는 홈페이지통해 부탁드립니다.

감사합니다.