2017年7月5日 星期三

shell script 特殊技巧收集

動態改變命令列的提示(prompt)

PROMPT_COMMAND

PROMPT_COMMAND 是 bash 的環境變數,每執行一道命令列命令後,存在此變數的指令都會被執行一遍,我們可以利用此一特性改變我們每一次的命令列提示。

以下所示範的是:

  1. 顯示上一次執行命令的狀態碼(Exit Code)。
  2. 若現行工作目錄未改變,則顯示目前的名錄名即可,否則顯示完整的路徑名。
  3. 部份 code 引用原來的 Ubuntu 家目錄(home)底下的 .bashrc 的 PS1 設定,如:debian_chroot,其他系統需要作相對應的修改。
__var_show_dirpath__=
__var_show_dirpath__='\w'
PROMPT_COMMAND='__prompt_command__'
__prompt_command__() {
    PS1="[$?] "
    if [ "$__var_show_dirpath__" = "$PWD" ]; then
        __show_dirpath__='\W'
    else
        __show_dirpath__='\w'
        __var_show_dirpath__=$PWD
    fi
    PS1+='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]'
    PS1+=$__show_dirpath__
    PS1+='\[\033[00m\]\$ '
}

你可以將以上的程式碼嵌入進你家目錄的 .bashrc 中,重開啟 shell 便能看到效果,如以下所示:
[0] cy@cyvm:~/tmp$ pwd
/home/cy/tmp
[0] cy@cyvm:tmp$ ls
mnt  old  test
[0] cy@cyvm:tmp$ cd test
[0] cy@cyvm:~/tmp/test$ ls

2017年7月2日 星期日

[便攜式 Debian 系統實作] - 創建第一個 ISO hybrid 映像檔

(本文主要依據 Debian Live Systems Manual 翻譯)

不管映像檔類型為何,創建的基本步驟大致上都相同:

  1. 建立一個工作目錄,更改到該目錄。工作目錄的名稱可由你自己決定。在這個示範裡,我們使用 live-default 做為我們的工作目錄。
  2. 執行以下 live-build 命令序列,以創建一個基本的 ISO hybrid 映像檔(一個已預設好的不包含 X.org 的【便攜式系統】)。適用於刻錄到CD或DVD介質上,也可以復製到U盤上。部份指令需要系統管理員權限才能正確執行,為了方便起見,建議你一開始執行的時候,就直接切換為系統管理員登入。
mkdir live-default && cd live-default
lb config
lb build

創建【便攜式系統】需要花一點時間,當建好的時候,你可以在工作目錄下看到一個名為 binary.hybrid.iso 的映像檔(譯註:不同系統的  live-build ,產出的檔名有區別,我在 Ubuntu 下產生的檔名如上,另外在 Debian 8 64bits 版本下產生的則為 live-image-amd64.hybrid.iso)。

除了自己動手做外,你也可以使用其他人已經預先構建好的映像檔。live-images 即是一個用 git 儲存庫 (git repository) 管理的便攜式 Debian 系統專案。官方穩定版本發佈在在 https://www.debian.org/CD/live/ (譯註:其實就是 Debian 安裝光檔)。而舊版與即將發行的版本,以及包含非免費軟韌體的非官方版本則是發佈在 http://debian-live.alioth.debian.org/cdimage/release/ (譯註:似乎已不存在)。


[便攜式 Debian 系統實作] - 基礎作業系統架構

(本文主要依據 Debian Live Systems Manual 翻譯)

【便攜式系統】包含了一個根據目標環境而建立的作業系統,包括了以下幾個部份:


  1. Linux內核映像檔(Linux kernel image)。通常命名為 vmlinuz。
  2. 初始虛擬磁碟映像檔(Initial RAM disk image)。通常命名為 initrd。為 Linux 開機時所使用的虛擬磁碟,包含了一些用來掛載系統映像檔可能需要的模組,以及一些用來設置的腳本。
  3. 系統映像檔(System image)。此為作業系統的檔案系統映像檔。我們可使用 SquashFS 壓縮式檔案系統來縮小系統映像檔。這映像檔內容是唯讀的。因此,在開機過程中,【便攜式系統】將啟用虛擬磁碟和「聯合(union)」的機制來啟用系統執行時所需要寫入的檔案。只是,所有修改將在機器關機時丟失,除非我們使用永久性寫入功能(這是可選用的功能)。
  4. 關機加載程式(Bootloader)。一小段特製的程式可從選定的介質中載入,可用來選擇或配置系統,可能使用提示列或從一個選單輸入。當使用者選定了之後,它加載 Linux 內核及相對應的 initrd 以運行相關的系統檔案系統。根據介質和檔案系統格式,我但可使用不同的解決方案:
    • isolinux:從使用 ISO9660 格式的光碟載入。
    • syslinux:從使用 VFAT 分區啟動的硬碟或 USB 隨身碟載入。
    • extlinux:則是從 ext2/3/4 或 btrfs 分區的硬碟或 USB 隨身碟載入。
    • pxelinux:從 PXE 進行網路開機 (netboot)
    • GRUB:則是從 ext2/3/4 分區的硬碟或 USB 隨身碟載入。

