Configure iptables firewall in the replictuserver system
Source: Internet
Author: User
Iptables is a default firewall installed on UbuntuServer. During normal ubuntu installation, iptables is installed, but it allows all traffic by default (regardless of whether the firewall is invalid). there is a lot of valuable information about iptables, however, most of them are complex descriptions. If you want to do some basic configuration, the following HowTo IptablesIs a default firewall installed on UbuntuServer. During normal ubuntu installation, IptablesIs installed, but it allows all traffic by default (whether or not the firewall is invalid)
There is a lot of valuable information about iptables, but most of them are described as complicated. If you want To do some basic configuration, the following How To is suitable for you.
◆ Basic commands
Type: # iptables-L
List rules in your current iptables. If you have just created your server, there may be no rules at this time, and you should see the following:
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
◆ Allow session creation
We can allow session establishment to accept traffic:
# Iptables-a input-m state -- state ESTABLISHED, RELATED-jACCEPT
◆ Allow inbound traffic on the specified port
You can also start the system to block all traffic, but you may be working through SSH. it is necessary to allow SSH traffic before you block other traffic.
To allow inbound traffic on Port 22 (the default SSH port), you can tell iptables to allow your network adapter to accept all TCP traffic on the target port 22.
# Iptables-a input-p tcp-I eth0 -- dport ssh-j ACCEPT
In particular, this will append the (-A) INPUT rule to the table, allowing all traffic destined for SSH to enter the interface (-I) eth0, so that iptables can complete the jump (-j) or action: ACCEPT
Let's check these rules: (only a few lines are displayed here. you should see more)
# Iptables-L
Chain INPUT (policy ACCEPT)
Target prot opt source destination
ACCEPT all -- anywhere state RELATED, ESTABLISHED
ACCEPT tcp -- anywhere tcp dpt: ssh
Now, let's allow all web traffic
# Iptables-a input-p tcp-I eth0 -- dport 80-j ACCEPT
Check our existing rules
# Iptables-L
Chain INPUT (policy ACCEPT)
Target prot opt source destination
ACCEPT all -- anywhere state RELATED, ESTABLISHED
ACCEPT tcp -- anywhere tcp dpt: ssh
ACCEPT tcp -- anywhere tcp dpt: www
We have already specified SSH and web ports as the TCP traffic allowed, but since we have not blocked any traffic, all the traffic so far can still enter.
◆ Block traffic
Once a rule matches a package, other rules are no longer valid for this package. Because our rules allow SSH and WEB traffic first, we can still accept the traffic we are interested in as long as the rules that block all traffic keep up with it. All we have to do is put the rule that blocks all traffic at the bottom, so we need to use it again.
# Iptables-a input-j DROP
# Iptables-L
Chain INPUT (policy ACCEPT)
Target prot opt source destination
ACCEPT all -- anywhere state RELATED, ESTABLISHED
ACCEPT tcp -- anywhere tcp dpt: ssh
ACCEPT tcp -- anywhere tcp dpt: www
DROP all -- anywhere
Because we didn't specify an interface or protocol just now, all traffic except web and ssh traffic will be blocked.
◆ Edit iptables
So far, the only problem in our setup process is that loopbakc is also blocked. We can specify-I eth0 to discard only the packets on eth0, but we can also add a rule for loopback. If we append this rule, it will be too late-because all traffic has been dropped. We have to insert this line to 4th rows.
# Iptables-I input 4-I lo-j ACCEPT
# Iptables-L
Chain INPUT (policy ACCEPT)
Target prot opt source destination
ACCEPT all -- anywhere state RELATED, ESTABLISHED
ACCEPT tcp -- anywhere tcp dpt: ssh
ACCEPT tcp -- anywhere tcp dpt: www
ACCEPT all -- anywhere
DROP all -- anywhere
The last two rows look almost the same, so we can make the iptables column more detailed.
# Iptables-L-v
◆ Logging
In the preceding example, all traffic is not recorded. If you are willing to record discarded packets in syslog, the following is the quickest way:
# Iptables-I INPUT 5-m limit -- limit 5/min-j LOG -- log-prefix "iptables denied:" -- log-level 7
Obtain the prompt section
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