天天看點

UNIX系統常用指令

UNIX系統常用指令格式: 

command [flags] [argument1] [argument2] ... 

其中flags以-開始,多個flags可用一個-連起來,如ls -l -a 與ls -la相同。 



根據指令的不同,參數分為可選的或必須的;所有的指令從标準輸入接受輸入,輸出 

結果顯示在标準輸出,而錯誤資訊則顯示在标準錯誤輸出裝置。可使用重定向功能對 

這些裝置進行重定向。 



指令在正常執行結果後傳回一個0值,如果指令出錯可未完全完成,則傳回一個 

非零值(在shell中可用變量$?檢視). 在shell script中可用此傳回值作為控制邏輯 

的一部分。 



注:不同的UNIX版本的flags可能有所不同。 



1、與使用者相關的指令 

1.1 login 

(在LINUX Redhat下此指令功能與Solaris/BSD不同,執行login會退出目前任務). 



login: 

Password: 



相關檔案: 

在下面的這些檔案中設定shell運作時必要的路徑,終端類型,其他變量或特殊程式. 



$HOME/.profile (Bourne shell, sh, bash) 

$HOME/.cshrc (csh, tcsh) 

$HOME/.tcshrc (tcsh) 

/etc/passwd檔案中列出每個使用者的shell 

/etc/csh.cshrc 

/etc/csh.login 

/etc/profile (Bourne shell, bash) 

/etc/login (Bourne shell, bash) 



csh: /etc/csh.cshrc和$HOME/.cshrc每次執行都會讀取, 

而/etc/csh.login和$HOME/.login隻有注冊shell才執行 

修改相應檔案後使用 source .cshrc使能相關修改,如果修改了path則 

還需使用rehash重新整理可執行檔案hash表。 



tcsh: $HOME/.tcshrc, 沒有些檔案讀取.cshrc 



sh: /etc/profile和$HOME/.profile注冊shell 

bash: /etc/profile和$HOME/.bash_profile注冊shell讀取 

.bashrc互動式非注冊shell才讀取。 



在sh/bash下手工執行相關檔案: 

. /etc/profile 



相關檔案執行順序 

sh: /etc/profile -> $HOME/.profile 

csh/tcsh: /etc/csh.cshrc -> /etc/csh.login -> $HOME/.cshrc 

-> $HOME/.login 



變量的設定: 

sh/bash: TERM=vt100; export TERM 

OR: export TERM=vt100 (bash) 

csh: setenv TERM vt100 



常用變量: 

(1)Backspace $HOME/.profile $HOME/.cshrc 

stty erase ^H 

(2)umask 建立檔案或目錄的保護屬性 

(3)TERM 

(4)切忌PATH中加入 . 



1.2. rlogin 

與telnet類似,連接配接到遠端主機. 



rlogin remotehost [ -l loginname ] 

Or: 

rsh [-l loginname] remotehost [command ] 



相關檔案: 

遠端主機的 /etc/hosts.equiv 和 $HOME/.rhosts 



相關網絡配置檔案: 

/etc/inetd.conf檔案中的r系統服務.Redhat下為shell, login, exec, 對應 

的網絡守護程序(daemon)為in.rshd, in.rlogind, in.rexecd. 



建議: 從安全角度出發,關閉r系列服務. 



1.3. telnet 

telnet remotehost [port] 



相關檔案: 

/etc/inetd.conf檔案中的telnet服務. 

/etc/issue.net 



TIPS: strings /usr/sbin/in.telnetd |egrep issue 



1.4. passwd 更改密碼 



1.5 exit 退出目前shell 





2. 指令或檔案的查找 

相關shell變量: 

csh/tcsh: $path 

.cshrc set path=(/usr/bin /usr/local/bin $path) 

sh/bash: $PATH 

.profile .bash_profile 

PATH=/usr/local/bin:$PATH; export PATH 



2.1 which 

Syntax: which command 



which為bash/tcsh内帶指令 

[[email protected]]$ which which 

which: shell built-in command. 

