1
Unix 實務
Ka-Lok Ng (吳家樂)
Department of Bioinformatics
Taichung Healthcare and Management University
klng@thmu.edu.tw
(O) 04-23323456 x1856
圖資大樓 3408 室
2
教學目標
Teach the student how to use Linux system using TEXT mode and set up a secure computing
environment.
課程大綱
Red Hat Linux operating system, basic Unix command: file and directory, vi editor, Shell 的種類,
system performance, system security, firewall (iptables, SQUID)
評量方式
專題分數佔 30%,平常分數 10% (習題繳交和學習勤惰考核),期中考分數佔 30%,期末考
分數佔 30%。
1 93/02/15~93/02/21 History of Unix, Simple UNIX command
2 93/02/22~93/02/28 File system
3 93/02/29~93/03/06 File and Directory Permission
4 93/03/07~93/03/13 Pipe and Filter
5 93/03/14~93/03/20 sort 排序
、正規表示法、
萬用字元 (Wild card 或 Metacharacter)
6 93/03/21~93/03/27
搜尋指令 grep
7 93/03/28~93/04/03 工作控制(Process Control)
8 93/04/04~93/04/10 vi 編輯器
9 93/04/11~93/04/17 Mid-term
10 93/04/18~93/04/24 Shell 的種類 , Unix 的環境設定檔
11 93/04/25~93/05/01 Linux 平台安全技術, Linux 防火牆(iptables, SQUID)
資訊安全技術及標準, 對稱式金鑰,非對稱式金鑰,
12 93/05/02~93/05/08
PHP and MySQL
13 93/05/09~93/05/15 專題分組報告
14 93/05/16~93/05/22 專題分組報告
15 93/05/23~93/05/29 專題分組報告
16 93/05/30~93/06/05 專題分組報告
17 93/06/06~93/06/12 專題分組報告
18 93/06/13~93/06/19 Final examination
TERM PAPER TOPICS 專題題目
例如:Apache+PHP+MySQL, Samba server, Firewall (iptables), E-mail server, FTP server, Proxy server,
system performance (benchmark) 。 評分:
Powerpoint 及 word 書面報告一份 。
分組口頭報告約 30 分鐘,每組最多三人。
書面報告一份 (報告需有班級、題目、目錄、內容、總結、參考文獻)
不可全部抄襲他人結果(如論文,網頁內容),多用自己的文字寫。
參考文獻
吳佳諺工作室,Red Hat Linux 8 玩家寳典,文魁出版 NT 690
翁仲岳編著,Unix 完全參考指南,文魁出版 NT 690
3
1.1 History of Unix
- 1978 AT&T, Bell Lab. http://www.bell-labs.com/
- by Ken Thompson and Dennis Ritchie
- Resource sharing, open system
- Two versions of Unix : BSD (Berkeley), SVR4 (System V Release 4, AT&T)
- TCP/IP protocol
- Commercial Unix : IBM – AIX, HP – HP-UX, Silicon Graphics – IRIX, Sun Microsystem –
Solaris, DEC - Ultrix
- Free Unix – Linux (L. Torvalds), FreeBSD
- Free Softwares (Richard Stallman), GNU= GNU’s Not Unix, 自由軟體基金會,
GPL =General Public License
Some peoples home pages
http://www.levenez.com/unix/
1.2 The uniqueness of UNIX
The features that made UNIX a hit from the start are:
Multitasking capability
Multi-users capability
Portability
UNIX programs
Library of application software
Multitasking capability
Lets a computer do several things at once.
4
Multi-users
The same design that permits multitasking permits multiple users to use the computer. The
computer can take the commands of a number of users
System portability
A major contribution of the UNIX system was its portability, permitting it to move from one
brand of computer to another with a minimum of code changes.
UNIX tools
- UNIX comes with hundreds of programs
- UNIX e-mail
- UNIX systems around the world were linked into a world wide web decades before the
development of today's World Wide Web.
Applications libraries
Today there are hundreds of UNIX applications that can be purchased from third-party vendors,
in addition to the applications that come with UNIX.
1.3 How UNIX is organized
The UNIX system is functionally organized at three levels:
The kernel, which schedules tasks and manages storage;
The shell, which connects and interprets users' commands, calls programs from memory, and
executes them; and
The tools and applications that offer additional functionality to the operating system
User
User
User
References
http://www.bell-labs.com/history/unix/tutorial.html
5
Unix/Linux command
How to login in ?
Software : Netterm, Netscape or IE
IP address : 192.168.xxx.xxx 210.70.81.70
Username : test
Password : 111111
Use the delete key, not backspace key to correct typo error.
Terminal type 查詢你用的 Shell 程式及終端機型態
Unix 指令皆小寫,參數皆大寫
$ env|more
$ echo $SHELL
$ echo $TERM
$ setenv TERM vt100 (In Unix, every object is a file !)
Command
查詢有關自己的資料
查詢系統使用狀況
$ mkdir your-seat-number.dir
(mkdir means make directory, for example mkdir 05.dir)
$ whoami
albert
$ who am I
root pts/1 Oct 3 16:18
$ id
uid=503(albert) gid=0(root) 群組=0(root)
$ users (何人在線上 ?)
albert
$ who (何人在線上 ? )
albert pts/1 Oct 3 16:18 (192.192.106.241)
$ w (何人在線上 ? )
4:26pm up 3 days, 4:41, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
albert pts/1 192.192.106.241 4:18pm 1.00s 0.40s 0.08s w
The JCPU time is the time used by all processes attached to the tty. It does not include past
background jobs, but does include currently running background jobs.
The PCPU time is the time used by the current process, named in the "what" field.
6
$ man w
$ finger
$ users
$ tty (終端機名稱)
$ date
File system
/ means the root directory
other directories
bin/ dev/ home/ lost+found/ proc/ sbin/ usr/
boot/ etc/ lib/ mnt/ root/ tmp/ var/
/ (根目錄, root directory)
bin/ boot/ dev/ etc/ home/ /lib proc/ …….
albert/ st3c/
file1, file2 …
關於 directory 及 file 的事實
系統中有那些目錄, 目錄中有多少個檔案, 檔案的名稱, date of creation, size (in block or
in Bytes), file type (executable, text, symbolic link), access permission (存取權限, read, write,
executable), 檔案擁有人, 檔案所屬群組, 檔案壓縮
command meaning
$ pwd present working directory
$ cd / change directory
$ ls list files and directories
$ ls -aCF * means executable files, @ means symbolic link, / means directory,
empty means file
$ cd /bin
$ ls -a list all the files, include hidden files。 . 代表目前的目錄 , .. 代
表上一層目錄
The ls command
$ ls –a
$ ls –a /
$ ls –a /home
$ ls t* ( * 萬用字元, wild card)
$ ls -aF
$ cd /bin; ls –l (long list, files –rwxrwxrwx, directory drwxrwxrwx, symbolic link
lrwxrwxrwx)
$ ls –s ( s means count the file block size, 1 block = 1024 Bytes)
$ ls -sF
$ ls –CF ( list file names in column )
7
$ ls –a /home
$ ls –ar (r means reversing the alphabetic order)
$ ls –artl (rt means reversing the time order)
$ ls –R (R means recursive 遞迴地列出)
$ ls -R /etc (ls –R /bin 或 ls-R /home)
$ touch test ( 產生一檔案,大小為 0 byte)
$ cd ( change to the default working directory, /home/st3c/ )
Exercise
Step 1 cd 座號.dir
Step 2 use the touch command to create files : file1 then file2.
Step 3 Use ls –t and other option, list file1 and file2 in sequential order (time and alphabetic order).
du (present working directory & sub-directories usage, same as
du –k, in block = 1kB = 1024 Bytes)
$ du -s . (s means total size in blocks, . means the current directory)
$ du –b . (b means size in Bytes)
$ du –s /etc (permission denied)
(do not try du –s / , because it may take a long time !)
$ du /etc (some of the files have no permission, 不能給出沒有讀取權限
的檔案)
$ df ( disks free 磁碟空間, same as df –k, in block = 1kB = 1024 Bytes)
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda8 257598 29278 215016 12% /
/dev/hda1 19485 2278 16201 12% /boot
/dev/hda6 909178 306460 555747 36% /home
/dev/hda5 909178 352616 509591 41% /usr
/dev/hda7 257598 9792 234502 4% /var
| | | | | |
disk device total size used 可用 百分比 mount point
File compression
$ compress –v today ( v means 列出壓縮百分比)
today: -- replaced with today.Z Compression: 44.64%
$ compress –d today.Z ( d means decompress 解壓縮)
$ gzip –v today
today: 63.3% -- replaced with today.gz
$ gzip –d filename.gz ( d means decompress 解壓縮)
8
File and Directory Permission
[albert@imlinux ~]$ ls -l
total 4
drwxr-xr-x 2 albert root 1024 Sep 18 11:32 J Builer
-rw-r--r-- 1 albert adm 605 Sep 5 14:54 Xrootenv.0
-rwxrwxrwx 共十個欄位
1 234 567 8910
- rwx rwx rwx
d,l,-,b,c,p,s owner group others
檔案類型 擁有人, 所屬群組 其它使用者
d means directory, l means symbolic link, - means file, b means 區塊式特殊檔, c means 字元式特殊檔,
p means pipe, s means socket
變更檔案存取權限
u=user, g=group, o=others, a=all, +=add, -=remove, = =set, r = read, w = write, x = execute,
$ chmod ugo+rwx filename ( chmod => change mode, 即變更檔案存取權限, ugo+rwx => 檔
案開放給所有使用者使用)
$ ls -l
$ chmod ugo-rwx filename
$ ls –l
Permission type 存取權限
Permission Binary Base 10
--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
r-- 100 4
r–x 101 5
rw- 110 6
rwx 111 7
$ chmod 755 filename (存取權限 rwxr-xr-x)
$ chmod 744 filename (存取權限 rwxr--r--)
umask 指令設定初始檔案和目錄存取權
$ umask
22
binary representation of umask value : 022
022 000 010 010
9
+ 777 + 111 111 111
755 111 101 101
in terms of rwx
初始目錄存取權 rwx r-x r-x
022 000 010 010
+ 666 + 110 110 110
644 110 100 100
初始檔案存取權 rw- r-- r- -
File and directory – make, copy, rename, delete, read
目錄與檔案 - 產生、拷貝、重新命名、刪除及閱讀
$ mkdir mydir.dir
$ mkdir /tmp/mydir.dir
$ cp .login login.copy
$ ls –al
$ cp .cshrc cshrc.copy
$ cp login.copy cshrc.copy dir2.dir
$ ls –l dir2.dir
$ mv login.copy new.login
$ ls –l
$ mkdir test.dir
$ rm test.dir
$ rm file1 file2 ( 小心使用 rm 指令 !)
$ rm –i file1 (interactive mode)
Unix command Option
mkdir -m 744
cp cp options [ source file] [target file]
cp options [ source dir.] [target dir.]
例 cp –vR sfile tfile
cp –vR sdir tdir
mv mv –v sdir tdir
rm rm –vi filename
閱讀檔案內容
$ head –4 .cshrc (head –n filename)
$ tail –n file1 file2
$ cat file1
$ more file1
$ more /etc/passwd
10
File type
$ cd /usr
$ ls -F
$ file tmp
$ cd lib
練習:使用 file 查詢 lib 內檔案類型
Pipe and Filter 管線與過濾器 (, >>, append >> or test (將 ls –l 輸出至檔案 test)
$ more test
$ wc wc-cshrc
$ cat wc-cshrc
$ wc –l .cshrc (計算 .cshrc 檔案行數, lines)
$ wc –w .cshrc (計算 .cshrc 檔案字數, words)
$ wc –c .cshrc (計算 .cshrc 檔案字元數, characters)
Q. Find the total number of files in a directory. 目前目錄內有多少個檔案 ?
$ ls –l
$ ls –l |wc -l
Q. Find the total number login users . 系統目前有多少個使用者連線 ?
$ who
$ who|wc –l
Q. Find the total number of users’ account. 系統內有多少個帳號 ?
$ cat /etc/passwd
練習題:Find the total number of symbolic link files under /lib. ________個。
sort 排序 --- 按資料的字母排序
規則 : 字母大寫
字母小寫
數字排序
$ sort -b omit blank
-d 以字典排序
-f 忽略大小寫
-n 以數字由小至大排序
-r 顛倒排序
$ cd /
11
$ ls –lCF
$ ls –lCF | sort –f
Q. 目錄中那一個檔案最大 ?
$ cd /home
$ ls –s
$ ls –s | sort –n
$ ls –s | sort –nr | head –10 ( sort the top 10 files )
練習題:請將/boot 目錄下的檔案及檔案夾按英文字母 (不分大小寫)順序排列。 使用指令為:
__________________________________。
查詢 Unix 指令使用之方法 – 說明頁
apropos
whatis
man
man –k
man –f
System V Unix 說明維護頁組織
節區號碼 範圍 指令*
1 使用者命令 ls, chmod
1M 系統維護命令 infocmp
2 系統呼叫 umask
3 程式庫 TclCommandWriting
4 維護用檔案 fd
5 雜項 groff_font
6 遊戲 intro
7 I/O 及特殊檔 Samba
8 維護命令 bdflush
*使用 apropos disk, apropos device, apropos game, apropos io, apropos 7, apropos 1m 查詢
練習題:請寫出 ls 的說明頁(man page)行數、字數及字元數。
使用之指令: _____________________,行數 ________,字數 ________,字元數 ________。
萬用字元 (Wild card 或 Metacharacter)、正規表示法、搜尋指令 grep
1. 萬用字元使用方法 ( ? * [ ] )
2. 搜尋指令 grep 及正規表示法(Regular Expression)
1. 萬用字元使用方法
* 表示配到任何長度字串或數字
12
? 表示配到任何單一字元
$ cd /
$ ls
$ cd h*
$ cd b* (有 error,因為 bin 及 boot 兩個目錄均為 b 開始 )
$ echo *
$ echo h*
$ ls ?lan (輸出 flan plan )
$ ls plan? (輸出 plane plans )
$ ls ?a?d (輸出 card land )
$ ls arrow* (輸出 arrow arrows )
$ ls a*ow (輸出 allow arrow )
$ ls b* c* (輸出 bon boot c )
$ ls –d b* d*
$ ls –d [bd]* ( [ ] 對單一字元範圍配對)
$ ls –d [bd] * (不能有空格)
$ ls –d [a-z]* (對 a 到 z 字元範圍配對)
$ ls –d [a-m]* (對 a 到 m 字元範圍配對)
$ ls -d *[+]*
特殊符號 Special character ( $ ‘’ `` ; () \ )
$ echo $shell ($ 代表輸出變數值)
$ echo ‘$shell ’ (‘’英文稱謂 Quotes,‘’代表輸出’’的內容,包括
空白及不理會特殊符號)
$ echo ‘* *shell’
$ echo Your dir is `pwd` (``英文稱謂 Back Primes 或 grave symbols,
``代表輸出``內的指令內容)
$ ls; ls –a; ls –A (; 代表連續執行多個指令)
$ (echo my home directory; pwd; ls)>home.txt
(‘’英文稱謂 Parentheses,()把多個指令組合在一起,通常把結果的內容,輸出至一檔案儲
存)
$ echo \*
( \ 英文稱謂 Backslash,代表不理會特殊符號的正常功能)
$ echo \$shell
2. 搜尋指令 grep 及正規表示法(Regular Expression)
grep 用來搜尋檔案內某些字串
使用語法: grep 選項(n 代表列出行數) 搜尋之字串 檔案名
先查看 /etc/passwd 內容
標記法 意義 例
13
A 如…A, A… grep A /etc/passwd
/ 或 : 或 ! 如 ../.. 或 ..:. 或 ..!.. grep –n / /etc/passwd
:2 如 :2 grep –n :2 /etc/passwd
\\ 特殊符號 \ grep –n \\ /etc/passwd
\* 特殊符號 * grep –n ‘\*’ /etc/passwd | wc
\^ 特殊符號 ^ grep –n ‘\^’ /etc/passwd
\$ 特殊符號 $ grep –n ‘\$’ /etc/passwd
\[ 特殊符號 [ grep –n ‘\[’ /etc/passwd
\] 特殊符號 ] grep –n ‘\]’ /etc/passwd
^st 行首字為 st 開頭(必需完全相同)grep –n ^st /etc/passwd
ot:$ 行尾字為 ot:結尾(必需完全相同)grep –n ‘ot:$’ /etc/passwd
c* c 出現 0 次或以上 grep –n ‘9*’ /etc/passwd
列出所有的,因為 9 可以出
現0次
搜尋指令 egrep
尋找多種樣式,egrep = expression grep
標記法 意義 例
a|b a或b egrep ‘y|z’ /etc/passwd
a|b|c a或b 或c egrep ‘x|y|z’ /etc/passwd
c+ c 出現一次或以上 egrep ‘9+’ /etc/passwd
搜尋結果等於
grep ‘99*’ /etc/passwd
練習題:用 grep 搜尋檔案。 於 ls 的說明頁內 user 這個字曾出現於多少行中? ________行。
又請問符號*共出現於 ls 的說明頁多少次? __________次。
工作控制(Process Control)
$ jobs
$ ps
終止及啟動工作
$ vi test
ctrlz
$ jobs (there are suspended jobs)
$ fg (foreground)
$ jobs (no more jobs)
$ ls –al>lsal.txt& (& means background jobs)
$ vi test1 及 ctrlz
$ vi test2 及 ctrlz
$ vi test3 及 ctrlz
$ jobs (three suspended jobs)
14
$ %1 或 fg%1
正在執行的工作 (Process Status, ps, BSD 選項:a,x)
$ ps (顯示連上此系統之 terminal user 所有正在執行的工作)
PID TTY TIME CMD
10702 pts/0 00:00:00 csh
10863 pts/0 00:00:00 ps
PID = Process ID
TTY = 正在執行此工作的 Terminal
TIME = 此工作至目前所需執行的時間
CMD = 指令
$ ps –x
(列出 user 透過或不透過 terminal 連線之工作狀態及正在執行的工作等資訊)
$ ps –u (usage, 列出 user 名字、CPU、RAM、工作狀態等資訊)
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
albert 28663 0.0 3.5 2824 1100 pts/0 S 16:01 0:00 -csh
albert 28702 0.0 2.8 2512 876 pts/0 R 16:26 0:00 ps –u
工作狀態 STAT 意義
R Running 執行中
S Sleeping 暫停執行
T Traced or stopped 停止執行
Z Zombies 已被結束之工作,但其父工作未正常停止此工作
$ ps –a
(all, 顯示連上此系統之 terminal user 及其它 users 正在執行的工作)
$ ps –ax| more (此系統所有正在執行的工作)
$ ps –e| more (此系統所有正在執行的工作)
$ ps –el | more (long list 此系統所有正在執行的工作)
$
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
100 S 0 1 0 0 60 0 - 274 do_sel ? 00:00:04 init
040 S 0 2 1 0 60 0 - 0 bdflus ? 00:00:04 kflushd
040 S 0 3 1 0 60 0 - 0 kpiod ? 00:00:00 kpiod
PPID = Parent Process ID
15
PRI = Priority
NI = Nice Value
$ nice -5 wc –l filename
移除正在執行的工作
$ ps (先查詢正在執行的工作)
$ kill PID
$ kill –9 PID
資訊及檔案搜尋 (find)
$ find . –name ‘string’ –print
(從現在的目錄開始搜尋 csh 出現於那個檔案夾,並顯示結果)
$ find / -name ‘csh’ –print
(從/目錄開始搜尋 csh 出現於那個檔案夾,並顯示結果)
$ find . –mtime 7 –print (搜尋 exactly 7 天前被更改的檔案)
$ find . –mtime -7 –print (搜尋 less than 7 天前被更改的檔案)
$ find . –mtime +7 –print (搜尋 longer than 7 天前被更改的檔案)
比較兩個檔案的差異 diff
$ diff filename1 filename2
檢查拼字 spell
$ spell –v filename (顯示所有拼錯的字)
別名 alias (execute in the present shell only)
使用別名定義新命令完成你要達成的工作
$ alias (查詢系統已定義的別名)
$ alias lsa=‘ls –a’ ( for bash, need = sign)
$ alias lscf=‘ls –CF’
$ alias (查詢新增的別名)
$ unalias lscf (刪除已定義的別名 lscf)
vi 編輯器
兩種模式
1. 命令模式(command mode)
2. 插入模式(insert mode),可開始編輯文件內容
使用字母
選項
$ vi 進入 命令 插入模式
Esc 鍵
16
$ ls –l > demo
$ vi demo
離開 vi、冒號(:)的配合指令、透過!與 Unix 溝通
vi 編輯時並不是直接修改文件內容,而是把檔案拷貝至系統的進行 buffer 修改,直到要儲
存編輯的內容時,才把 buffer 內容寫入檔案。 故此你可以不作修改,使檔案維持原貌。
指令 結果
:w 儲存檔案
:w file1 儲存至 file1 檔案
:q 離開 vi,不作任何自上次使用 w 後的更改
:q! 離開 vi,不作任何更改
:wq 儲存檔案及離開 vi
:set nu 顯示文件中的每一行的號碼
:set nonu 不顯示文件中的每一行的號碼
:r file1 在目前位置加入 file1 所有內容
:e filename 開啟另一檔案編輯
:!ls / 暫時離開 vi,使用單一 Unix 指令查詢,如列出/ 的
檔案
:sh 暫時離開 vi,可使用多個 Unix 指令查詢,完成後按
完成後按 Ctrl+d Ctrl+d 回到 vi
:n,k w file1 將文件中第 n 行至第 k 行儲存至 file1 新檔案
:n,k w! file1 將文件中第 n 行至第 k 行儲存至 file1 已存在檔案
:n,k w>>file1 將文件中第 n 行至第 k 行附加至 file1 檔案
移動游標
使用 keyboard 的箭頭移動游標。
在頁間移動游標
$ ls –l >output
$ vi output
按鍵 結果
Ctrl+f Forward 游標向下移動一個畫面
Ctrl+b Backward 游標向上移動一個畫面
Ctrl+u Up 游標向下移動半個畫面
Ctrl+d Down 游標向上移動半個畫面
nG 游標移至第 n 行
G 游標移至檔案尾端
w 游標移至下一個字的開頭
b 游標移至上一個字的開頭
17
^或 0 游標移至行首
$ 游標移至行尾
編輯檔案及刪除文字
指令 結果
i Insert 游標前插入文字
a Append 游標後插入文字
o Open a new line 游標下方開一新行
O Open a new line 游標上方開一新行
r replace 取代字元
r 檔名 游標下方加入另一檔案內容至本檔案
cw change word 更改字串
cc 更改一整行的內容
J Join 游標所在的一行與下一行合併
x 刪除游標所在字元
#x 刪除游標所在及之後 3 個字元
dw Delete word 刪除一個字(word)
D Delete 刪除自游標行尾所有文字
dd 刪除一整行
3dd 刪除三整行
配合 p paste 在游標下方貼上三整行內容(搬移)
3yy Yank, Copy 3 lines 拷貝三整行
配合 p 在游標下方貼上三整行內容
u undo 回復剛才修改的一個動作
U 回復游標所在一整行的更改動作
搜尋與取代
指令 結果
/hello 往後搜尋檔案中 hello 一字
再按
n 代表 next 搜尋檔案中下一個 hello 一字
N 搜尋檔案中前一個 hello 一字
?hello 往前搜尋檔案中 hello 一字
再按
n 代表 next 搜尋檔案中前一個 hello 一字
N 搜尋檔案中下一個 hello 一字
:1,$ s/a/A/g 從第一行至最後一行的所有”a”取代
為”A”,g 代表 global 即全部
:!sed ‘s/^/>’ 在每一行行首加入”>”
18
$ vi –r filename (file recover)
$ vi +3 filename (move vi to the 3rd line)
$ vi +$ filename (move vi to the last line)
$ vi –Rfilename (read only, cannot edit)
$ vi +/usr filename (move to /usr)
練習題:
先切換至你的目錄,然後使用 vi 建立下列內容的檔案(可用中文或英文)。
Name : ?? ??? ???? student ID: ??
I am a year three student at Ling Tung College, major in Information Management.
I am a year three student at Ling Tung College, major in Information Management.
I am a year three student at Ling Tung College, major in Information Management.
I am a year three student at Ling Tung College, major in Information Management.
I am a year three student at Ling Tung College, major in Information Management.
Name :
Sex :
Birthday :
Education :
Working Experience :
…………..
在此處加入 ls –l / 檔案內容至本檔案。
註:利用 yy 及 p 兩個指令拷貝及貼上
19
其它基本指令 ( # 代表 root 的權限)
$ whereis ls /bin/ls
$ tree directory_name list contents of directories in a tree-like format
$ uname -a print system information (Linux kernel information)
$ dmesg|more print the system CPU, cache, memory, network interface hardware information
$ su 變更為管理員身份
# su test 變更為一般使者之身份
# sync moving the buffer data to hard disk
# shutdown –h shutdown the system
-r reboot after shutdown
# reboot 重新開機
# halt –p shutdown the system
# chown test.test filename change file owner and group ownership
# chgrp test root filename change file group ownership
顯示系統之使用情况
$ top see man top for explanation
$ sar 5 2 collect, report system activity information every two seconds five times
$ sar –r 5 2 collect, report system activity information for memory usage
$ vmstat report virtual memory statistics
$ free –tb display amount of free and used memory in the system (b=in byte, m=in MB)
$ free -o disable the display of the buffer memory
系統管理指令
# mount 掛載檔案系統,如用來等掛載 Floppy, CD 等週邊設備
# mount -t iso9660 /dev/cdrom /mnt/cdrom
iso9660 是 CD 之檔案格式
20
# umount umount –v /mnt/cdrom (v 執行時顯示過程)
# crontab 設定某一時間為使用者做某一件例行性的指令動作
# crontab –e
- edit the current crontab using the editor specified by the VISUAL or EDITOR environment
variables
# crontab –l
- causes the current crontab to be displayed on standard output
# crontab –r
- causes the current crontab to be removed
範例
列舉磁帶內容之指令:gtar tvf tapelib1:/dev/rmt/5
磁帶備份之指令:gtar cvf tapelib1:/dev/rmt/5 /export/home/ftp/d1
gtar 配合 crontab 使用方法
為了要維護 FTP 站定期保存最新的資料,所以需配合 crontab 之使用,crontab 使用方
法如下:
先查詢,echo $EDITOR
為了讓 crontab 的使用可用 vi 模式編改,首先需在.bashrc 檔內加入
EDITOR=vi
export EDITOR
儲存後,回到主畫面,再執行 bash, 使 crontab -e 可使用 vi 模式編改
執行 crontab –e 編輯所要定時執行的配置檔,例如
0 14 * * 0-6 /usr/local/bin/gtar cvf tapelib1:/dev/rmt/5 /export/home/ftp/d1>backup.log
這代表每天(0-6,Sun-Sat.)下午 2 時 0 分(0 14)會執行 gtar 的指令,把備份的目
錄記錄在 backup.log 的檔案中
建立群組及使用者帳號
# groupadd –g 999 imtest (groupadd 路徑為 /usr/sbin/, 999 is group ID,不同組用不同 gid)
# useradd –u 999 –g imtest –d /home/imtest imtest (useradd 路徑為 /usr/sbin/, 999 is user ID)
# passwd imtest
# userdel imtest
查詢磁碟空間限制
$ quota –g (查詢系統中所有群組之磁碟空間限制)
$ quoat –u std1 (查詢系統中使用者 std1 之磁碟空間限制)
檔案備份
$ tar 先把檔案或 directory 打包, tar [參數] [備份檔案] [來源檔案或 directory]
例:
$ tar cvf test.tar test
c產生新的備份檔, v顯示過程, 把 test directory 備份成 test.tar
$ gzip test.tar 產生 test.tar.gz
$ tar zxvf test.tar.gz 解 gzip 壓縮(-z)及還原(-x)
21
網路相關指令
$ ping ping –c 5 –i 2 –s 128 (IP or DNS)
$ traceroute or /usr/sbin/ traceroute
$ traceroute www.kimo.com.tw 找尋封包到主機的路徑
$ netstat
$ netstat –t
$ netstat –r
$ netstat –a
$ ifconfig
$ nslookup www.thmu.edu.tw
$ ftpwho
$ ftpcount
$ rpm –ivh 套件名稱 安裝套件
$ rpm –e 套件名稱 移除套件