내 컴퓨터가 다른 IP에 연결하는 기능을 어떻게 제한합니까?

내 컴퓨터가 다른 IP에 연결하는 기능을 어떻게 제한합니까?

다음과 같이 텍스트 파일에 쉼표로 구분된 IP 주소 목록이 있습니다.

192.xxx.xxx.xxx,213.www.www.www,255.yyy.yyy.yyy

Ubuntu 19가 이러한 IP 주소에 연결되지 않도록 할 수 있나요? 그렇다면 어떻게?

답변1

첫째, 패킷 필터(예: Linux iptables)는 호스트 이름이나 도메인별로 트래픽을 차단하거나 허용할 수 없습니다. 패킷 필터는 IP 주소만 인식합니다.

둘째, FirewallD는 해킹하지 않는 한 아웃바운드 트래픽을 필터링할 수 없습니다. 따라서 간단히 iptables를 사용해야 합니다.

먼저 목록에 포함된 모든 호스트 이름을 IP 주소로 확인해야 합니다. 해결해야 할 호스트 이름이 수백 또는 수천 개 있는 경우 다음 도구를 사용할 수 있습니다. http://domaintoipconverter.com/index.php

그런 다음 IP 주소 목록을 파일(예: list.txt)에 저장합니다.

$ cat list.txt
10.20.20.2
8.8.8.8
1.1.1.1
1.2.3.4
8.8.4.4

그런 다음 간단한 스크립트를 사용하여 방화벽 규칙에 IP 주소를 추가합니다.

레드햇/CentOS

#!/bin/bash
# Stop and disable firewalld
systemctl stop firewalld
systemctl disable firewalld
yum clean all
yum install iptables-services -y
systemctl enable iptables
systemctl start iptables
# For loop statement that will read and add all the IPs from the list to firewall rule. 
for i in $(cat list.txt);
do 
iptables -A OUTPUT -d "$i" -j DROP
done
# Save the rules
service iptables save

데비안/우분투

#!/bin/bash
apt-get update
apt install  iptables-persistent -y
# For loop statement that will read and add all the IPs from the list to firewall rule. 
for i in $(cat list.txt);
do 
iptables -A OUTPUT -d "$i" -j DROP
done
# Save the rules
netfilter-persistent save
netfilter-persistent reload

변경사항 확인:

$ iptables -L

관련 정보