2.2 where(tcsh) 

Syntax: where command 

2.3 locate (LINUX) 

Syntax: locate filename 



相關指令: updatedb更改locate檔案名資料庫 



3. 檢視指令的用法 

man 

相關檔案: 

/etc/man.config 



4. 管理者常用指令 



4.1 install 

用于安裝一個新的指令或daemon等. 一般情況下可以不用,但很多軟體在其安裝 

shell script中使用install将目标檔案複制到相應的目錄并設定正确的屬性等. 





NAME 

install - copy files and set their attributes 



SYNOPSIS 

install [options] [-s] [--strip] source dest 

install [options] [-s] [--strip] source... directory 

install [options] [-d,--directory] directory... 

Options: 

[-c] [-g group] [-m mode] [-o owner] [--group=group] 

[--mode=mode] [--owner=owner] [--help] [--version] 



4.2 shutdown 

不同的unix參數不盡相同,在linux下常用如下方式關機: 

shutdown -h now 

shutdown -r now (等同于reboot) 



4.3 halt 

poweroff 

關機,在多使用者方式下(Runlevel 3)不建議使用, 



4.4 ulimit 

korn shell和bourne shell中可用此指令,在csh系列中相應的指令為limit. 

用于限制每個程序可使用的系統資源,通常分兩種限制: 

. Hard limits 系統所定義的資源,隻有root能更改 

. 軟限制 對建立程序所使用的限制,可增加到系統的Hard Limit. 



Flags: 

-a 列出軟限制 

-Ha 列出Hard Limit 

-c size 設定coredump size的塊大小 

-t size cputime 

-f size file size 



4.5 umask 系統管理者用于設定使用者預設的umask值. 





5.與程序相關的指令 



程序基本概念: 程序與指令的執行相關,但并不是一一對應; 一條指令可能對應若幹 

個程序(如shell script, pipe等).但最簡單的指令與umask隻有一個程序. 



程序分類: 

.互動式程序:可以前台或背景執行,前背景可切換 

.批處理程序:不通過終端送出,一般将它們放在任務隊列中順序執行. 如通過 

at 和 crontab送出的任務. 

.deamon:永不終止的程序,等待響應來自其他程序的服務請求.如sendmail, 

named(DNS), POP3及apache等. 



程序的相關屬性: 

PID 

Real UID 

Effective UID (SUID) 

Real GID 

Effective GID (SGID) 

Priority(Nice Number)執行的優先級 



5.1 ps 

Process Status, 列出目前運作的程序狀态,根據選項不同,可列出所有的或部分程序 

. 無選項時隻列出從目前終端上啟動的程序(SYSV)或目前使用者($USER)的程序(BSD), 

不同的UNIX版本之間有差别. linux使用BSD版本的ps 

. BSD版本ps指令使用方法: 

ps 

ps aux 列出系統中所有的程序的詳細情況 

ps aux |egrep inetd 



輸出資訊内容: 

PID 

Terminal 如無相應終端則為- 

cpu time 

UserID or Username 

程序啟動時間或日期 

