“UbuntuHelp:IptablesHowTo”的版本间的差异
来自Ubuntu中文
小 |
小 |
||
第1行: | 第1行: | ||
{{From|https://help.ubuntu.com/community/IptablesHowTo}} | {{From|https://help.ubuntu.com/community/IptablesHowTo}} | ||
− | {{Languages| | + | {{Languages|UbuntuHelp:IptablesHowTo}} |
THIS IS NOT COMPLETE AND SHOULD BE COMPLETED BY SOMEONE WHO KNOWS MORE THAN ME! THANKS | THIS IS NOT COMPLETE AND SHOULD BE COMPLETED BY SOMEONE WHO KNOWS MORE THAN ME! THANKS | ||
第11行: | 第11行: | ||
Typing | Typing | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see | lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see | ||
第29行: | 第28行: | ||
We can allow established sessions to receive traffic: | We can allow established sessions to receive traffic: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
第37行: | 第35行: | ||
To allow incoming traffic on port 22 (traditionally used by SSH), you could tell iptables to allow all TCP traffic on port 22 of your network adapter. | To allow incoming traffic on port 22 (traditionally used by SSH), you could tell iptables to allow all TCP traffic on port 22 of your network adapter. | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
Specifically, this appends (-A) to the table INPUT the rule that any traffic to the interface (-i) eth0 on the destination port for ssh that iptables should jump (-j), or perform the action, ACCEPT. | Specifically, this appends (-A) to the table INPUT the rule that any traffic to the interface (-i) eth0 on the destination port for ssh that iptables should jump (-j), or perform the action, ACCEPT. | ||
第43行: | 第40行: | ||
Lets check the rules: (only the first few lines shown, you will see more) | Lets check the rules: (only the first few lines shown, you will see more) | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
Chain INPUT (policy ACCEPT) | Chain INPUT (policy ACCEPT) | ||
target prot opt source destination | target prot opt source destination | ||
第52行: | 第48行: | ||
Now, let's allow all web traffic | Now, let's allow all web traffic | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
Checking our rules, we have | Checking our rules, we have | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
Chain INPUT (policy ACCEPT) | Chain INPUT (policy ACCEPT) | ||
target prot opt source destination | target prot opt source destination | ||
第71行: | 第65行: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
− | |||
Chain INPUT (policy ACCEPT) | Chain INPUT (policy ACCEPT) | ||
target prot opt source destination | target prot opt source destination | ||
第88行: | 第80行: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
− | |||
Chain INPUT (policy ACCEPT) | Chain INPUT (policy ACCEPT) | ||
target prot opt source destination | target prot opt source destination | ||
第101行: | 第91行: | ||
The last two lines look nearly the same, so we will list iptables in greater detail. | The last two lines look nearly the same, so we will list iptables in greater detail. | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
第107行: | 第96行: | ||
In the above examples none of the traffic will be logged. If you would like to log dropped packets to syslog, this would be the quickest way: | In the above examples none of the traffic will be logged. If you would like to log dropped packets to syslog, this would be the quickest way: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
See Tips section for more ideas on logging. | See Tips section for more ideas on logging. | ||
第117行: | 第105行: | ||
Save your firewall rules to a file | Save your firewall rules to a file | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
Then modify the ''/etc/network/interfaces'' script to apply the rules automatically (the bottom line is added) | Then modify the ''/etc/network/interfaces'' script to apply the rules automatically (the bottom line is added) | ||
第146行: | 第133行: | ||
If you edit your iptables beyond this tutorial, you may want to use the <code><nowiki>iptables-save</nowiki></code> and <code><nowiki>iptables-restore</nowiki></code> feature to edit and test your rules. To do this open the rules file in your favorite text editor (in this example gedit). | If you edit your iptables beyond this tutorial, you may want to use the <code><nowiki>iptables-save</nowiki></code> and <code><nowiki>iptables-restore</nowiki></code> feature to edit and test your rules. To do this open the rules file in your favorite text editor (in this example gedit). | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
You will have a file that appears similiar to (following the example above): | You will have a file that appears similiar to (following the example above): | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
*filter | *filter | ||
:INPUT ACCEPT [368:102354] | :INPUT ACCEPT [368:102354] | ||
第163行: | 第147行: | ||
-A INPUT -j DROP | -A INPUT -j DROP | ||
COMMIT | COMMIT | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
Notice that these are iptables commands minus the <code><nowiki>iptable</nowiki></code> command. Feel free to edit this to file and save when complete. Then to test simply: | Notice that these are iptables commands minus the <code><nowiki>iptable</nowiki></code> command. Feel free to edit this to file and save when complete. Then to test simply: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
After testing, if you have not added the <code><nowiki>iptables-save</nowiki></code> command above to your <code><nowiki>/etc/network/interfaces</nowiki></code> remember not to lose your changes: | After testing, if you have not added the <code><nowiki>iptables-save</nowiki></code> command above to your <code><nowiki>/etc/network/interfaces</nowiki></code> remember not to lose your changes: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
第177行: | 第158行: | ||
For further detail in your syslog you may want create an additional Chain. This will be a very brief example of my /etc/iptables.up.rules showing how I setup my iptables to log to syslog: | For further detail in your syslog you may want create an additional Chain. This will be a very brief example of my /etc/iptables.up.rules showing how I setup my iptables to log to syslog: | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
*filter | *filter | ||
:INPUT ACCEPT [273:55355] | :INPUT ACCEPT [273:55355] | ||
第193行: | 第173行: | ||
-A LOGNDROP -j DROP | -A LOGNDROP -j DROP | ||
COMMIT | COMMIT | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
Note a new CHAIN called <code><nowiki>LOGNDROP</nowiki></code> at the top of the file. Also, the standard <code><nowiki>DROP</nowiki></code> at the bottom of the INPUT chain is replaceed with <code><nowiki>LOGNDROP</nowiki></code> and add protocol descriptions so it makes sense looking at the log. Lastly we drop the traffic at the end of the <code><nowiki>LOGNDROP</nowiki></code> chain. The following gives some idea of what is happening: | Note a new CHAIN called <code><nowiki>LOGNDROP</nowiki></code> at the top of the file. Also, the standard <code><nowiki>DROP</nowiki></code> at the bottom of the INPUT chain is replaceed with <code><nowiki>LOGNDROP</nowiki></code> and add protocol descriptions so it makes sense looking at the log. Lastly we drop the traffic at the end of the <code><nowiki>LOGNDROP</nowiki></code> chain. The following gives some idea of what is happening: | ||
第204行: | 第183行: | ||
If you need to disable the firewall temporarily, you can flush all the rules using | If you need to disable the firewall temporarily, you can flush all the rules using | ||
<pre><nowiki> | <pre><nowiki> | ||
− | |||
</nowiki></pre> | </nowiki></pre> | ||
第227行: | 第205行: | ||
Thanks to Rusty Russell and his How-To, as much of this is based off that. | Thanks to Rusty Russell and his How-To, as much of this is based off that. | ||
---- | ---- | ||
− | CategorySecurity CategoryCleanup | + | [[category:CategorySecurity]] [[category:CategoryCleanup]] |
[[category:UbuntuHelp]] | [[category:UbuntuHelp]] |
2007年5月14日 (一) 11:03的版本
THIS IS NOT COMPLETE AND SHOULD BE COMPLETED BY SOMEONE WHO KNOWS MORE THAN ME! THANKS
目录
- 1 Basic Iptables How to for Ubuntu Server Edition
Basic Iptables How to for Ubuntu Server Edition
Iptables is a firewall, installed by default on the Ubuntu Server. On regular Ubuntu install, iptables is installed but allows all traffic (thus firewall is ineffective / inactive)
There is a wealth of information available about iptables, but much of it is fairly complex, and if you want to do a few basic things, this How To is for you.
Basic Commands
Typing
lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see
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
Allowing Established Sessions
We can allow established sessions to receive traffic:
Allowing Incoming Traffic on Specific Ports
You could start by blocking traffic, but you might be working over SSH, where you would need to allow SSH before blocking everything else.
To allow incoming traffic on port 22 (traditionally used by SSH), you could tell iptables to allow all TCP traffic on port 22 of your network adapter.
Specifically, this appends (-A) to the table INPUT the rule that any traffic to the interface (-i) eth0 on the destination port for ssh that iptables should jump (-j), or perform the action, ACCEPT.
Lets check the rules: (only the first few lines shown, you will see more)
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
Now, let's allow all web traffic
Checking our rules, we have
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:www
We have specifically allowed tcp traffic to the ssh and web ports, but as we have not blocked anything, all traffic can still come in.
Blocking Traffic
Once a decision is made about a packet, no more rules affect it. As our rules allowing ssh and web traffic come first, as long as our rule to block all traffic comes after them, we can still accept the traffic we want. All we need to do is put the rule to block all traffic at the end. The -A command tells iptables to append the rule at the end, so we'll use that again.
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:www DROP all -- anywhere anywhere
Because we didn't specify an interface or a protocol, any traffic for any port on any interface is blocked, except for web and ssh.
Editing iptables
The only problem with our setup so far is that even the loopback port is blocked. We could have written the drop rule for just eth0 by specifying -i eth0, but we could also add a rule for the loopback. If we append this rule, it will come too late - after all the traffic has been dropped. We need to insert this rule onto the fourth line.
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:www ACCEPT all -- anywhere anywhere DROP all -- anywhere anywhere
The last two lines look nearly the same, so we will list iptables in greater detail.
Logging
In the above examples none of the traffic will be logged. If you would like to log dropped packets to syslog, this would be the quickest way:
See Tips section for more ideas on logging.
Saving iptables
If you were to reboot your machine right now, your iptables configuration would disapear. Rather than type this each time you reboot, however, you can save the configuration, and have it start up automatically. To save the configuration, you can use iptables-save
and iptables-restore
.
Configuration on startup
Save your firewall rules to a file
Then modify the /etc/network/interfaces script to apply the rules automatically (the bottom line is added)
auto eth0 iface eth0 inet dhcp pre-up iptables-restore < /etc/iptables.up.rules
You can also prepare a set of down rules and apply it automatically
auto eth0 iface eth0 inet dhcp pre-up iptables-restore < /etc/iptables.up.rules post-down iptables-restore < /etc/iptables.down.rules
Tips
If you manually edit iptables on a regular basis
The above steps go over how to setup your firewall rules and presume they will be relatively static (and for most people they should be). But if you do a lot of development work, you may want to have your iptables saved everytime you reboot. You could add a line like this one in /etc/network/interfaces
:
pre-up iptables-restore < /etc/iptables.up.rules post-down iptables-save > /etc/iptables.up.rules
The line "post-down iptables-save > /etc/iptables.up.rules" will save the rules to be used on the next boot.
Using iptables-save/restore to test rules
If you edit your iptables beyond this tutorial, you may want to use the iptables-save
and iptables-restore
feature to edit and test your rules. To do this open the rules file in your favorite text editor (in this example gedit).
You will have a file that appears similiar to (following the example above):
*filter :INPUT ACCEPT [368:102354] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [92952:20764374] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 -A INPUT -j DROP COMMIT
Notice that these are iptables commands minus the iptable
command. Feel free to edit this to file and save when complete. Then to test simply:
After testing, if you have not added the iptables-save
command above to your /etc/network/interfaces
remember not to lose your changes:
More detailed Logging
For further detail in your syslog you may want create an additional Chain. This will be a very brief example of my /etc/iptables.up.rules showing how I setup my iptables to log to syslog:
*filter :INPUT ACCEPT [273:55355] :FORWARD ACCEPT [0:0] :LOGNDROP - [0:0] :OUTPUT ACCEPT [92376:20668252] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -j LOGNDROP -A LOGNDROP -p tcp -m limit --limit 5/min -j LOG --log-prefix "Denied TCP: " --log-level 7 -A LOGNDROP -p udp -m limit --limit 5/min -j LOG --log-prefix "Denied UDP: " --log-level 7 -A LOGNDROP -p icmp -m limit --limit 5/min -j LOG --log-prefix "Denied ICMP: " --log-level 7 -A LOGNDROP -j DROP COMMIT
Note a new CHAIN called LOGNDROP
at the top of the file. Also, the standard DROP
at the bottom of the INPUT chain is replaceed with LOGNDROP
and add protocol descriptions so it makes sense looking at the log. Lastly we drop the traffic at the end of the LOGNDROP
chain. The following gives some idea of what is happening:
-
--limit
sets the number of times to log the same rule to syslog -
--log-prefix "Denied..."
adds a prefix to make finding in the syslog easier -
--log-level 7
sets the syslog level to informational (see man syslog for more detail, but you can probably leave this)
Disabling the firewall
If you need to disable the firewall temporarily, you can flush all the rules using
Easy configuration via GUI
A new user can use Firestarter (a gui), available in repositories (Synaptic or apt-get) to configure her/his iptable rules, without needing the command line knowledge. Please see the tutorial though... Configuration is easy, but may not be enough for the advanced user. However, it should be enough for the most home users... The (read:my) suggested outbound configuration is "restrictive", with whitelisting each connection type whenever you need it (port 80 for http, 443 for secure http -https-, 1863 for msn chat etc) from the "policy" tab within firestarter. You can also use it to see active connections from and to your computer... The firewall stays up once it is configured using the wizard. Dial-up users will have to specify it to start automatically on dial up in the wizard.
Homepage for firestarter: http://www.fs-security.com/ (again, available in repositories, no compiling required) Tutorial: http://www.fs-security.com/docs/tutorial.php
Personal note: Unfortunately, it does not have the option to block (or ask the user about) connections of specific applications/programs... Thus, my understanding is that once you enable port 80 (i.e. for web access), any program that uses port 80 can connect to any server and do anything it pleases...
Further Information
Netfilter and Iptables Multilingual Documentation
Easy Firewall Generator for IPTables
Credits
Thanks to Rusty Russell and his How-To, as much of this is based off that.