[便攜式 Debian 系統實作] - 工具安裝

(本文主要依據 Debian Live Systems Manual 翻譯)

需求

  • 系統管理員(root)權限
  • live-build
  • bash
  • debootstrap or cdebootstrap
  • Linux 2.6 以上版本

安裝 live-build

apt-get update
apt-get install live-build

安裝 live-bootlive-config

以 apt-get 安裝 live-build 時,即會自動安裝此兩套件。


2017年6月29日 星期四

[便攜式 Debian 系統實作] - 名詞解釋

(本文主要依據 Debian Live Systems Manual 翻譯)


  • 便攜式系統 (Live System):不需安裝到硬碟即可開機使用的作業系統。
【便攜式系統】通常指的是從可移動介質(例如:CD-ROM 或 USB 隨身碟)或從網絡啟動的操作系統,不需要安裝到硬碟,在運行時即能完成自動配置,開機後即可使用。

一般來說,【便攜式系統】不會影響原本已存在計算機硬碟上的作業系統或檔案,除非我們有意為之。典型的【便攜式系統】可以用光碟或隨身碟開機,有些則能夠通過網路開機。
  • 便攜式介質 (Live medium):使用 live-build 生成出來的可開機的位元檔 (binary) ,可存放於指定的可執行開機程式的介質上,如:光碟、隨身碟 或 網路上存放可開機檔案的位置,都是我們這裡說的便攜式介質。
  • 便攜式系統專案 (Live Systems Project):本專案維護以下具包,包括:live-boot, live-build, live-config, live-toolslive-manual 等等。
  • 育成環境 (Host system):用於創建【便攜式系統】 的環境。
  • 目標環境 (Target system):用於運行【便攜式系統】的環境。
  • live-boot:用於開機載入【便攜式系統】的腳本集。
  • live-build:用於構建客製化的【便攜式系統】的腳本集。
  • live-config:用於在開機過程中配置【便攜式系統】的腳本集。
  • live-tools:在運行中的【便攜式系統】中,用於執行一些特定任務的附加腳本集。
  • live-manual:相關文件集。
  • Debian安裝程序 (Debian Installer (d-i)):Debian官方的軟體安裝系統。
  • 開機參數 (Boot parameters):可以在開機程式提示列輸入以設定內核或 live-config 的參數。 
  • chroot:此命令可使我們能在同一個系統裡同時運行不同的 GNU / Linux 的環境實例,而無需重新啟動系統。
  • 位元映像檔 (Binary image):【便攜式系統】的位元檔,如:live-image-i386.hybrid.iso 或 live-image-i386.img。
有三種最常用類型:

  1. iso-hybrid:可用於虛擬機,光學介質(CD/DVD) 或 USB 等便攜式存儲設備。 
  2. hdd:不適用於光學介質,但適用於 USB 隨身碟(包括:快閃記憶體 或 硬碟),在某些 BIOS 支援 iso-hybrid 有問題的電腦系統上,這可能是解決方案。
  3. netboot:此類型的映像檔需要伺服器的輔助,使用者需要有網路開機的知識與技術。但一旦安裝完成,它是一種非常低成本而便利的方法,可以在區域網路上進行電腦系統開機,而不需要進行一些映像檔燒錄進介質的動作,通常可用來測試並部署映像檔。
  • 目標發行版 (Target distribution:):【便攜式系統】所依據的 Linux 發行版。這可能與【育成環境】的 Linux 發行版不同。
  • 穩定/測試/不穩定 (stable/testing/unstable):
    • 穩定的發行版:目前代號為 jessie,包含 Debian 最新正式發行的發行版。
    • 測試的發行版:(暫時代號為 stretch)是下一個穩定版本的待待命集結區。使用此發行版的主要優點是它具有相對於穩定版本的更新版本的軟件。
    • 不穩定的分配:永久代號為 sid,是 Debian 積極發展的區域。一般來說,這種分配由開發人員所管理。

