LINUX选修课讲义:UNIX常用命令

时间:2022-01-24 08:02:30
发信人: albin (阿乐斌), 信区: LINUX标  题: LINUX选修课讲义:UNIX常用命令发信站: 碧海青天 (Wed Apr 21 23:14:19 1999), 转信版权声明:    本文内容为大连理工大学LINUX选修课讲义,欢迎大家转载,但禁止使用本材料进行    任何商业性或赢利性活动。转载时请保留本版权声明。    作者:何斌武,hbwork@dlut.edu.cn,大连理工大学网络中心,April 1999.    URL: ftp://ftp.dlut.edu.cn/pub/PEOPLE/albin/------------------------------------------------------------------------------四、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 issue1.4. passwd 更改口令1.5 exit 退出当前shell2. 命令或文件的查找   相关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.config4. 管理员常用命令4.1 install        用于安装一个新的命令或daemon等. 一般情况下可以不用,但很多软件在其安装shell script中使用install将目标文件复制到相应的目录并设置正确的属性等.        NAME       install - copy files and set their attributesSYNOPSIS       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, bg6. 通讯6.1 ftp6.2 mailx6.3 minicom (串口终端,MODEM)7. 文件比较   cmp   comm   diff (用于比较文本文件)   diff3 (比较3个文件)   sdiff8. 文件操作   touch  创建文件,修改文件日期等   chmod   chwon   chgrp   rm   慎用 rm -rf   mv   移动文件或改名   cp   cp -r 复制目录   cat   rcp 远程复制   ln  默认情况下为硬连接,每个文件具有相同的inode       ln -s sourcefile targetfile9. 目录操作   mkdir [-p] [-m mode] directory   rmdir [-p] directory10. 文件信息命令    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    joe12. 文件内容查找   grep   egrep         正规表达式:  . * ^ $ + ? []   strings   13. 任务调度   at   atq   列出队列中的任务   crontab14. 存储,归纳及压缩    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    uudecode15. 其他命令    date    env    unix2dos (linux没有)    dos2unix    uname    uptime    free    time    top16. 文本处理    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           碰撞:0eth0      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 up17.2 route     路由表维护命令, 相关文件: /proc/net/route$ /sbin/routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Ifacelocalnet        *               255.255.255.0   U     0      0       49 eth0192.168.1.0     *               255.255.255.0   U     0      0      655 eth1192.168.2.0     *               255.255.255.0   U     0      0      498 eth2192.168.3.0     *               255.255.255.0   U     0      0      825 eth3127.0.0.0       *               255.0.0.0       U     0      0       13 lodefault         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.1617.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.cnServer:  cedrus.dlut.edu.cnAddress:  202.118.66.6Name:    peony.dlut.edu.cnAddress:  202.118.66.18Aliases:  www.dlut.edu.cn     [hbwork@helius hbwork]$ nslookupDefault Server:  cedrus.dlut.edu.cnAddress:  202.118.66.6> www.dlut.edu.cnServer:  cedrus.dlut.edu.cnAddress:  202.118.66.6Name:    peony.dlut.edu.cnAddress:  202.118.66.18Aliases:  www.dlut.edu.cn > set q=ns           #查询相应域的DNS服务器> dlut.edu.cn        #输入要查询的域名Server:  cedrus.dlut.edu.cn    #默认域名服务器为cedrus.dlut.edu.cnAddress:  202.118.66.6         #参考/etc/resolv.conf文件dlut.edu.cn     nameserver = gingko.dlut.edu.cndlut.edu.cn     nameserver = olive.dlut.edu.cndlut.edu.cn     nameserver = cedrus.dlut.edu.cngingko.dlut.edu.cn      internet address = 202.118.66.8olive.dlut.edu.cn       internet address = 202.118.68.1olive.dlut.edu.cn       internet address = 202.118.69.1olive.dlut.edu.cn       internet address = 202.118.70.1olive.dlut.edu.cn       internet address = 202.118.71.1olive.dlut.edu.cn       internet address = 202.118.66.16cedrus.dlut.edu.cn      internet address = 202.118.66.6 >dl.lnpta.net.cn        #查询dl.lnpta.net.cn的域名服务器Server:  cedrus.dlut.edu.cnAddress:  202.118.66.6Non-authoritative answer:dl.lnpta.net.cn nameserver = ns.lnpta.net.cnAuthoritative 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.cnAddress:  202.96.64.68Non-authoritative answer:www.dlut.edu.cn canonical name = peony.dlut.edu.cn > dlut.edu.cn           #查询域dlut.edu.cn的MX记录Server:  ns.lnpta.net.cnAddress:  202.96.64.68Non-authoritative answer:dlut.edu.cn     preference = 1, mail exchanger = gingko.dlut.edu.cnAuthoritative answers can be found from:dlut.edu.cn     nameserver = gingko.dlut.edu.cndlut.edu.cn     nameserver = CEDRUS.dlut.edu.cndlut.edu.cn     nameserver = olive.dlut.edu.cngingko.dlut.edu.cn      internet address = 202.118.66.8CEDRUS.dlut.edu.cn      internet address = 202.118.66.6olive.dlut.edu.cn       internet address = 202.118.71.1olive.dlut.edu.cn       internet address = 202.118.66.16olive.dlut.edu.cn       internet address = 202.118.68.1olive.dlut.edu.cn       internet address = 202.118.69.1olive.dlut.edu.cn       internet address = 202.118.70.1 >exit17.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_address17.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--[m[1;32m※ 来源:.碧海青天 bbs.dlut.edu.cn.[FROM: apple.dlut.edu.c][m