摘要:【筆記整理】CentOS常用指令整理( U ~ Z )
整理一下有關CentOS的相關指令,
基本上使用man來看會比較完整,下列只是一些簡單及常用的組合應用。
當然也會增加一些相關註解。
1. ulimit:控制系統資源
1.1. ulimit -a:顯示目前的系統資源使用限制

1.2. ulimit -d 區塊大小:程序資料區段大小的上線,單位為『KB』
1.3. ulimit -f 檔案大小:設定『shell』建立檔案大小的最大上限
1.4. ulimit -H 配額大小:『Hard limit』,嚴格設定,絕對不能超過這個設定的數值
1.4. ulimit -l 記憶體大小:設定可鎖定記憶體的上限
1.5. ulimit -m 記憶體大小:設定常駐程式的上限
1.6. ulimit -n 檔案數:檔案數的上限
1.7. ulimit -S 配額大小:『Soft limit』,警告的設定,可已超過設定的數值,若超過會有警告訊息,
一般與『-H』配合。例如:Soft 設定為80,Hard 設定為100,所以範圍為80~100,可超過80,但會有警告訊息。
1.8. ulimit -u 程序數目:單一使用者可執行程序的最大數目
1.9. ulimit -v 虛擬記憶體:『shell』可使用的虛擬記憶體最大上限
2. umask:設定新增檔案的權限遮罩
a. umask:顯示預設的檔案權限

b. umask -S:以文字表示權限遮罩下的權限設定

註:一般的權限為完整的權限(777)減去遮罩權限。Ex. 遮罩權限為022,則一般權限為777-022=755
3. umount:卸載檔案系統
a. umount /cdrom:卸載已掛載的目錄『cdrom』
b. umount -a:卸載所有定義在『/etc/mtab』內的檔案系統
4. uname:顯示系統資訊
a. uname -a:顯示所有資訊

b. uname -i:顯示硬體平台

c. uname -m:顯示硬體類別

d. uname -n:顯示主機名稱

e. uname -o:顯示作業系統

f. uname -p:顯示處理器類別

g. uname -r:顯示作業系統核心程式的版本

h. uname -s:顯示作業系統的名稱

i. uname -v:顯示系統時間

5. uniq:排序命令,檢查、計算、刪除檔案中出現重複的行列
用法:uniq [ -cdiu ]
01. cat test1 | sort | uniq:排序並刪除重複的行列

02. cat test1 | sort | uniq -c:計算重複出現的次數

03. cat test1 | sort | uniq -d:僅顯示重複出現的行列

04. cat test1 | sort | uniq -i:忽略大小寫

05. cat test1 | sort | uniq -u:僅顯示出一次的行列

6. unset:移除變數設定
a. unset -f 變數:僅將函數移除
b. unset -v 變數:僅將變數移除
7. unzip:解壓縮zip的檔案
a. unzip -l test.zip:顯示『test.zip』中所包含的檔案

b. unzip test.zip:解壓縮『test.zip』

c. unzip -t test.zip:檢查『test.zip』的正確性

d. unzip -u test.zip:更新已存在的檔案,並將壓縮檔的其他檔案解開

e. unzip -c test.zip:解壓縮到標準輸出,並將字元做適當轉換

註:參數『-p』與『-c』類似,但不會對字元做轉換
8. uptime:顯示系統已經執行的時間
a. uptime:顯示系統開機時間、使用時間/天數、目前線上使用者數、系統『Load Average』。

9. useradd:新增帳號
用法:useradd [ -u UID ] [ -g 初始群組 ] [ -G 次要群組 ] [ -mM ] [ -c 說明欄 ] [ -d 根目錄絕對路徑 ] [ -s shell ]
[ -efr ] username
01. useradd -u 1111 -d /tmp/ghoseliang -m ghoseliang:指定使用者的『UID』為『1111』及根目錄為
『/tmp/ghoseliang』

02. useradd -g test1 user1:新增帳號並指定帳號的初始群組
註:可在『/etc/passwd』看到第四欄位所屬群組的『GID』

03. useradd -G test2 user2:新增帳號並指定該帳號的次要群組
註:可在『/etc/group』看到相關資料

04. useradd -M user3:強制不建立使用者帳號根目錄

05. useradd -e 2013-03-19 ghoseliang:指定帳號的有效期限,儲存在『/etc/shadow』。

06. useradd -f 1 ghoseliang:限定密碼過期多少天後,將該使用者帳號停用

07. useradd -D:呼叫預設值或是直接查看『/etc/default/useradd』

08. for i in $(seq 1 1 10); do useradd ghoseliang${i}; echo ghoseliang${i} | passwd --stdin
ghoseliang${i}; done:使用『for』迴圈一次新增多個帳號
註:有關『for』迴圈簡易應用,請參考最下方備註的『04』

註1:『/etc/login.defs』:預設帳號基本設定

