Detailed iptables settings in Linux

In any case, iptables is a thing that needs to be set up with special care. If the server is not with you, and you can't SSH due to the settings rashly, then wait to be scolded by the boss, huh, huh. . .

The following content is written to prevent this from happening, of course it is very basic, but the general server is also enough:

1. First introduce the instructions and related configuration files

  1. Start command: service iptables start   
  2. Restart command: service iptables restart   
  3. Shutdown command: service iptables stop   
  4.   
  5. Then the relevant configuration: /etc/sysconfig/iptables   
  6. How to operate this configuration?   
  7. vim /etc/sysconfig/iptables   
  8. Then go in and modify it. What should I do after the modification? Many people here will think of the /etc/rc.d/init.d/iptables save command, but once you do that, your changes are for nothing. . .   
  9. The specific method is:   
  10. The only way to modify /etc/sysconfig/iptables to make it take effect is to first service iptables restart after modification, and then call /etc/rc.d/init.d/iptables save,   
  11. Because /etc/rc.d/init.d/iptables save will be reloaded when the iptables service starts, if you call /etc/rc.d/init.d/iptables save directly before restarting, then you   
  12. The /etc/sysconfig/iptables configuration is rolled back to the configuration of the last startup service, which must be noted! ! !  

2. The following introduces some instruction usage (mainly man iptables to see the relevant information)

  1. -A: Specify the chain name   
  2. -p: specify the protocol type   
  3. -d: specify the destination address   
  4. --dport: Specify the destination port (destination port destination port)   
  5. --sport: Specify the source port (source port source port)   
  6. -j: specify the action type  

3. Can I type commands directly without modifying the file? Of course, no problem. The steps are as follows:

  1. For example, I add a statement to SSH to release:   
  2. Add input record: iptables -A INPUT -p tcp --dport  22  -j ACCEPT   
  3. Add output record: iptables -A OUTPUT -p tcp --sport  22  -j ACCEPT   
  4. Finally, note that you need to execute /etc/init.d/iptables save again, so that these two statements are saved to the /etc/sysconfig/iptables file just now.  

4. Next, I will explain the steps. If the machine is not with me, I can only SSH in to make iptables rules, then I must pay attention to every step, don't make a mistake, otherwise it is possible that the SSH link will not be connected!

  1. The first thing to do is to configure ACCEPT for our SSH to avoid direct failure to connect:   
  2. 1. If the SSH port is 22 (it is not recommended to use the default port here, it is better to change the SSH port)   
  3. iptables -A INPUT -p tcp --dport 22 -j ACCEPT   
  4. iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT   
  5. Note that to save /etc/rc.d/init.d/iptables, it is best to execute this statement in each of the following steps, and the following will not be repeated.   
  6.   
  7. 2.vim /etc/sysconfig/iptables to determine whether the configuration has been added, if possible, execute service iptables restart to take effect after restarting   
  8.   
  9. 3. The following is a very dangerous operation. If you do not do the first step, it may directly cause you to be unable to connect to SSH. Remember to perform the first step before this step! ! !   
  10. iptables -P INPUT DROP    
  11. iptables -P OUTPUT DROP    
  12. iptables -P FORWARD DROP   
  13. 这个步骤是把所有不符合自己配置的规则ACCEPT的连接全部DROP掉,执行完以后如果咱SSH还没掉,那么谢天谢地,安全了,重启下iptables后继续下面的配置!   
  14.   
  15. 4.下面咱就不细说了,具体就是看自己服务器要开放哪些端口或者是要访问哪些端口来做具体的配置,下面是我自己的机器的配置:   
  16.   
  17. /etc/sysconfig/iptables文件配置如下:   
  18. # Generated by iptables-save v1.4.7 on Fri Mar  2 19:59:43 2012   
  19. *filter   
  20. :INPUT DROP [0:0]   
  21. :FORWARD DROP [0:0]   
  22. :OUTPUT DROP [8:496]   
  23. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT   
  24. #ping使用的端口   
  25. -A INPUT -p icmp -j ACCEPT   
  26. -A INPUT -i lo -j ACCEPT   
  27. -A INPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT   
  28. -A INPUT -s 192.168.2.200/32 -d 192.168.2.200/32 -j ACCEPT   
  29. #允许服务器自己的SSH(对外部请求来说服务器是目标所以使用--dport)   
  30. -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT   
  31. #80端口不用说了吧,服务器网站访问端口   
  32. -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT   
  33. -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT   
  34. -A INPUT -p tcp -m tcp --dport 11211 -j ACCEPT   
  35. -A INPUT -p tcp -m tcp --dport 11212 -j ACCEPT   
  36. -A FORWARD -j REJECT --reject-with icmp-host-prohibited   
  37. #53端口是DNS相关,TCP和UDP都要配置   
  38. -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT   
  39. -A INPUT -p udp -m udp --dport 53 -j ACCEPT   
  40. #ping使用的端口   
  41. -A OUTPUT -p icmp -j ACCEPT   
  42. -A OUTPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT   
  43. -A OUTPUT -s 192.168.2.200/32 -d 192.168.2.200/32 -j ACCEPT   
  44. #允许服务器SSH到其他机器(使用外部端口就使用--dport)   
  45. -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT   
  46. #允许服务器自己的SSH(自已为源输出就使用--sport)   
  47. -A OUTPUT -p tcp -m tcp --sport 22 -j ACCEPT   
  48. #访问外部网站80端口(使用外部端口就使用--dport)   
  49. -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT   
  50. #如果服务器需要访问外部网站,那么OUTPUT也需要配置53端口(使用外部端口就使用--dport)   
  51. -A OUTPUT -p tcp -m tcp --dport 53 -j ACCEPT   
  52. -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT   
  53. #如果有访问外部邮箱,那么打开邮箱相关端口(使用外部端口就使用--dport)   
  54. -A OUTPUT -p tcp -m tcp --dport 465 -j ACCEPT   
  55. -A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT   
  56. -A OUTPUT -p tcp -m tcp --dport 110 -j ACCEPT   
  57. #服务器网站访问端口(自已为源输出就使用--sport)   
  58. -A OUTPUT -p tcp -m tcp --sport 80 -j ACCEPT   
  59. -A OUTPUT -p tcp -m tcp --sport 3306 -j ACCEPT   
  60. -A OUTPUT -p tcp -m tcp --sport 11211 -j ACCEPT   
  61. -A OUTPUT -p tcp -m tcp --sport 11212 -j ACCEPT   
  62. COMMIT   
  63. # Completed on Fri Mar  2 19:59:43 2012  

