Chinaunix首页 | 论坛 | 博客
  • 博客访问: 738929
  • 博文数量: 235
  • 博客积分: 4309
  • 博客等级: 中校
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-17 11:25
个人简介

If you don\\\\\\\\\\\\\\\'t wanna do it, you find an EXCUSE; if you do, you\\\\\\\\\\\\\\\'ll find a WAY :-)

文章分类

全部博文(235)

文章存档

2014年(3)

2013年(2)

2012年(31)

2011年(199)

分类: LINUX

2011-01-17 17:50:01


1. 

  1. # Ubuntu - WARNING: Iptables and NetworkManager can conflict
  2. $ sudo apt remove network-manager
  3. or
  4. $ sudo apt --purge remove network-manager # use with caution

2. iptables version1.8.3

  1. # There are 5 methods to run iptables automatically at Startup


3. rc-local.service

  1. # Method 1

  2. # rc-local.service - /etc/rc.d/rc.local

  3. # systemctl status rc-local.service

  4. # chmod a+x /etc/rc.d/rc.local; ll /etc/rc.d/rc.local
  5. # systemctl daemon-reload

  6. # systemctl list-dependencies multi-user.target | grep rc-local

  7. # chown root:root /etc/iptables.start.rules
  8. # chmod 700 /etc/iptables.start.rules

  9. # echo "/etc/iptables.start.rules" >> /etc/rc.d/rc.local


4.  利用systemd 构建 serivce

  1. #
  2. # Method 2

  3. # Systemctl Service
  4. # /usr/lib/systemd/system - system level
  5. # /etc/systemd/system - user level


  6. # 2.1
  7. # sudo bash iptables.autoconf.sh

  8. # cat iptables.autoconf.sh
  9. #!/bin/bash
  10. #

  11. # disable ufw service
  12. systemctl disable ufw.service

  13. # copy the appropriate files to the target location
  14. cp ./iptables.{start,stop}.rules /etc/
  15. cp ./iptables.service /etc/systemd/system/

  16. # make the iptables script executable
  17. chmod a+x /etc/iptables.{start,stop}.rules

  18. # apply the customized iptables rules
  19. /bin/bash /etc/iptables.start.rules

  20. # dump iptables rules to a file
  21. /sbin/iptables-save > /etc/iptables.start.rules

  22. # enable the unit
  23. systemctl enable iptables.service


  24. #
  25. # Customizing Service
  26. $ cat /etc/systemd/system/iptables.service

  27. [Unit]
  28. Description=iptables firewall
  29. Documentation=man:iptables(8)
  30. DefaultDependencies=no
  31. Before=network.target

  32. [Service]
  33. Type=oneshot
  34. RemainAfterExit=yes
  35. ExecStart=/bin/bash -c "/sbin/iptables-restore < /etc/iptables.start.rules"
  36. ExecStop=/bin/bash -c "/etc/iptables.stop.rules"

  37. [Install]
  38. WantedBy=multi-user.target

  39. # enable the unit
  40. $ sudo systemctl enable iptables.service

  41. #
  42. # disable and remove the symlinked service

  43. $ sudo systemctl disable iptables.service
  44. $ sudo rm /etc/systemd/system/iptables.service


5. 利用systemd 构建 serivce,脚本模式

  1. # 2.2
  2. # sudo bash iptables.script.autoconf.sh

  3. # cat iptables.script.autoconf.sh
  4. #!/bin/bash
  5. #

  6. # disable ufw service
  7. systemctl disable ufw.service

  8. # copy the appropriate files to the target location
  9. cp ./iptables.{start,stop}.rules /etc/
  10. cp ./iptables.script.service /etc/systemd/system/

  11. # make the iptables script executable
  12. chmod a+x /etc/iptables.{start,stop}.rules

  13. # apply the customized iptables rules
  14. /bin/bash /etc/iptables.start.rules

  15. # enable the unit
  16. systemctl enable iptables.script.service

  17. #
  18. # disable and remove the symlinked service

  19. $ sudo systemctl disable iptables.script.service
  20. $ sudo rm /etc/systemd/system/iptables.script.service


  21. #
  22. # Customizing Service
  23. $ cat /etc/systemd/system/iptables.script.service

  24. [Unit]
  25. Description=iptables firewall
  26. Documentation=man:iptables(8)
  27. DefaultDependencies=no
  28. Before=network.target

  29. [Service]
  30. Type=oneshot
  31. RemainAfterExit=yes
  32. ExecStart=/bin/bash -c "/etc/iptables.start.rules"
  33. ExecStop=/bin/bash -c "/etc/iptables.stop.rules"

  34. [Install]
  35. WantedBy=multi-user.target


6. iptables-persistent

  1. #
  2. # Method 3

  3. # iptables-persistent
  4. # Install and use the iptables-persistent package

  5. $ sudo apt install iptables-persistent
7.

  1. #
  2. # Method 4 #(might be obsolete)
  3. /etc/network/interfaces


8.

  1. #
  2. # Method 5 #(might be obsolete)
  3. /etc/network/if-pre-up.d
  4. /etc/network/if-post-down.d


9. reference
阅读(949) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~