程序狀态(Stat: S(leep) R(unnable) D(uninterrupt sleep) Z(ombie) 

W 程序沒有駐留頁面, N: 程序的nice值為負值 



5.2 kill 

kill [-signal] PID 



kill -l 列出可用信号量 

常用資訊量: 

-HUP (1) 重新開機程序 

ps ax |egrep inetd 

kill -HUP pid_of_inetd 

-KILL (9) 強制中止 



PID取值含義: 

>0 指特定程序(實際程序) 

=0 同組使用者的所有process(PGID) 

=-1 Effective UID = 執行指令使用者之uid 

<0 && !=1 取絕對值之程序 



5.3 nice 

用于改變一個或多個程序的優先級; 但隻有root或提高程序的優先級, 普通使用者隻能 

降低程序的優先級. 

nice用負數表示提高優先級,而正數表明降低優先級,通常此值範圍是-20~20. 



如未指定提高優先級,nice會降低或不改變程序的優先級. 當然如果沒有權限的話 

程序的優先級不會有任何改變. 



一般情況下我們用nice來降低背景程序的優先級(預設優先級為10). 



nice find . -name "*.c" -print & 

nice 16 find . -name "*.c" -print & 



5.4 wait 

wait PID 



等待作業結束,參數為pid,在shell script中有時用到. 



5.5 nohup 

nohup command & 



5.6 sleep 



sleep seconds 



程序前背景切換: Ctrl-Z, fg, bg 





6. 通訊 



6.1 ftp 

6.2 mailx 

6.3 minicom (序列槽終端,MODEM) 



7. 檔案比較 

cmp 

comm 

diff (用于比較文本檔案) 

diff3 (比較3個檔案) 

sdiff 



8. 檔案操作 

touch 建立檔案,修改檔案日期等 

chmod 

chwon 

chgrp 

rm 慎用 rm -rf 

mv 移動檔案或改名 

cp cp -r 複制目錄 

cat 

rcp 遠端複制 

ln 預設情況下為硬連接配接,每個檔案具有相同的inode 

ln -s sourcefile targetfile 



9. 目錄操作 

mkdir [-p] [-m mode] directory 

rmdir [-p] directory 



10. 檔案資訊指令 

ls 

find 

最基本的功能是查找一個檔案名或目錄,常用格式如下: 

find . -print (類似于 ls -lR) 



find可使用如下屬性進行查找: 

-name 查找檔案名,含通配符*?的檔案名用引号括起來 

-perm 00x000 八進制檔案屬性 

-atime n n天之前通路過的檔案 

-mtime n n天之間修改過的檔案 

-ctime n 檔案的狀态在n前之間修改過 

-exec command 如指令的傳回代碼為零(找到相應的檔案)則真,command必須以 

/; 結果,此外在指令的執行中 {} 為查找到的檔案路徑名 

-ok command 與exec相類似,但在執行每個指令之間要求使用者确認 

-print 列印目前路徑名 

-newer filename 如檔案的最後修改日期較filename新則為真 

-type c c=[b,c,d,l,p,f]檔案類型 

-user username 如檔案的屬主為username則為真 

-nouser 檔案屬主在/etc/passwd檔案中不存在 

-group grouname 檔案組 



邏輯運算符: -a -o ! 



示列: 

find $HOME /( -name a.out -o -name "*.o" /) -atime +7 -exec rm {} /; 

find . -atime 0 -print 

find / .name .profile -print 

find . -perm 777 -a -mtime 7 -exec chmod 755 {} /; 





file 

more 

less 

tail (tail -f filename) 

head 

wc 

read 用于shell程式設計 

col 

pg (SYSV) 



11. 編輯器 

vi 

ed 

joe 



12. 檔案内容查找 

grep 

egrep 

正規表達式: . * ^ $ + ? [] 



strings 





13. 任務排程 

at 

atq 列出隊列中的任務 

crontab 



14. 存儲,歸納及壓縮 

compress .Z 

uncompress .Z 

cpio 

dd dd if=inputfile of=outputfile 

dd if=boot.img of=/dev/fd0H1440 

pack .z 30%-50%文本檔案 

pcat pact file.z 

gzip .gz 

gunzip 

tar tar -[txc]vf targetfile [sourcefile] 

tar -cvf target.tar sourcefilelist 

tar -tvf target.tar [filename] 

tar -xvf target.tar [filename] 

GNU TAR: 

tar -zcvf target.tar.gz sourcefilelist 

tar -zxvf target.tar.gz [filelist] 

tar -ztvf target.tar.gz [filelist] 



zcat .Z 



uuencode 

uudecode 



15. 其他指令 

date 

env 

unix2dos (linux沒有) 

dos2unix 

uname 

uptime 

free 

time 

top 



16. 文本處理 

cut 

fmt 每行格式轉化為72列,用于郵件格式化 

fold 折行處理,一行到多行,一般為80列 

join 

paste 

sort 

tr 

tr '/"' '' < file1 



#!/bin/sh 



for i in * 

do 

mv $i `echo $i |tr /[a-z]/[A-Z]/` 

done 



uniq 報告/删除檔案中相同的複制行 

sed 流編輯器 

sed 's/96/tt/' student.txt 

awk 

awk '{print $1" "$2}' sourcefile 

awk -f class.awk student.txt > linux-student.txt 

檔案class.awk内容如下: 



# 

#class.awk 

# 

BEGIN {printf "%-12s%s/n","班級","學号 姓名"; 

printf "-------------------------------------------/n/n"} 

/[1-9]+/B*$/ {class=$0} 

/^9[5-8]+/ {printf "%-12s%s/n", class,$0 | "sort"} 

#Enf of class.awk 



awk -f traffic.awk traffic.txt 

檔案traffic.awk内容如下: 

# 

#traffic.awk 

# 

{ if ( $2 < 10000 ) t_0 += $2; 

if ( $2 > 10000 && $2 < 50000) t_10 += $2; 

if ( $2 > 50000 && $2 < 100000) t_50 += $2; 

if ( $2 > 100000) t_100 += $2; 

total += $2 } 

END {printf "t_0 = %dKB %5.2f/%/n",t_0,t0*100/total; 

printf "t_10 = %dKB %5.2f/%/n",t_10,t10*100/total; 

printf "t_50 = %dKB %5.2f/% /n",t_50,t_50*100/total; 

printf "t_100 = %dKB %5.2f/%/n",t_100,t_100*100/total; 

printf "Total = %dKB/n", total} 



#End of traffic.awk 





17. 網絡配置指令及故障排除指令 

17.1 ifconfig 

Interface Config , 網卡配置指令, 相關檔案:/proc/net/dev 

詳細使用說明: man ifconfig 



示例: 

#ifconfig 

lo Link encap:Local Loopback 

網卡辨別 封裝類型: 本地回環 



inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0 

IP位址:127.0.0.1 廣播位址:127.255.255.255 子網路遮罩:255.0.0.0 



UP BROADCAST LOOPBACK RUNNING MTU:3584 Metric:1 

已啟動 接受廣播 本地回環 正在運作 最大傳輸單元: 3584 路由距離向量:1 



RX packets:718 errors:0 dropped:0 overruns:0 frame:0 

已接受資料包:718 



TX packets:718 errors:0 dropped:0 overruns:0 carrier:0 

已發送資料包:718 



collisions:0 

碰撞:0 



eth0 Link encap:Ethernet HWaddr 00:80:C8:4C:6A:D0 

網卡辨別 封裝類型: Ethernet 硬體(MAC)位址: 00:80:C8:4C:6A:D0 



inet addr:202.118.66.81 Bcast:202.118.66.255 Mask:255.255.255.0 

IP位址:202.118.66.81 廣播位址:202.118.66.255 子網路遮罩:255.255.255.0 



UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 

已啟動 接受廣播 正在運作 多點廣播 最大傳輸單元: 1500 路由距離向量:1 



RX packets:13900 errors:0 dropped:0 overruns:0 frame:0 

已接受資料包:13900 



TX packets:5859 errors:0 dropped:0 overruns:0 carrier:0 

已發送資料包:5859 



collisions:0 

碰撞:0 



Interrupt:10 Base address:0xe400 

中斷(IRQ):10 端口位址: 0xe400 



#ifconfig eth0 顯示eth0的相關資訊 

#ifconfig -a 顯示所有網絡裝置的配置資訊 

#ifconfig eth0 down Down掉eth0 

#ifconfig eth0 202.118.66.81 broadcast 202.118.66.255 [up] 

#ifconfig eth0 202.118.66.81 broadcast 202.118.66.255 netmask 255.255.255.0 

#ifconfig eth0 up 



17.2 route 

路由表維護指令, 相關檔案: /proc/net/route 



$ /sbin/route 

Kernel IP routing table 

Destination Gateway Genmask Flags Metric Ref Use Iface 

localnet * 255.255.255.0 U 0 0 49 eth0 

192.168.1.0 * 255.255.255.0 U 0 0 655 eth1 

192.168.2.0 * 255.255.255.0 U 0 0 498 eth2 

192.168.3.0 * 255.255.255.0 U 0 0 825 eth3 

127.0.0.0 * 255.0.0.0 U 0 0 13 lo 

default olive.dlut.edu. 0.0.0.0 UG 1 0 4834 eth0 



#route add default gw 202.118.66.1 

#route add default gw 202.118.66.1 eth0 

#/sbin/route add -net 202.118.68.0 netmask 255.255.252.0 gw 202.118.66.16 

#/sbin/rouet del default 202.118.66.1 

#/sbin/route del -net 202.118.68.0 netmask 255.255.252.0 gw 202.118.66.16 





17.3 netstat 

顯示主機目前路由表, 相關檔案: /proc/net/route 

netstat -rn 

/home/hbwork[102]netstat -rn 



Routing Table: 

Destination Gateway Flags Ref Use Interface 

-------------------- -------------------- ----- ----- ------ --------- 

202.118.70.27 202.118.66.16 UGHD 0 1 

202.118.69.254 202.118.66.16 UGHD 0 1 

202.118.68.243 202.118.66.16 UGHD 0 1 

202.118.70.21 202.118.66.16 UGHD 0 0 

202.118.71.10 202.118.66.16 UGHD 0 1 

202.118.71.204 202.118.66.16 UGHD 0 1 

202.118.68.160 202.118.66.16 UGHD 0 1 

202.199.128.52 202.118.66.254 UGHD 0 2 

202.118.68.66 202.118.66.16 UGHD 0 1 

202.118.69.69 202.118.66.16 UGHD 0 1 

202.118.69.228 202.118.66.16 UGHD 0 1 

202.118.71.68 202.118.66.16 UGHD 0 1 

202.118.70.37 202.118.66.16 UGHD 0 1 

202.118.66.0 202.118.66.18 U 3 12259 hme0 

224.0.0.0 202.118.66.18 U 3 0 hme0 

default 202.118.66.1 UG 0 70354 

127.0.0.1 127.0.0.1 UH 0 41316 lo0 



Destionation: 目标網絡或主機 

Gateway: 下一個路由,認為距離目标較近的路由的IP位址,在資料傳送時将發往 

這一IP位址. 

Flags: 

U Router is up, 目标可達 

H Specific router,到特定主機的路由 

G 此路由為其他路由進行間接通路到的,如果沒有G标志則表明相應的 

路由器或主機是直接連接配接在相應的路由器上的. 

D 此路由是ICMP協定的路徑重定向資訊所建立的. 

M 由ICMP之重定向資訊所修改 



REFS: 在此路由上現在正在使用的連結數,這些連接配接可能是由連續時間較長的ftp或 

telnet任務, 每個使用tcp的服務或應用在執行時此列值均加1. 



Use: 自TCP/IP啟動以來通過此路由器的資料包量. 



Interface: 網卡邏輯名,UNIX不同取名不同. 





17.4 nslookup 

Name Server Lookup, DNS伺服器診斷工具 

使用示列: 

[[email protected] hbwork]$ nslookup www.dlut.edu.cn
 

Server: cedrus.dlut.edu.cn 

Address: 202.118.66.6 



Name: peony.dlut.edu.cn 

Address: 202.118.66.18 

Aliases: www.dlut.edu.cn
 



[[email protected] hbwork]$ nslookup 

Default Server: cedrus.dlut.edu.cn 

Address: 202.118.66.6 



> www.dlut.edu.cn
 

Server: cedrus.dlut.edu.cn 

Address: 202.118.66.6 



Name: peony.dlut.edu.cn 

Address: 202.118.66.18 

Aliases: www.dlut.edu.cn
 

> set q=ns #查詢相應域的DNS伺服器 

> dlut.edu.cn #輸入要查詢的域名 

Server: cedrus.dlut.edu.cn #預設域名伺服器為cedrus.dlut.edu.cn 

Address: 202.118.66.6 #參考/etc/resolv.conf檔案 



dlut.edu.cn nameserver = gingko.dlut.edu.cn 

dlut.edu.cn nameserver = olive.dlut.edu.cn 

dlut.edu.cn nameserver = cedrus.dlut.edu.cn 

gingko.dlut.edu.cn internet address = 202.118.66.8 

olive.dlut.edu.cn internet address = 202.118.68.1 

olive.dlut.edu.cn internet address = 202.118.69.1 

olive.dlut.edu.cn internet address = 202.118.70.1 

olive.dlut.edu.cn internet address = 202.118.71.1 

olive.dlut.edu.cn internet address = 202.118.66.16 

cedrus.dlut.edu.cn internet address = 202.118.66.6 



> dl.lnpta.net.cn #查詢dl.lnpta.net.cn的域名伺服器 

Server: cedrus.dlut.edu.cn 

Address: 202.118.66.6 



Non-authoritative answer: 

dl.lnpta.net.cn nameserver = ns.lnpta.net.cn 



Authoritative answers can be found from: 

ns.lnpta.net.cn internet address = 202.96.64.68 



> server ns.lnpta.net.cn 

Default Server: ns.lnpta.net.cn 

Address: 202.96.64.68 



server: ns.lnpta.net.cn 

Address: 202.96.64.68 



Non-authoritative answer: 

www.dlut.edu.cn
 canonical name = peony.dlut.edu.cn 

> dlut.edu.cn #查詢域dlut.edu.cn的MX記錄 

Server: ns.lnpta.net.cn 

Address: 202.96.64.68 



Non-authoritative answer: 

dlut.edu.cn preference = 1, mail exchanger = gingko.dlut.edu.cn 



Authoritative answers can be found from: 

dlut.edu.cn nameserver = gingko.dlut.edu.cn 

dlut.edu.cn nameserver = CEDRUS.dlut.edu.cn 

dlut.edu.cn nameserver = olive.dlut.edu.cn 

gingko.dlut.edu.cn internet address = 202.118.66.8 

CEDRUS.dlut.edu.cn internet address = 202.118.66.6 

olive.dlut.edu.cn internet address = 202.118.71.1 

olive.dlut.edu.cn internet address = 202.118.66.16 

olive.dlut.edu.cn internet address = 202.118.68.1 

olive.dlut.edu.cn internet address = 202.118.69.1 

olive.dlut.edu.cn internet address = 202.118.70.1 

> exit 



17.5 ping 

TCP/IP ICMP(Internet Control Message Protocol)診斷工具 



ping [hostname|IpAddress] 



Error Message: host unreachable 

network unreachable 



[[email protected] hbwork]$ ping 202.118.66.1 

PING 202.118.66.1 (202.118.66.1): 56 data bytes 

64 bytes from 202.118.66.1: icmp_seq=0 ttl=255 time=23.1 ms 

64 bytes from 202.118.66.1: icmp_seq=1 ttl=255 time=2.1 ms 

64 bytes from 202.118.66.1: icmp_seq=2 ttl=255 time=1.9 ms 



--- 202.118.66.1 ping statistics --- 

3 packets transmitted, 3 packets received, 0% packet loss 

round-trip min/avg/max = 1.9/9.0/23.1 ms 



17.6 hostname 

顯示或設定主機名 



17.7 domainname 

顯示或設定主機域名 



17.8 traceroute 

Windows 95: tracert 



traceroute hostname 

traceroute destionation_ip_address 

17.9 arp 

顯示或設定相應主機/ip位址的mac位址 

相關檔案: /proc/net/arp 

cat /proc/net/arp 



$arp hostname 

$arp -a 

$arp ip_address 



顯示或設定主機名需要root權限 

#arp -s hostname eth_address [temp] [pub] 

#arp -d hostname 

#arp -d ip_address 

#arp -f mac_ip_map_file 常用檔案名為/etc/ether