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

2017年3月19日 星期日

備份與回存硬碟分割資訊

備份

sfdisk -d /dev/sdx > part_table_filepath

回存

sfdisk /dev/sdx < part_table_filepath

root@cyvm:/dev# sfdisk /dev/sdd < /media/sf_hd_image_backup/5054/part_table
Checking that no-one is using this disk right now ... OK

Disk /dev/sdd: 698.7 GiB, 750156374016 bytes, 1465149168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x5e487107

Old situation:

Device     Boot    Start        End    Sectors  Size Id Type
/dev/sdd1           2048   31459327   31457280   15G 27 Hidden NTFS WinRE
/dev/sdd2       31459328   38799359    7340032  3.5G 12 Compaq diagnostics
/dev/sdd3  *    38799360   39004159     204800  100M  7 HPFS/NTFS/exFAT
/dev/sdd4       39004160 1465145343 1426141184  680G  f W95 Ext'd (LBA)
/dev/sdd5       39006208 1465145343 1426139136  680G  7 HPFS/NTFS/exFAT

>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x2fdd5485.
Created a new partition 1 of type 'Hidden NTFS WinRE' and of size 9.8 GiB.
/dev/sdd2: Created a new partition 2 of type 'FAT16' and of size 51.2 GiB.
/dev/sdd3: Created a new partition 3 of type 'HPFS/NTFS/exFAT' and of size 50.9 GiB.
/dev/sdd4: 
New situation:

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sdd1              63  20466809  20466747  9.8G 27 Hidden NTFS WinRE
/dev/sdd2  *     20466810 127732814 107266005 51.2G  6 FAT16
/dev/sdd3       127732815 234436544 106703730 50.9G  7 HPFS/NTFS/exFAT

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

dd

使用 dd 僅能備份主要分割資訊,所以不建議使用,這裡僅供研究參考用。

參考:https://wiki.archlinux.org/index.php/Fdisk

MBR 使用磁碟最前面 512 bytes 來儲存資訊,包含了三部份:

  1. 446 bytes: boot loader
  2. 64 bytes: partition table (共四組,每組使用16 bytes)
  3. 2 bytes: identifier

備份方法:

dd if=/dev/sdx of=part_table_filepath bs=1 skip=446 count=64

回存方法:

dd if=part_table_filepath of=/dev/sdx bs=1 seek=446 count=64

2017年3月18日 星期六

使用 systemd 操作 service - 列出所有受管理的項目

cy@cyvm:~$ systemctl status -all
● cyvm
    State: degraded
     Jobs: 0 queued
   Failed: 1 units
    Since: 六 2017-03-18 10:26:53 CST; 7h ago
   CGroup: /
           ├─init.scope
           │ └─1 /sbin/init splash
           ├─system.slice
           │ ├─avahi-daemon.service
           │ │ ├─859 avahi-daemon: running [cyvm.local
           │ │ └─931 avahi-daemon: chroot helpe
           │ ├─vboxadd-x11.service
...
cy@cyvm:~$ systemctl list-unit-files --all
UNIT FILE                                  STATE
proc-sys-fs-binfmt_misc.automount          static
dev-hugepages.mount                        static
dev-mqueue.mount                           static
proc-sys-fs-binfmt_misc.mount              static
sys-fs-fuse-connections.mount              static
sys-kernel-config.mount                    static
sys-kernel-debug.mount                     static
acpid.path                                 enabled
cups.path                                  enabled
systemd-ask-password-console.path          static
systemd-ask-password-plymouth.path         static
systemd-ask-password-wall.path             static
...

2017年3月17日 星期五

shell script 特殊技巧收集

將字串變大/小寫

大寫

cy@cyvm:~$ STR=teSt
cy@cyvm:~$ echo ${STR}
teSt

首字大寫

cy@cyvm:~$ echo ${STR^}
TeSt

全部大寫

cy@cyvm:~$ echo ${STR^^}
TEST

小寫

cy@cyvm:~$ STR=TeSt
cy@cyvm:~$ echo ${STR}
TeSt

首字小寫

cy@cyvm:~$ echo ${STR,}
teSt

全部小寫

cy@cyvm:~$ echo ${STR,,}
test