譯註:

將 Live System 翻成【便攜式系統】,我相信一定有很多人不滿意,甚至存在許多人可能會認為乾脆不翻,直接使用 Live System 會比較貼近真義。

我認為所謂的貼近真義是陷入一種迷思中。

這道理很簡單,若你拿 Live System 去問不是一個相關領域的人,看看他們能不能一聽到 Live System 就能能聯想到「一種系統,不用安裝到硬碟就能執行」。我敢說,就算是以英文為母語的人,也不可能望文生義。

就像我們把 macro 翻譯【巨集】或【宏】,是因為在我們腦中看到這,便存有巨大的集合或宏大的圖像嗎?

當然,不是。

是因為我們知道 macro、【巨集】或【宏】是什麼東西,做什麼用的,我們將這功能與這名字做了聯結,所以才會「望文生義」。

Live System 算是一種新詞,其實我認為也應該造一個新詞來做聯結是比較好一點,我看到的譯法中,我覺得【自生系統】是比較好又貼近英文的譯法,不過,我總覺得那裡怪怪的,所以,還是使用了比較穩健的【便攜式系統】來當 Live System 的中譯,畢竟這概念與 Portable Software 是有點像的(原來是使用可攜式系統,後來改用便攜式系統)。

不過,我開始這研究不是為了可攜式目的的,而是希望設計一套系統,平時可將耗電的硬碟關閉,只靠隨身碟來運作,只有在需要時才把硬碟打開。

【便攜式系統】可以做的東西很多,可攜性只是其中一個特性而已--雖然是很重要的一項。希望大家不會因為這名字而限制了對這系統使用上的想像。

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

2017年2月21日 星期二

取消 Ubuntu 圖形介面,改用文字介面

修改 GRUB

  1. 修改 /etc/default/grub,將 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 變成 GRUB_CMDLINE_LINUX_DEFAULT="text"
    # If you change this file, run 'update-grub' afterwards to update
    # /boot/grub/grub.cfg.
    # For full documentation of the options in this file, see:
    #   info -f grub -n 'Simple configuration'
    
    GRUB_DEFAULT=0
    GRUB_HIDDEN_TIMEOUT=0
    GRUB_HIDDEN_TIMEOUT_QUIET=true
    GRUB_TIMEOUT=10
    GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
    #GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
    GRUB_CMDLINE_LINUX_DEFAULT="text"
    GRUB_CMDLINE_LINUX=""
    
  2. 更新 GRUB
    root@cysrv:~# update-grub
    Generating grub configuration file ...
    Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
    Found linux image: /boot/vmlinuz-4.4.0-63-generic
    Found initrd image: /boot/initrd.img-4.4.0-63-generic
    Found linux image: /boot/vmlinuz-4.4.0-62-generic
    Found initrd image: /boot/initrd.img-4.4.0-62-generic
    Found linux image: /boot/vmlinuz-4.4.0-59-generic
    Found initrd image: /boot/initrd.img-4.4.0-59-generic
    Found memtest86+ image: /memtest86+.elf
    Found memtest86+ image: /memtest86+.bin
    done
    


修改 systemd

  1. sudo systemctl enable multi-user.target --force
  2. sudo systemctl set-default multi-user.target

還原 GUI 設定 systemd 的方法

  1. sudo systemctl unmask lightdm
  2. sudo systemctl enable lightdm
  3. sudo systemctl set-default graphical.target

2017年1月30日 星期一

Linux 時間管理常用指令收集

1. date

[說明]

查看或設定時間。


[範例]

查看時間

cy@cyvm:~$ date
公曆 20十七年 一月 卅日 週一 十六時47分廿四秒

查看時間,顯示成英文格式

cy@cyvm:~$ LANG=en_US ; date
Mon Jan 30 16:53:18 CST 2017

cy@cyvm:~$ LANG=en ; date
Mon Jan 30 16:53:31 CST 2017

查看時間,格式化顯示時間

