1. Host Service Port#
$ iptables -I INPUT -p tcp --dport 80 -j DROP
$ iptables -I INPUT -p tcp -s 1.2.3.4 --dport 80 -j ACCEPT
Here, only 1.2.3.4
is allowed to access the local host's port 80.
2. Docker Service Port#
For services running with commands like docker run -d -p 80:80 shaowenchen/demo-whoami
, the above method is ineffective, and rules need to be added to the DOCKER-USER chain.
Docker will add iptables rules to the DOCKER chain, and if you need to add rules before Docker, they should be added to the DOCKER-USER chain.
$ iptables -I DOCKER-USER -i ens190 ! -s 1.2.3.4 -p tcp --dport 80 -j DROP
ens190 is the local network interface, and here only 1.2.3.4
is allowed to access the local host's port 80.
3. Clean Environment#
$ yum install -y iptables-services
$ systemctl restart iptables.service
If you need the iptables settings to remain effective after the host restarts, you need to install iptables-services and save the settings.
$ yum install -y iptables-services
$ service iptables save