5.可能有时候需要删除规则,最简单就是修改一下/etc/sysconfig/iptables然后service iptables restart,最后/etc/rc.d/init.d/iptables save即可。

当然也可以使用指令完成:

  1. 在网上找了一下,删除规则的方法:   
  2. 语法是: iptables -D chain rulenum [options]   
  3. 其中: chain 是链的意思,就是INPUT FORWARD 之类的   
  4.              rulenum 是规则的编号。从1 开始。可以使用  --line-numbers 列出规则的编号   
  5.     
  6. 所以,例如上面要删除一个INPUT链的规则的话可以这样:iptables -D INPUT 3   
  7. 意思是删除第3条规则。   
  8. 还有第二种方法。第二种办法是 -A 命令的映射,不过用-D替换-A。当你的链中规则很复杂,而你不想计算它们的编号的时候这就十分有用了。也就是说,你如何用iptables -A.... 语句定义了一个规则,则删除此规则时就用 -D 来代替- A  其余的都不变即可。   
  9. ======================   
  10. 说一下上面的 --line-numbers 选项,如下面的命令:   
  11. iptables -L INPUT --line-numbers   列出INPUT 链所有的规则   
  12. num  target     prot opt source               destination            
  13. 1    REJECT     tcp  --  anywhere             anywhere            tcp dpt:microsoft-ds reject-with icmp-port-unreachable   
  14. 2    REJECT     tcp  --  anywhere             anywhere            tcp dpt:135 reject-with icmp-port-unreachable   
  15. 3    REJECT     tcp  --  anywhere             anywhere            tcp dpt:netbios-ssn reject-with icmp-port-unreachable   
  16.   
  17. ...   
  18. ...   
  19. 删除指定行规则:   
  20. [root@localhost rc.d]# iptables -D INPUT 4  

  6.最后补充一下,如果想针对某IP进行单独开放端口可以如下配置:

  1. 如果我需要对内网某机器单独开放mysql端口,应该如下配置:   
  2. iptables -A INPUT -s 192.168.2.6 -p tcp -m tcp --dport 3306 -j ACCEPT   
  3. iptables -A OUTPUT -s 192.168.2.6 -p tcp -m tcp --sport 3306 -j ACCEPT  

7.彻底禁止某IP访问:

  1. #屏蔽单个IP的命令是   
  2. iptables -I INPUT -s 123.45.6.7 -j DROP   
  3. #封整个段即从123.0.0.1到123.255.255.254的命令   
  4. iptables -I INPUT -s 123.0.0.0/8 -j DROP   
  5. #封IP段即从123.45.0.1到123.45.255.254的命令   
  6. iptables -I INPUT -s 124.45.0.0/16 -j DROP   
  7. #封IP段即从123.45.6.1到123.45.6.254的命令是   
  8. iptables -I INPUT -s 123.45.6.0/24 -j DROP   
  9. 指令I是insert指令 但是该指令会insert在正确位置并不像A指令看你自己的排序位置,因此用屏蔽因为必须在一开始就要加载屏蔽IP,所以必须使用I命令加载,然后注意执行/etc/rc.d/init.d/iptables save进行保存后重启服务即可  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390704&siteId=291194637