Linux/SSH

SSH 포트 변경 & 2개 이상 열기

GGkeeper 2021. 12. 8. 23:21

실습> 포트 변경하기

22 -> 2200

포트를 변경하기 전에는 ssh의 기본 포트번호는 22번 포트이다.
[root@server1 ~]# netstat -nltp | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      10455/sshd    
tcp6       0      0 :::22                   :::*                    LISTEN      10455/sshd    

서버의 설정파일에서 포트번호를 지정하는 Port 지시자를 변경하고 SSH 서비스를 재시작한다.

#Port 22 -> Port 2200

SELinux를 잠시 중지한다.
setenforce 0 : SELinux Off (일시적으로 중지)
setenforce 1 : SELinux On
[root@server1 ~]# setenforce 0
[root@server1 ~]# sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive  <--
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31


서버의 설정파일에서 포트번호를 지정하는 Port 지시자를 변경하고 서버를 재시작한다.
[root@server1 ~]# vi /etc/ssh/sshd_config
Port 2200
[root@server1 ~]# systemctl restart sshd

[root@server1 ~]# netstat -nltp | grep 22
tcp        0      0 0.0.0.0:2200            0.0.0.0:*               LISTEN      10564/sshd    
tcp6       0      0 :::2200                 :::*

방화벽룰을 모두 초기화한다.
[root@server1 ~]# iptables -F

Host OS에서 putty로 접속한다.
IP : 192.168.108.20
Port : 2200

로그인 성공적으로 되었다.
login as: root
root@192.168.108.20's password:
Last login: Tue Dec  7 16:27:22 2021 from 192.168.108.1
[root@server1 ~]#


실습> 포트 번호 변경하기

SELinux를 On 시킨 상태에서 이 문제를 해결한다.

-- 조건 --
1. SELinux 활성화
2. Port 2200 -> Port 2021
-- 조건 --

1. SELinux 활성화
SELinux가 비활성화 되어 있다면 활성화시킨다.
Server1# setenforce 1
Server1# sestatus
  :
Current mode:                   enforcing
  :

2. semanage 명령어 패키지 설치
semanage 명령어를 사용하기 위해서는 policycoreutils-python 패키지를 설치한다.

Server1# yum -y install policycoreutils-python

3. 포트 확인
ssh_port_t 가 사용하는 포트를 확인한다.
Server1# semanage port -l
  :
  :(생략)

Server1# semanage port -l | grep ssh_port
ssh_port_t                     tcp      22

4. 포트 등록
SELinux가 활성화된 상태에서는 포트번호를 변경하기 위해서는 semanage 명령어를 사용해서 
ssh_port_t 의 포트를 등록하고 sshd 데몬을 재시작해야 한다.

 

/etc/ssh/sshd_config 에서 17번째 줄 Port 번호를 2021로 변경

그리고 systemctl restart sshd 를 하면 오류가 난다.

 

semanage port 에 ssh_port_t 에서 새로 변경한 포트 번호를 등록해주어야 한다.

 

형식 : semanage port -a -t ssh_port_t -p tcp 포트번호

Server1# semanage port -a -t ssh_port_t -p tcp 2021
Server1# semanage port -l | grep ssh_port
ssh_port_t                     tcp      2021, 22

ssh 데몬을 재시작한다.
Server1# systemctl restart sshd

Server1# netstat -nlpt | grep 2021
tcp        0      0 0.0.0.0:2021            0.0.0.0:*               LISTEN      10630/sshd    
tcp6       0      0 :::2021                 :::*                    LISTEN      10630/sshd   

5. 접속
로컬에서 접속하기
ssh로 접속하면 기본 접속 포트는 22번으로 접속한다. 
그러므로 아래처럼 Connection refused가 발생되서 접속이 거절된다.
Server1# ssh localhost
ssh: connect to host localhost port 22: Connection refused

ssh 명령어의 옵션을 확인한다.
Server1# ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
           [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
           [user@]hostname [command]

user1 사용자를 생성하고 비밀번호를 111111 로 설정한다.
여기서는 테스트이기 때문에 파이프로 넘겨서 변경한다.
Server1# useradd user1
Server1# echo 111111 | passwd --stdin user1
아래처럼 -p 2021로 설정한 후 접속을 한다.
-p : 포트 설정

Server1# ssh -p 2021 user1@localhost
user1@localhost's password:
[user1@localhost ~]$ exit
logout
Connection to localhost closed.

Host OS에서 putty로 접속한다.
Host Name : 192.168.108.20
Port : 2021
Saved Session : 192.168.108.20:2021


포트 2개 이상 열기

포트를 2개 이상 열기 위해서는 Port 지시자를 여러번 써주면 된다.
[root@server1 ~]# vi /etc/ssh/sshd_config
-- /etc/ssh/sshd_config --
Port 2021                                                                         
Port 22
-- /etc/ssh/sshd_config --

[root@server1 ~]# semanage port -l | grep ssh
ssh_port_t                     tcp      2021, 22

[root@server1 ~]# systemctl restart sshd
[root@server1 ~]# netstat -nltp|grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      26086/sshd          
tcp        0      0 0.0.0.0:2021            0.0.0.0:*               LISTEN      26086/sshd          
tcp6       0      0 :::22                   :::*                    LISTEN      26086/sshd          
tcp6       0      0 :::2021                 :::*                    LISTEN      26086/sshd  

 

'Linux > SSH' 카테고리의 다른 글

OpenSSH 소스 삭제하기  (0) 2021.12.08
OpenSSH 소스 설치 [2]  (0) 2021.12.08
OpenSSH 소스 설치  (0) 2021.12.08
ssh 에 root 접속 금지하기  (0) 2021.12.08
sshd 설정 파일 설명 (/etc/ssh/sshd_config)  (0) 2021.12.08