[Ubuntu] 특정 IP 차단/허용
Linux에서 특정 IP에서만 ssh 접속을 할 수 있도록 설정 할 수 있음.
목표
192.168.110.90에서만 192.168.110.111 서버 ssh 접속 가능하도록 설정
1. 모든 IP 차단
# vi /etc/hosts.deny
sshd: ALL |
2. 특정 IP 허용
# vi /etc/hosts.allow
sshd: 192.168.110.90 |
3. sshd 재시작
# systemctl restart sshd
4. 다른 서버에서 접속 테스트
# ssh 192.168.110.111
kex_exchange_identification: read: Connection reset by peer #접속 불가
추가1) route 설정과 netfilter를 조작하여 차단 할 수도 있음
# route add -host DOMAIN_OR_IP reject
# route del -host DOMAIN_OR_IP reject
# iptables -A INPUT -s xxx.xxx.xxx.xxx -j DROP
# iptables -A INPUT -s xxx.xxx.xxx.xxx -j ACCEPT
# iptables -F
추가2) 차단 할 IP 목록이 많을 경우 ipset / iptables 조합으로 차단 가능
(1) ipset 설치
# apt install ipset iptables
(2) ipset 생성
ipset create countryblock nethash
ipset add countryblock 1.0.1.0/24
ipset add countryblock 1.0.2.0/23
ipset add countryblock 1.0.8.0/21
ipset add countryblock 1.0.32.0/19
ipset add countryblock 1.1.0.0/24
ipset add countryblock 1.1.2.0/23
ipset add countryblock 1.10.8.0/23
ipset add countryblock 1.202.0.0/15
ipset add countryblock 5.10.68.240/29
ipset add countryblock 5.10.70.40/30
ipset add countryblock 5.10.72.16/29
ipset create countryblock2 nethash
ipset add countryblock2 1.0.1.0/24
ipset add countryblock2 1.0.2.0/23
ipset add countryblock2 1.0.8.0/21
(3) ipset 확인
root@server1:~# ipset list
Name: countryblock
Type: hash:net
Revision: 6
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 1152
References: 0
Number of entries: 11
Members:
1.0.8.0/21
1.0.32.0/19
1.1.2.0/23
1.0.2.0/23
1.1.0.0/24
1.0.1.0/24
1.202.0.0/15
5.10.72.16/29
5.10.70.40/30
1.10.8.0/23
5.10.68.240/29
Name: countryblock2
Type: hash:net
Revision: 6
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 640
References: 0
Number of entries: 3
Members:
1.0.1.0/24
1.0.2.0/23
1.0.8.0/21
(4) iptables로 생성한 ipset 리스트 차단
# iptables -A INPUT -m set --match-set countryblock src -j DROP
(5) iptables 확인
root@server1:~# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- anywhere anywhere match-set countryblock src
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
참고)