Iptables configuration methods and related commands

Source: Internet
Author: User
Firewall instructions: iptables-L view firewall rules iptables-save display current iptables settings iptables-tmangle-L view firewall advanced settings (including prerouting, input, forwad, output, postrouting) iptables-PINPUT/OUTPUT/FORWADDROP Firewall instructions:
Iptables-L view firewall rules
Iptables-save: displays current iptables settings
Iptables-t mangle-L view firewall advanced settings (including prerouting, input, forwad, output,
Postrouting)
Iptables-p input/OUTPUT/forwad drop/ACCEPT
Iptables-a input-p tcp-s 10.10.21.114 -- dport 22-j ACCEPT received from the host 114
Ssh request
Iptables-a input-s 10.10.90.0/24-p tcp-multi -- dport 143,445-j ACCEPT allowed
Samba service
Iptables-L -- line lists firewall rules by serial number
Iptables-d input 1 delete the 1st rules of the INPUT chain
Iptables-I INPUT 1-S 10.10.21.110-j ACCEPT insert a new
Rules
Iptable-r input 1-s 10.10.21.110-p tcp-multi -- port5801, 5802,6001-j ACCEPT
Iptables:
Deny all data packets before allowing the required
Iptables-P INPUT DROP
Iptables-P FORWARD DROP
Iptables-P OUTPUT ACCEPT
Enable Port:
Samba service port
The samba service uses the following ports:
TCP 137
TCP 138
TCP/IP 139
TCP/IP 445
1. check the settings of IPTABLES on the local machine [root @ tp ~] # Iptables-L-n
Chain INPUT (policy ACCEPT)
Target prot opt source destination
Chain FORWARD (policy ACCEPT)
Target prot opt source destination
Chain OUTPUT (policy ACCEPT)
Target prot opt source destination
Chain RH-Firewall-1-INPUT (0 Records)
Target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
ACCEPT udp -- 0.0.0.0/0 224.0.0.20.udp dpt: 5353
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 uddpt: 631
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED, ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: 22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: 80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: 25
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-withicmp-host-prohibited
We can see that when I installed linux, I chose to have a firewall and opened ports 22, 80, and 25.
If you do not choose to start the firewall when installing linux
[Root @ tp ~] # Iptables-L-n
Chain INPUT (policy ACCEPT)
Target prot opt source destination
Chain FORWARD (policy ACCEPT)
Target prot opt source destination
Chain OUTPUT (policy ACCEPT)
Target prot opt source destination
There are no rules.
2. clear the original rules.
Whether or not you have enabled the firewall when installing linux, if you want to configure your own firewall, clear the current filter
All rules.
[Root @ tp ~] # Iptables-F clear the rules of all rule chains in the filter of the preset table
[Root @ tp ~] # Iptables-X clear the rules in the user-defined chain in the filter of the preset table
Let's take a look.
[Root @ tp ~] # Iptables-L-n
Chain INPUT (policy ACCEPT)
Target prot opt source destination
Chain FORWARD (policy ACCEPT)
Target prot opt source destination
Chain OUTPUT (policy ACCEPT)
Target prot opt source destination
Nothing, just like we didn't start the firewall when installing linux.
If the IP address is configured in the command, restarting the IP address will become useless.) how to save the IP address.
[Root @ tp ~] #/Etc/rc. d/init. d/iptables save
In this way, you can write it to the/etc/sysconfig/iptables file. remember to repeat the firewall after writing it to make it take effect.
[Root @ tp ~] # Service iptables restart
Now, no configuration is available in the IPTABLES configuration table. let's start with our configuration. 3. set the default rule [root @ tp ~] # Iptables-p INPUT DROP
[Root @ tp ~] # Iptables-p OUTPUT ACCEPT
[Root @ tp ~] # Iptables-p FORWARD DROP
The above means that when two chain rules (INPUT, FORWARD) in the filter table in IPTABLES are exceeded, they are not
Then, how should we handle the data packets? DROP (discard). it should be said that this configuration is safe. we need to control inbound data.
Package
For the OUTPUT chain, that is, the outgoing package, we do not need to impose too many restrictions, but adopt ACCEPT. that is to say, there is no rule
Then, what should we do with the package.
We can see what packets are allowed to pass through the INPUT and FORWARD chains, and what packets are not allowed to pass through the OUTPUT chain.
Pass.
This setting is quite reasonable. of course you can also DROP all the three links, but I don't think it is necessary to write this.
The rule will be added, but if you only want to have a limited number of rules, for example, to only use WEB servers, or to recommend that all three links are DROP.
Note: If you log on remotely through SSH, you should drop it when you enter the first command and press enter, because you have not set any
What are the rules.
What should I do? go to the local machine to operate it!
4. add rules.
First, add the INPUT chain. the default rule of the INPUT chain is DROP, so we will write the chain that requires ACCETP ().
To enable remote SSH login, we need to enable port 22.
[Root @ tp ~] # Iptables-a input-p tcp -- dport 22-j ACCEPT
[Root @ tp ~] # Iptables-a output-p tcp -- sport 22-j ACCEPT (note: If you set
If the OUTPUT is set to DROP, this part should be written. many people are eager to write this rule, and SSH is always unavailable.
Okay.
The same applies to other ports. if the web server is enabled and the OUTPUT is set to DROP, a chain should also be added:
[Root @ tp ~] # Iptables-a output-p tcp -- sport 80-j ACCEPT .)
If the WEB server is configured, enable port 80.
[Root @ tp ~] # Iptables-a input-p tcp -- dport 80-j ACCEPT
If the email server is configured, enable Port 25,110.
[Root @ tp ~] # Iptables-a input-p tcp -- dport 110-j ACCEPT
[Root @ tp ~] # Iptables-a input-p tcp -- dport 25-j ACCEPT
If the FTP server is configured, enable port 21.
[Root @ tp ~] # Iptables-a input-p tcp -- dport 21-j ACCEPT
[Root @ tp ~] # Iptables-a input-p tcp -- dport 20-j ACCEPT
If the DNS server is configured, enable Port 53.
[Root @ tp ~] # Iptables-a input-p tcp -- dport 53-j ACCEPT
If you have another server, you just need to open the port and write it.
The above mainly writes the INPUT chain, and all the rules that are not in the above DROP
Allow icmp packets to pass, that is, allow ping,
[Root @ tp ~] # Iptables-a output-p icmp-j ACCEPT (if OUTPUT is set to DROP)
[Root @ tp ~] # Iptables-a input-p icmp-j ACCEPT (if INPUT is set to DROP)
Allow loopback! (Otherwise, DNS may fail to be shut down normally.) IPTABLES-a input-I lo-p all-j ACCEPT (if it is INPUT DROP)
IPTABLES-a output-o lo-p all-j ACCEPT (if it is output drop)
The OUTPUT chain is written below. the default rule of the OUTPUT chain is ACCEPT, so we will write the chain that needs to be dropped (abandoned) to reduce insecure port connections.
[Root @ tp ~] # Iptables-a output-p tcp -- sport 31337-j DROP
[Root @ tp ~] # Iptables-a output-p tcp -- dport 31337-j DROP
Some Trojans scan services from ports 31337 to 31340 (elite ports in hacking languages. Valid
Services do not use these non-standard ports for communication. blocking these ports can effectively reduce the number of machines that may be infected on your network.
Chance of independent communication between the server and their remote master server
There are also other ports, such as 31335, 27444, 27665, 20034 NetBus, 9704, 137-139 (smb), 2049
(NFS) ports should also be disabled. I have not written all of them here. if you are interested, check the relevant information.
Of course, you can set the OUTPUT chain to DROP for more secure access, so you can add more rules, just like adding
Allow SSH login. just write it.
The following is a more detailed rule, that is, to restrict access to a certain machine. for example, we only allow access to SSH from machines 192.168.0.3.
[Root @ tp ~] # Iptables-a input-s 192.168.0.3-p tcp -- dport 22-j ACCEPT
If you want to allow or restrict the availability of a certain IP address, 192.168.0.0/24 indicates all IP addresses of 192.168.0.1-255.
24 indicates the number of subnet masks. remember to delete this line in/etc/sysconfig/iptables.
-A input-p tcp-m tcp -- dport 22-j ACCEPT because it indicates that all addresses can log on.
Or use the following command:
[Root @ tp ~] # Iptables-d input-p tcp -- dport 22-j ACCEPT
Save it. I'll talk about it again. Instead, it uses the command method and only takes effect at that time. if you want to restart it, you need to save it.
Save. write to the/etc/sysconfig/iptables file.
[Root @ tp ~] #/Etc/rc. d/init. d/iptables save
Write it like this! 192.168.0.3 indicates IP address other than 192.168.0.3
The same is true for other rule connections.
The following is the FORWARD chain. the default rule of the FORWARD chain is DROP, so we will write the chain that requires ACCETP (via) to monitor the ongoing forwarding chain.
Enable the forwarding function (required when the default FORWARD rule is DROP when performing NAT)
[Root @ tp ~] # Iptables-a forward-I eth0-o eth1-m state -- state

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.