CentOS7 install iptables firewall

The default firewall of CentOS7 is not iptables, but firewalle.

Install iptable iptable-service

# First check if iptables is installed
service iptables status
# Install iptables
yum install -y iptables
# Upgrade iptables
yum update iptables 
# Install iptables-services
yum install iptables-services

Disable/stop the built-in firewalld service

# Stop firewalld service
systemctl stop firewalld
# Disable firewalld service
systemctl mask firewalld

Set up existing rules

# View iptables existing rules
iptables -L -n
# Allow all first,Otherwise, there may be cups
iptables -P INPUT ACCEPT
# Clear all default rules
iptables -F
# Clear all custom rules
iptables -X
# All counters return to 0
iptables -Z
# Allow data packets from the lo interface(Local access)
iptables -A INPUT -i lo -j ACCEPT
# Open port 22
iptables -A INPUT -p tcp --dport 22-j ACCEPT
# Open port 21(FTP)
iptables -A INPUT -p tcp --dport 21-j ACCEPT
# Open port 80(HTTP)
iptables -A INPUT -p tcp --dport 80-j ACCEPT
# Open port 443(HTTPS)
iptables -A INPUT -p tcp --dport 443-j ACCEPT
# Allow ping
iptables -A INPUT -p icmp --icmp-type 8-j ACCEPT
# Allow the return data RELATED after accepting the request of the machine,Is set for FTP
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
# All other inbound will be discarded
iptables -P INPUT DROP
# Green light for all outbound stations
iptables -P OUTPUT ACCEPT
# All forwards are discarded
iptables -P FORWARD DROP

Other rule settings

# If you want to add intranet ip trust (accept all its TCP requests)
iptables -A INPUT -p tcp -s 45.96.174.68-j ACCEPT
# Filter all requests other than the above rules
iptables -P INPUT DROP
# To block an IP, use the following command:
iptables -I INPUT -s ***.***.***.***-j DROP
# To unblock an IP, use the following command:
iptables -D INPUT -s ***.***.***.***-j DROP

Save rule settings

# Save the above rules
service iptables save

Open iptables service

# Register iptables service
# Equivalent to the previous chkconfig iptables on
systemctl enable iptables.service
# Open service
systemctl start iptables.service
# Check status
systemctl status iptables.service

Solve the problem that vsftpd cannot use passive mode after iptables is turned on

  1. First modify or add the following content in /etc/sysconfig/iptables-config
# Add the following,Note that the order cannot be reversed
IPTABLES_MODULES="ip_conntrack_ftp"
IPTABLES_MODULES="ip_nat_ftp"
  1. Reset iptables settings
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

The following is the complete setup script

#! /bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22-j ACCEPT
iptables -A INPUT -p tcp --dport 21-j ACCEPT
iptables -A INPUT -p tcp --dport 80-j ACCEPT
iptables -A INPUT -p tcp --dport 443-j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8-j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service

Category: Linux

Recommended Posts

CentOS7 install iptables firewall
1.5 Install Centos7
CentOS7.3 install iptables and detailed use
Centos6 install Python2.7.13
Centos7.3 install nginx
CentOS7.2 install Mysql5.7.13
CentOS install Redmine
Centos7 install Python 3.6.
CentOS7 install MySQL
Centos7 install protobuf
CentOS 7 install Docker
CentOS7 install GlusterFS
CentOS 7.4 install Zabbix 3.4
CentOS7 install Docker
Centos6.5 install Tomcat
CentOS install Python 3.6
centos7 install docker-ce 18.01.0
CentOS 7.2 install MariaDB
CentOS 7 install Hadoop 3.0.0
Centos7 install Python2.7
Centos 7.6 install seleniu
CentOS 7.3 install Zabbix3
Centos7 install LAMP+PHPmyadmin
CentOS install mysql
CentOS install openjdk 1.8
CENTOS6.5 install CDH5.12.1 (1)
CentOS install PHP
CentOS6 install mist.io
Centos7 install Docker
CentOS7 install mysql
centOs install rabbitMQ
CentOS 7 install MySQL 5.6
Centos7 install Nginx
CentOS6.5 install CDH5.13
Centos7 install docker18
Centos install Python3
centos7 install docker
CentOS install jdk
centos7 install nginx-rtmp
CentOS8 install MySQL8.0
Centos6.3 install KVM
CentOS install PostgreSQL 9.1
CentOS7 install mysql8
CentOS 7 install Java 1.8
CentOS8 install fastdfs6.06
CentOS 7 install Gitlab
Centos 7 install PostgreSQL
CentOS7 install MySQL8
CentOS 7 install Java 1.8
CentOS 6 install Docker
centos 6.5 install zabbix 4.4
Centos8 install Docker
CentOS6.8 install python2.7
CentOS install nodejs 8
CentOS6.5 install GNS3
centos 7.5 install mysql5.7.17
Centos7 install MySQL8.0-manual
CentOS7 install Kubernetes 1.16.3
VirtualBox install centos7
centos7 install lamp
Install centos7 and connect