註2:『/etc/skel/』:使用者根目錄內容資料參考目錄
註:有關『/etc/passwd』、『/etc/group』、『/etc/shadow』請參考最下方備註的( 01 - 03 )
10. userdel:刪除帳號
用法:userdel [ -r ] username
01. userdel ghose:刪除使用者帳號,但不會刪除該使用者目錄

02. userdel -r ghoseliang:刪除使用者帳號並刪除該使用者根目錄下所有檔案

03. for i in $(seq 1 1 10); do userdel -r ghoseliang${i}; done:使用『for』迴圈一次刪除多個帳號

11. usermod:修改帳號設定
用法:usermod [ -cdefgGlsuLU ] username
01. usermod -c "This is test" user1:後面接帳號的說明,即『/etc/passwd』第五欄的說明欄,可以加入一些帳號
的說明

02. usermod -d /tmp/user2 [ -m ] user2:指定使用者登入時所進的目錄,並給予對該目錄的完全控制權,即修改
『/etc/passwd』第六欄
註:參數『-m』為舊目錄會搬至新目錄,若舊目錄不存在則會新建一個

03. usermod -e 2013-05-30 ghoseliang:指定帳號的有效期限,即『/etc/shadow』第八欄位

04. usermod -f 2 ghoseliang:限定密碼過期多少天後將該使用者帳號停用
註:『0』為立刻停用,『-1』為關閉此功能,預設為『-1』

05. usermod -g test1 user2:更改使用者帳號初始群組,即修改『/etc/passwd』第四欄位,亦是『GID』欄位
註:群組名稱需存在。

06. usermod -G test3 user2:修改次要群組( 支援群組 ),即修改『/etc/group』第四欄位

07. usermod -l ghose user2:修改『LoginName』

08. usermod -s /bin/csh ghoseliang:修改『登入shell』,若空白,系統則採用『預設shell』

09. usermod -u 502 ghose:修改使用者帳號的『UID』,即修改『/etc/passwd』第三欄位
註:除非使用『-o』,為『非唯一值』。數字不可為『負值』。

10. usermod -L ghoseliang:凍結使用者密碼,使其無法登入,即修改『/etc/shadow』密碼欄

11. usermod -U ghoseliang:解除凍結,即修改『/etc/shadow』密碼欄,將『!』移除

註:有關『/etc/passwd』、『/etc/group』、『/etc/shadow』請參考最下方備註的( 01 - 03 )
12. users:顯示目前登入的帳號資訊

13. vi:文書編輯器
參考網址:http://linux.vbird.org/linux_basic/0310vi.php
14. vim:文書編輯器,具程式編輯能力,可主動的以字體顏色辨別語法的正確性,
方便程式設計
參考網址:http://linux.vbird.org/linux_basic/0310vi.php
15. vmstat:顯示虛擬記憶體的狀態

a. vmstat -a:顯示使用與非使用中的虛擬記憶體狀態

b. vmstat -d:顯示磁碟狀態

c. vmstat -f:顯示開機後fork的用量

d. vmstat -s:顯示較完整的數據與統計

e. vmstat -S 單位:單位為k、K、m、M,分別以1000、1024、1000000、1048576 bytes為單位

16. w:顯示目前登入的帳號資訊

a. w -f ghoseliang:顯示使用者從何處登入

b. w -h:不顯示標題列

c. w -s:將帳號資訊用較簡潔的格式顯示

註:『USER』為登入的使用者;『TTY』為終端機;『FROM』為來源位址;『LOGIN@』為登入的時間;
『IDLE』為閒置的時間;『JCPU』為對於同一個終端機下所有程序的執行時間;
『PCPU』為『WHAT』欄位顯示的程序所執行的時間;『WHAT』為目前正在執行的工作。
17. wall:廣播訊息
a. wall 訊息:傳訊『訊息』給所有同意接受公開訊息的終端機

18. watch:週期性的執行指令並以全螢幕輸出
a. watch [指令]


b. watch -d w:標記與之前顯示的差異部分

c. watch -n 5 w:每隔5秒執行一次指令

19. wc:計算檔案的byte數、字數、列數
用法:wc [ -clLmw ] [ File ]
01. wc /etc/man.config:計算並列出檔案中的『行數』、『字數(英文字母)』、『字元數』

02. wc -c /etc/man.config:列出檔案的『byte數』

03. wc -l /etc/man.config:計算並列出檔案的『行數』

04. wc -L /etc/man.config:計算並列出檔案最長列的『字元數』

05. wc -m /etc/man.config:計算並列出檔案的『字元數』

06. wc -w /etc/man.config:計算並列出檔案的『字數(英文字母)』

20. wget:從指定的網址下載檔案
a. wget 檔案下載位址

註:從man上查看有許多參數可運用,有興趣者可自行嘗試。
21. whereis:尋找指定的檔案
a. whereis Filename

b. whereis -m Filename:尋找該檔案的說明檔

22. which:尋找指定的檔案

23. who:顯示目前登入的帳號資訊

a. who -a:顯示所有資訊

b. who -b:顯示系統最近一次重新啟動的時間

c. who -m:只列出自己登入的相關資訊

d. who -q:顯示目前登入的使用者名稱與總人數