cy@cyvm:~$ date +"%Y-%m-%d %H:%M:%S"
2017-01-30 17:05:53
cy@cyvm:~$ date +"%s"
1485767170

  • 一般常用年月日時分秒組合數字格式
    %Ffull date; same as %Y-%m-%d
    %Ttime; same as %H:%M:%S
    %Yyear
    %mmonth (01..12)
    %dday of month (e.g., 01)
    %Hhour (00..23)
    %Mminute (00..59)
    %Ssecond (00..60)
    %Nnanoseconds (000000000..999999999)

  • 月份文字
    %Blocale's full month name (e.g., January)
    %blocale's abbreviated month name (e.g., Jan)
    %hsame as %b

  • 星期文字
    %Alocale's full weekday name (e.g., Sunday)
    %alocale's abbreviated weekday name (e.g., Sun)

  • 時區
    %z+hhmm numeric time zone (e.g., -0400)
    %:z+hh:mm numeric time zone (e.g., -04:00)
    %::z+hh:mm:ss numeric time zone (e.g., -04:00:00)
    %:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
    %Zalphabetic time zone abbreviation (e.g., EDT)

  • UNIX Timestamp (Epoch time)
    %sseconds since 1970-01-01 00:00:00 UTC

  • 年或週的週期日數
    %jday of year (001..366)
    %uday of week (1..7); 1 is Monday

  • 週數
    %Uweek number of year, with Sunday as first day of week (00..53)
    %Wweek number of year, with Monday as first day of week (00..53)

  • ISO week number
    %Gyear of ISO week number (see %V); normally useful only with %V
    %glast two digits of year of ISO week number (see %G)
    %VISO week number, with Monday as first day of week (01..53)

  • 世紀
    %Ccentury; like %Y, except omit last two digits (e.g., 20)
    %ylast two digits of year (00..99)

  • 12小時制
    %Ihour (01..12)
    %plocale's equivalent of either AM or PM; blank if not known
    %Plike %p, but lower case

  • 地區限定格式
    %clocale's date and time (e.g., Thu Mar 3 23:05:25 2005)
    %xlocale's date representation (e.g., 12/31/99)
    %Xlocale's time representation (e.g., 23:13:48)
    %rlocale's 12-hour clock time (e.g., 11:11:04 PM)

  • 特殊文字
    %%a literal %
    %na newline
    %ta tab

  • 不常用或有其他替代方案的格式
    %Ddate; same as %m/%d/%y
    %R24-hour hour and minute; same as %H:%M
    %eday of month, space padded; same as %_d
    %khour, space padded ( 0..23); same as %_H
    %lhour, space padded ( 1..12); same as %_I
    %wday of week (0..6); 0 is Sunday


設定時間

*注意!操作此需要 root 權限。

cy@cyvm:~$ sudo date +"%Y-%m-%d %H:%M:%S" -s "2016-10-05 12:24:36"
2016-10-05 12:24:36



2. ntpdate

[說明]

經由 NTP (Network Time Protocol,RFC 1305),設定系統時間。


[範例]

設定時間

*注意!操作此需要 root 權限。

cy@cyvm:~$ sudo ntpdate -s time.stdtime.gov.tw



3. hwclock

[說明]

在 Linux 下操作時間,一般都只是寫入作業系統,若要同步寫入硬體(RTC, real-time clock),需要使用此指令。


[範例]

查看時間

cy@cyvm:~$ sudo hwclock -r
Mon Jan 30 05:35:39 2017  .262043 seconds

設定時間

cy@cyvm:~$ sudo hwclock -w

2017年1月24日 星期二

在 Ubuntu 上安裝 Samba Server

1. 安裝相關套件

sudo apt update
sudo apt upgrade
sudo apt install samba
sudo apt install libnss-winbind
其中,libnss-winbind 可讓 Linux 解析 Windows 的主機名。

2. 相關設定檔

2.1. smb.conf

/etc/samba/smb.conf

2.1.1. 一般使用者「家」目錄設定

[homes]
   comment = Home Directories
   browseable = yes
   read only  = no
   create mode    = 0600
   directory mode = 0700

2.2. nsswitch.conf

/etc/nsswitch.conf

2.2.1. 讓 Linux 解析 Windows 的主機名

hosts:          files mdns4_minimal [NOTFOUND=return] dns wins
在 hosts 後面多加一個 wins。

3. 資源管理

3.1. 新增使用者

sudo pdbedit -a -u user_name
其中,user_name為你要給定的使用者帳號名稱。
實作範例:
cy@cyvm:~$ sudo pdbedit -a -u cy
new password:
retype new password:
Unix username:        cy
NT username:
Account Flags:        [U          ]
User SID:             S-1-5-12-12345678-123456789-123456789-1234
Primary Group SID:    S-1-5-12-12345678-123456789-123456789-321
Full Name:            CY
Home Directory:       \\cyvm\cy
HomeDir Drive:
Logon Script:
Profile Path:         \\cyvm\cy\profile
Domain:               CYVM
Account desc:
Workstations:
Munged dial:
Logon time:           0
Logoff time:          三, 06  二  2036 23:06:39 CST
Kickoff time:         三, 06  二  2036 23:06:39 CST
Password last set:    二, 24  一  2017 23:46:36 CST
Password can change:  二, 24  一  2017 23:46:36 CST
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF


