본문 바로가기

LINUX

[리눅스] Linux SSH 특정 IP 접속 허용/차단



SSH 특정 IP 접속 허용/차단

 hosts.allow, hosts.deny설정을 통한 SSH 접속 허용/차단 방법



1. hosts.allow 접속 허용IP 추가 (예제: 192.168.0.100, 192.168.0.200)


 

  [root@localhost ~]# vi /etc/hosts.allow


  #

  # hosts.allow   This file contains access rules which are used to

  #               allow or deny connections to network services that

  #               either use the tcp_wrappers library or that have been

  #               started through a tcp_wrappers-enabled xinetd.

  #

  #               See 'man 5 hosts_options' and 'man 5 hosts_access'

  #               for information on rule syntax.

  #               See 'man tcpd' for information on tcp_wrappers

  #

  sshd: 192.168.0.100

  sshd: 192.168.0.200




2. hosts.deny 수정


 

  [root@localhost ~]# vi /etc/hosts.deny


  #

  # hosts.deny    This file contains access rules which are used to

  #               deny connections to network services that either use

  #               the tcp_wrappers library or that have been

  #               started through a tcp_wrappers-enabled xinetd.

  #

  #               The rules in this file can also be set up in

  #               /etc/hosts.allow with a 'deny' option instead.

  #

  #               See 'man 5 hosts_options' and 'man 5 hosts_access'

  #               for information on rule syntax.

  #               See 'man tcpd' for information on tcp_wrappers

  #

  sshd: ALL


 - sshd: ALL - hosts.allow에 등록된 IP를 제외한 모든 아이피 차단

 - sshd: IP - 입력된 IP 차단



3. sshd 서비스 재시작


 

  [root@localhost ~]# systemctl restart sshd        # 또는 service sshd restart




4. 접속 차단 테스트


 

  [root@denyhost ~]# ssh 192.168.0.300

  ssh_exchange_identification: read: Connection reset by peer         # 접속 불가 메세지 확인