e. who -r:顯示目前的執行等級

f. who -t:顯示最後一次修改時鐘的時間
g. who -u:顯示使用者已閒置的時間

24. write:傳送訊息給其他使用者

25. yum:套件更新管理
a. yum install 套件1 [套件2]...:安裝套件
b. yum update [套件1] [套件2]...:更新套件,若不加套件,則會更新目前已安裝的所有套件
c. yum remove 套件1 [套件2]...:移除套件
d. yum clean packages:清除暫存在『/var/cache/yum/』下的套件
e. yum list:列出所有套件
f. yum list update:列出所有可以更新的套件
g. yum list installed:列出所有已安裝的套件
h. yum check-update:檢查可以更新的套件
i. yum info:列出所有套件的資訊
j. yum grouplist:列出所有群組套件
k. yum groupinstall 套件1 [套件2]...:安裝群組套件
l. yum groupupdate 套件1 [套件2]...:更新群組套件
m. yum groupremove 套件1 [套件2]...:移除群組套件
n. yum groupinfo 套件1 [套件2]...:列出群組套件的資訊
26. zip:壓縮為zip檔
a. zip test.z /root/*:將『/root/』下所有檔案壓縮為壓縮檔『test.z』

b. zip -d test.z /root/test1:移除壓縮檔『test.z』內的檔案『test1』

c. zip -m test2.z /root/Filename:將檔案壓縮後,刪除原始檔案

27. zipinfo:顯示壓縮檔詳細資訊

備註
1. /etc/passwd:使用者帳號資訊,為7個欄位並使用冒號『:』隔開

01. 第一欄位( User Name ):使用者帳號名稱
02. 第二欄位( Password ):使用者密碼,儲存於『/etc/shadow』
03. 第三欄位( UID ):使用者號碼( User ID Number )
04. 第四欄位( GID ):使用者群組號碼( Group ID Number )
05. 第五欄位( Remark ):使用者資訊說明欄
06. 第六欄位( Home Directory ):使用者根目錄路徑,即該使用者帳號登入系統後預設登入目錄
07. 第七欄位( Shell ):使用者帳號登入後第一個執行的程序
註:每個使用者帳號會有一個唯一的識別碼稱為『UID( User ID Number)』,並從序號500開始,有關使用者帳號
資訊儲存於『/etc/passwd』、『/etc/shadow』;有關使用者群組資訊儲存於『/etc/group』、
『/etc/gshadow』。
2. /etc/group:使用者群組資訊,為4個欄位並使用冒號『:』隔開

01. 第一欄位( Group ID ):群組名稱,ex. ghoseliang
02. 第二欄位( Password ):群組密碼,儲存於『/etc/gshadow』
03. 第三欄位( GID ):使用者群組號碼( Group ID Number ),與『/etc/passwd』的『GID』欄位相對應
04. 第四欄位( Member ):群組成員,成員之間使用逗點( ,)隔開
註:每個使用者帳號會有一個唯一的識別碼稱為『UID( User ID Number)』,並從序號500開始,有關使用者帳號
資訊儲存於『/etc/passwd』、『/etc/shadow』;有關使用者群組資訊儲存於『/etc/group』、
『/etc/gshadow』。
3. /etc/shadow:使用者密碼資訊,為 9個欄位並使用冒號『:』隔開

01. 第一欄位( User Name ):帳號名稱,與『/etc/passwd』相對應
02. 第二欄位( Password ):經過編碼(加密)的密碼
03. 第三欄位( Last Password Change ):最近更動密碼的日期
04. 第四欄位( Minimum ):密碼不可被更動的天數,在最近一次被更改後需要經過幾天才可以再被變更。
05. 第五欄位( Maximum ):密碼需要重新變更的天數,指令在最近一次更改密碼後,在多少天數內需要再次的變更
密碼。
06. 第六欄位( Warn ):密碼需要變更期限前的警告天數
07. 第七欄位( Inactive ):密碼過期後的帳號寬限時間,即密碼失效日。密碼過期時,仍可使用,不過系統會強制要
求必須重新設定密碼才能登入繼續使用。但若到了密碼失效日,仍未更改密碼,則無法使用該密碼登入系統。
08. 第八欄位( Expire ):帳號失效日期
09. 第九欄位( ):保留,看以後有無新功能加入
註:每個使用者帳號會有一個唯一的識別碼稱為『UID( User ID Number)』,並從序號500開始,有關使用者帳號
資訊儲存於『/etc/passwd』、『/etc/shadow』;有關使用者群組資訊儲存於『/etc/group』、
『/etc/gshadow』。
4. 『for』簡易應用範例
01. for i in $(seq 1 1 10); do echo ${i}; done:『for』迴圈中透過『seq』來輸出數字,初始值為『1』,累加值為
『1』(每次加『1』),結束值為『10』,利用『echo』來輸出每次『seq』指定的『i』值。

02. for i in $(seq -w 1 1 10); do echo ${i}; done:加入參數『-w』來對齊數字

<......不定時更新、修改、增加中......>
參考