当前位置:文档之家› UNIX系统常用命令

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, 没有些文件读取.cshrcsh: /etc/profile和$HOME/.profile注册shellbash: /etc/profile和$HOME/.bash_profile注册shell读取.bashrc交互式非注册shell才读取。

在sh/bash下手工执行相关文件:. /etc/profile相关文件执行顺序sh: /etc/profile -> $HOME/.profilecsh/tcsh: /etc/csh.cshrc -> /etc/csh.login -> $HOME/.cshrc-> $HOME/.login变量的设置:sh/bash: TERM=vt100; export TERMOR: export TERM=vt100 (bash)csh: setenv TERM vt100常用变量:(1)Backspace $HOME/.profile $HOME/.cshrcstty 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. telnettelnet remotehost [port]相关文件:/etc/inetd.conf文件中的telnet服务./etc/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_profilePATH=/usr/local/bin:$PATH; export PATH2.1 whichSyntax: which commandwhich为bash/tcsh内带命令[hbwork@toshiba]$ which whichwhich: shell built-in command.2.2 where(tcsh)Syntax: where command2.3 locate (LINUX)Syntax: locate filename相关命令: updatedb更改locate文件名数据库3. 查看命令的用法man相关文件:/etc/man.config4. 管理员常用命令4.1 install用于安装一个新的命令或daemon等. 一般情况下可以不用,但很多软件在其安装shell script中使用install将目标文件复制到相应的目录并设置正确的属性等.NAMEinstall - copy files and set their attributesSYNOPSISinstall [options] [-s] [--strip] source destinstall [options] [-s] [--strip] source... directoryinstall [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 nowshutdown -r now (等同于reboot)4.3 haltpoweroff关机,在多用户方式下(Runlevel 3)不建议使用,4.4 ulimitkorn 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 size4.5 umask 系统管理员用于设置用户默认的umask值.5.与进程相关的命令进程基本概念: 进程与命令的执行相关,但并不是一一对应; 一条命令可能对应若干个进程(如shell script, pipe等).但最简单的命令与umask只有一个进程.进程分类:.交互式进程:可以前台或后台执行,前后台可切换.批处理进程:不通过终端提交,一般将它们放在任务队列中顺序执行. 如通过at 和 crontab提交的任务..deamon:永不终止的进程,等待响应来自其他进程的服务请求.如sendmail, named(DNS), POP3及apache等.PIDReal UIDEffective UID (SUID)Real GIDEffective GID (SGID)Priority(Nice Number)执行的优先级5.1 psProcess Status, 列出当前运行的进程状态,根据选项不同,可列出所有的或部分进程. 无选项时只列出从当前终端上启动的进程(SYSV)或当前用户($USER)的进程(BSD), 不同的UNIX版本之间有差别. linux使用BSD版本的ps. BSD版本ps命令使用方法:psps aux 列出系统中所有的进程的详细情况ps aux |egrep inetd输出信息内容:PIDTerminal 如无相应终端则为-cpu timeUserID or Username进程启动时间或日期进程状态(Stat: S(leep) R(unnable) D(uninterrupt sleep) Z(ombie)W 进程没有驻留页面, N: 进程的nice值为负值5.2 killkill [-signal] PIDkill -l 列出可用信号量常用信息量:-HUP (1) 重启进程ps ax |egrep inetdkill -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 waitwait PID等待作业结束,参数为pid,在shell script中有时用到.5.5 nohupnohup command &5.6 sleepsleep seconds进程前后台切换: Ctrl-Z, fg, bg6. 通讯6.1 ftp6.2 mailx6.3 minicom (串口终端,MODEM)7. 文件比较cmpcommdiff (用于比较文本文件)diff3 (比较3个文件)sdiff8. 文件操作touch 创建文件,修改文件日期等chmodchwonchgrprm 慎用 rm -rfmv 移动文件或改名cp cp -r 复制目录catrcp 远程复制ln 默认情况下为硬连接,每个文件具有相同的inodeln -s sourcefile targetfile9. 目录操作mkdir [-p] [-m mode] directoryrmdir [-p] directory10. 文件信息命令lsfind最基本的功能是查找一个文件名或目录,常用格式如下: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 -printfind / .name .profile -printfind . -perm 777 -a -mtime 7 -exec chmod 755 {} \;filemorelesstail (tail -f filename)headwcread 用于shell编程colpg (SYSV)11. 编辑器viedjoe12. 文件内容查找grepegrep正规表达式: . * ^ $ + ? []strings13. 任务调度atatq 列出队列中的任务crontab14. 存储,归纳及压缩compress .Zuncompress .Zcpiodd dd if=inputfile of=outputfiledd if=boot.img of=/dev/fd0H1440pack .z 30%-50%文本文件pcat pact file.zgzip .gzgunziptar tar -[txc]vf targetfile [sourcefile] tar -cvf target.tar sourcefilelisttar -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 .Zuuencodeuudecode15. 其他命令dateenvunix2dos (linux没有)dos2unixunameuptimefreetimetop16. 文本处理cutfmt 每行格式转化为72列,用于邮件格式化fold 折行处理,一行到多行,一般为80列joinpastesorttrtr '\"' '' < file1#!/bin/shfor i in *domv $i `echo $i |tr /[a-z]/[A-Z]/`doneuniq 报告/删除文件中相同的复制行sed 流编辑器sed 's/96/tt/' student.txtawkawk '{print $1" "$2}' sourcefileawk -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.awkawk -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.awk17. 网络配置命令及故障排除命令17.1 ifconfigInterface Config , 网卡配置命令, 相关文件:/proc/net/dev详细使用说明: man ifconfig示例:#ifconfiglo Link encap:Local Loopback网卡标识封装类型: 本地回环inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0IP地址:127.0.0.1 广播地址:127.255.255.255 子网掩码:255.0.0.0UP BROADCAST LOOPBACK RUNNING MTU:3584 Metric:1已启动接受广播本地回环正在运行最大传输单元: 3584 路由距离向量:1 RX packets:718 errors:0 dropped:0 overruns:0 frame:0已接受数据包:718TX packets:718 errors:0 dropped:0 overruns:0 carrier:0已发送数据包:718collisions:0碰撞:0eth0 Link encap:Ethernet HWaddr 00:80:C8:4C:6A:D0网卡标识封装类型: Ethernet 硬件(MAC)地址: 00:80:C8:4C:6A:D0inet addr:202.118.66.81 Bcast:202.118.66.255 Mask:255.255.255.0IP地址:202.118.66.81 广播地址:202.118.66.255 子网掩码:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1已启动接受广播正在运行多点广播最大传输单元: 1500 路由距离向量:1RX packets:13900 errors:0 dropped:0 overruns:0 frame:0已接受数据包:13900TX packets:5859 errors:0 dropped:0 overruns:0 carrier:0已发送数据包:5859collisions:0碰撞:0Interrupt: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 . 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/routenetstat -rn/home/hbwork[102]netstat -rnRouting Table:Destination Gateway Flags Ref Use Interface-------------------- -------------------- ----- ----- ------ --------- 202.118.70.27 202.118.66.16 UGHD 0 1202.118.69.254 202.118.66.16 UGHD 0 1202.118.68.243 202.118.66.16 UGHD 0 1202.118.70.21 202.118.66.16 UGHD 0 0202.118.71.10 202.118.66.16 UGHD 0 1202.118.71.204 202.118.66.16 UGHD 0 1202.118.68.160 202.118.66.16 UGHD 0 1202.199.128.52 202.118.66.254 UGHD 0 2202.118.68.66 202.118.66.16 UGHD 0 1202.118.69.69 202.118.66.16 UGHD 0 1202.118.69.228 202.118.66.16 UGHD 0 1202.118.71.68 202.118.66.16 UGHD 0 1202.118.70.37 202.118.66.16 UGHD 0 1202.118.66.0 202.118.66.18 U 3 12259 hme0224.0.0.0 202.118.66.18 U 3 0 hme0default 202.118.66.1 UG 0 70354127.0.0.1 127.0.0.1 UH 0 41316 lo0Destionation: 目标网络或主机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 nslookupName Server Lookup, DNS服务器诊断工具使用示列:[hbwork@helius hbwork]$ nslookup Server: Address: 202.118.66.6Name: Address: 202.118.66.18Aliases: [hbwork@helius hbwork]$ nslookupDefault Server: Address: 202.118.66.6> Server: Address: 202.118.66.6Name: Address: 202.118.66.18Aliases: > set q=ns #查询相应域的DNS服务器> #输入要查询的域名Server: #默认域名服务器为 Address: 202.118.66.6 #参考/etc/resolv.conf文件 nameserver = nameserver = nameserver = internet address = 202.118.66.8。

相关主题