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. 完成,可重開機以進行測試。

shell script 特殊技巧收集

列出所有的引數(arguments)

使用 bash 變數: $@

#!/bin/bash
echo $@ ## 這是引數陣列

[測試]

cy@cyvm:~/test$ ./test.sh 1 2 3 4 5
1 2 3 4 5

一一列出 $@ 每一個元素:

#!/bin/bash
for i ; do
 echo $i
done

[測試]

cy@cyvm:~/test$ ./test.sh 1 2 3 4 5
1
2
3
4
5

改用 $* 看看:

#/bin/bash
echo $* ## 這是引數字串
for i in $*; do
 echo $i
done

[測試]

cy@cyvm:~/test$ ./test.sh 1 2 3 4 5
1 2 3 4 5
1
2
3
4
5

使用 shift 移出前頭的引數來一一列示:

#/bin/bash
echo $# ## 這是引數總數
while (("$#")); do
 echo $1
 shift
done

[測試]

cy@cyvm:~/test$ ./test.sh 1 2 3 4 5
5
1
2
3
4
5
參考文章

在 Linux 安全移除 USB Disk 的指令

udisksctl unmount -b /dev/sdx1
udisksctl power-off -b /dev/sdx
其中 /dev/sdx 為 USB 所在區塊設備。

2017年5月15日 星期一

在 Ubuntu 開機時自動設置所需的內核參數

重點在於修改 /etc/sysctl.conf 檔案,修改了之後可下sysctl -p使設定立即生效。

常用的參數:


  • 路由路
    • 打開內核的封包傳遞 (IP forward) 功能:net.ipv4.ip_forward = 1