大魚

          UNIX命令

          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內帶命令
          [hbwork@toshiba]$ 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服務器診斷工具
          使用示列:
          [hbwork@helius 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

          [hbwork@helius 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

          [hbwork@helius 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
           

          posted on 2009-03-13 23:10 大魚 閱讀(330) 評論(0)  編輯  收藏 所屬分類: unix


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 武鸣县| 曲沃县| 仙桃市| 东平县| 名山县| 吉木萨尔县| 江源县| 金门县| 景泰县| 新安县| 莱芜市| 潼关县| 阿克苏市| 郴州市| 沂水县| 调兵山市| 织金县| 安仁县| 合肥市| 张家川| 邹平县| 长岛县| 阿巴嘎旗| 桃园市| 保山市| 海口市| 耒阳市| 内丘县| 渝中区| 肥东县| 两当县| 延吉市| 明星| 绥德县| 北安市| 梧州市| 兴海县| 景宁| 平邑县| 邹城市| 郁南县|