2017年5月18日 星期四

在 Ubuntu 開機時應用 NetworkManager 自動回存 iptables 設定


  1. 首先要使用 iptables-save 指令將目前的 iptables 設定儲存到某個檔案。這裡假設為 /etc/network/iptables/eth0_foxconn.ipv4.rules。
    root@cyvm:~# iptables-save > /etc/network/iptables/eth0_foxconn.ipv4.rules
    root@cyvm:~# 
    root@cyvm:~# 
  2. 在 /etc/NetworkManager/dispatcher.d/pre-up.d 底下新增一個 script。這裡假設為 iptables,完整檔案路徑即為 /etc/NetworkManager/dispatcher.d/pre-up.d/iptables。內容如下:
    #!/bin/sh
    ## $1 是網路介面名稱,如:eth0, enx00708fc27880 等等。
    ## $2 網路設定的 stage,在這裡限定此 screipt 只在 pre-up 時工作。
    
    if ! [ "$2" = "pre-up" ]; then
     echo "cylog: $0 restoring doesn't work at $2"
     exit 0
    fi
    
    case "$1" in
     "eth0")  ## 在 pre-up eth0 時怍用,網路介面並非限定 eth0,可依實際系統規畫而定。
      echo "cylog: $0 restoring iptables rules for $1"
      /sbin/iptables-restore /etc/network/iptables/eth0_foxconn.ipv4.rules
      ;;
     *)
      echo "cylog: $0 nothing to do for '$1' in '$2'"
      ;;
    esac
    
    exit 0
  3. 完成,可重開機以進行測試。

沒有留言:

張貼留言