4. 測試

4.1. 測試 smb.conf 設定是否正確

testparm
實作範例:
cy@cyvm:~$ testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
WARNING: The "syslog" option is deprecated
Processing section "[homes]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
        server string = %h server (Samba, Ubuntu)
        server role = standalone server
        map to guest = Bad User
        obey pam restrictions = Yes
        pam password change = Yes
        passwd program = /usr/bin/passwd %u
        passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
        unix password sync = Yes
        syslog = 0
        log file = /var/log/samba/log.%m
        max log size = 1000
        dns proxy = No
        usershare allow guests = Yes
        panic action = /usr/share/samba/panic-action %d
        idmap config * : backend = tdb


[homes]
        comment = Home Directories
        read only = No
        create mask = 0600
        directory mask = 0700
        directory mode = 0700


5. 服務管理

systemctl status smbd.service
systemctl restart smbd.service
systemctl enable smbd.service


2017年1月23日 星期一

shell script 特殊技巧收集

查看目前所使用的 shell 程式

cy@cyvm:~$ echo $0
-bash

在 Ubuntu 安裝 Node.js

1. 安裝相關套件

Node.js 6.x

sudo apt-get update
sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install nodejs
sudo apt-get install build-essential

官方參考網站: Debian and Ubuntu based Linux distributions



2. 相關設定檔

(N/A)



3. 資源管理

(N/A)



4. 測試

4.1. 查看 Node.js 版本

cy@cysrv:~$ node --version
v6.9.4

4.1. 查看 npm 版本

cy@cysrv:~$ npm --version
3.10.10

在 Debian 安裝 SSH Server

1. 安裝相關套件

sudo apt-get update
sudo apt-get install openssh-server


2. 相關設定檔

2.1. sshd_config

/etc/ssh/sshd_config

2.1.1. 安全性設定

# What ports, IPs and protocols we listen for
Port 12345

SSH 預設使用 port 22,建議更改,特別在面向網路網路使用時,幾乎只要一上網就會被攻擊。埠號可自定,不要與使用中的埠號即可。

PermitRootLogin prohibit-password

建議不要使用 root 登入 SSH,所以可將 PermitRootLogin 設為 no。但讓 root 能登入有時候滿方便的,特別是有時需要傳一些需要 root 權限的檔案時,可以省掉一些操作上的麻煩。所以這裡使用 prohibit-password,再加上使用免密碼登入方式,就可以讓 root 能登入 SSH,且能保持一定程度的安全性。

#AllowUsers cy # NOTE! this will disable root login

可限定只有某些使用者登入,值得注意的是,若沒有加入 root,將會使 root 不能登入,即使 PermitRootLogin 為 yes 或 prohibit-password 亦然。在我的 Server 並未實作此項,所以前面加 #。

2.1.2. 效能性設定

UseDNS no

加了這個據說能加速登入速度。



3. 資源管理

3.1. 最好不要使用的帳號名

以下為我曾被攻擊的帳號名,最好不要使用這些帳號,特別是 admin:

0
0000
1111
1234
123456
adam
adm
admin
agent
anonymous
apache
bill
cisco
client
customer
default
edu
ftp
ftpuser
guest
jack
john
manager
mark
master
mike
nagios
operator
oracle
osmc
pi
plcmspip
raspberry
sales
scan
service
steve
super
support
Taiwan
test
testuser
tiffany
ubnt
uploader
user
user1
usuario
vyatta
www


4. 測試

4.1. ssh 登入測試:

cy@cyvm:~$ ssh -p 12345 cy@192.168.0.1

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
No mail.
Last login: Sat Mar 18 11:25:52 2017 from 192.168.0.2

4.2. ssh 登入測試:

cykuo@cyvm:~$ scp -P 12345 -i ~/keys/cy_ssh_key test.file root@192.168.0.1:~/root_work_dir
test.file                     100%  273     0.3KB/s   00:00


5. 服務管理

安裝完會自動啟動服務,其餘服務常用操作指令如下所列。

systemctl status sshd.service
systemctl restart sshd.service
systemctl enable sshd.service