RHCE133实验(系统管理)

原贴:http://blog.chinaunix.net/u/23204/showart_165062.html

 

 
RHCE133实验(系统管理)
 
 

奉献Red Hat Linux 133 实验翻译(KevinZ)

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


###请转贴时保留以下内容######
Red Hat Linux 133 实验部分
应广大RH爱好者的要求,奉献Red Hat Linux 133 实验翻译
请提出宝贵意见Mail:kissingwolf@hotmail.com

KevinZ(kissingwolf)
2004.1.4
#############################

Lab1 硬件和安装

步骤一:准备计算机
任务: 使用Red Hat Linux光盘启动系统
在启动时进入BIOS界面
设置系统启动顺序为A,CDROM,C
修改其他任何推荐的设置
保存并退出BIOS设置
步骤二:使用Anaconda(图形模式)安装Red Hat Linux
任务: 按照以下要求从光盘安装Red Hat Linux。注意,要初始化图形安装界面可能需要等待一两分钟。
l 使用检测到的鼠标配置(除非老师另有指定)
l 选择全新安装
l 使用定制安装选项
l 选择使用Disk Druid手动分区,删除所有原有的分区
l 使用以下分区方案:
n /boot 100M
n / 256M
n /usr 1000M
n swap 512M
n /var 400M
l 格式化所有分区,但是不选择检查坏块
l 使用默认的启动加载器设置(除非老师另有指定),不创建启动加载器密码
l 为网络设置选择DHCP,选择启动时激活
l 使用默认防火墙配置
l 选择适当的语言支持
l 设置对应的时区,根据老师的指示设置UTC
l 设置根密码为redhat
l 启用MD5和shaow密码模式(默认验证设置)
l 选择安装X window,不选任何其他组件
l 切换到tty5查看文件系统格式化的过程(使用Ctrl-Alt-5,用Alt-7切换回安装向导)
l 创建启动软盘可选
l 使用检测到的显示器和图形卡设置(除非老师另有指定)
l 配置使用图形界面登录
l 在安装结束后重启,完成初始化设置,不注册Red Hat Network

安装结束后启动系统,以root帐号登录,并检查以下文件:
l /var/log/messages
l /var/log/dmesg

以上安装的系统使用twm视窗管理器。通过后续的实验,我们将安装更多的软件包,实现功能的扩展和界面的美观

步骤三:使用NFS,FTP或HTTP安装Red Hat Linux
任务: 破坏现有的系统,重新安装Red Hat Linux。事先准备安装光盘1或从老师那里得到启动的介质。

破坏现有系统:
cat /var/log/messages > /dev/hda; reboot
cat /var/log/messages > /dev/sda; reboot

重启后使用启动介质启动,按照以下要求安装(由于已经覆盖了分区表,系统将警告没有找到分区表,必须重新初始化)

1. 使用CD启动
2. 在boot提示下回车
3. 选择对应的语言(English)
4. 在OK提示下回车
5. 选择对应的键盘(US)
6. 在OK提示下回车
7. 选择对应的安装方式(NFS镜像,FTP,HTTP)
8. 配置TCP/IP,选择“使用动态IP配置(BOOTP/DHCP)”
9. 在OK提示下回车
10. 根据选择的安装方式输入对应的信息:

FTP方式
FTP站点名称:192.168.0.254
Red Hat目录:pub/

HTTP方式
Web站点名称:192.168.0.254
Red Hat目录:pub/

NFS方式
NFS服务器名:192.168.0.254
NFS加载点:/var/ftp/pub

11. 这时Anaconda会读取安装镜像并检测显示器和鼠标的类型,显示欢迎界面
12. 选择定制安装
13. 使用diskdruid分区。使用以下分区方案:
/boot 100M
/ 2000M
swap 512M
/home 3 × 256M RAID0

14. 启动加载器,时区,图形,防火墙和验证方式都是用默认设置,除非教师指定
15. 设置适当的语言
16. 设置root密码为redhat
17. 安装默认的软件包

*指南中的分区方案非常重要,否则可能出现意料外的结果。


Lab2 Linux文件系统
目标:熟悉文件系统相关知识和技能

步骤一:创建和加载文件系统
任务:
1. 使用fdisk –l 得到ev/hda的分区尺寸信息。计算硬盘上没有分区的空间尺寸
2. 使用fdisk新增一个512M的逻辑分区(使用w命令将改动写入磁盘)。这个新分区的设备名是/dev/had_ ?为什么?
3. 重启以确定改动后的分区表被读入
4. 使用mke2fs,在新建的分区上创建一个新的ext2文件系统。创建时使用2k的块,每4k一个inode的设置。可能需要查看mke2fs的man page
5. 创建目录/data,作为该文件系统的加载点
6. 使用mount命令把新文件系统加载到/data。把/etc/passwd复制到/data,检查确认复制成功
7. Umount /data
8. 使用e2label为新分区指定卷标:
e2label /dev/hdax /data x是新创建分区的序号
9. 在/etc/fstab文件中为加载/data加入以下行:
LABEL=/data /data ext2 defaults 1 2
或者
/dev/hdax /data ext2 defaults 1 2
以上两行的实际效果相同。但是,如果更换了硬盘的总线或者是更改了主从的顺序,在fstab中使用卷标仍然可以定位这个设备
10. 加载新的分区
mount /data
11. 复制文件或使用touch 创建文件

步骤二:把ext2转换为ext3
1. 键入sync。这个命令把磁盘缓存信息写入磁盘。通常这个命令是定期执行的,但是以下步骤可能会先占自动同步。
2. 使用reset键重启动,或者使用电源开关关闭再打开(正常情况下不要这样)
3. 如果出现“Repair filesystem”的提示,尝试使用e2fsck /dev/hdx 修复文件系统
4. 成功启动后,通过创建journaling inode日志把ext2文件系统转换为ext3。因为ext3 的数据完整性和文件系统完整性大大增强了,所以可以 把自动预加载(pre-mount)基于时间的定期文件系统检查。
Tune2fs –j –c 0 –i 0 /dev/<partition>
5. 检查文件系统的characteristics
tune2fs –l /dev/<partition>
6. 编辑/etc/fstab /data的相关行,把文件系统由etx2更改为ext3
7. 卸载并用ext3重新加载文件系统,确认使用了ext3:
umount /data ; mount /data
df –T /data
8. 确认/boot下初始的虚拟盘镜像中包含了必要的ext3模块和jbd日志模块。如果/data是你的机器上的第一个ext3文件系统,很可能initrd中并不包含这些模块。这种情况只当我们需要ext3的支持而在initrd中又不包括相关的模块时非常重要。假设出现了这种情况,我们可以制作一个/boot/initrd-<version>.img文件:
mkinitrd –f –v /boot/initrd-$(uname –r).img $(uname –r)
9. 键入sync,然后手动重启动系统。
10. 观察启动过程。系统检查了哪个文件系统?在/data文件系统,是否看到了“recovering journal”提示信息?当不正常重启时,使用ext3的日志恢复是否比ext2的fsck快了?

步骤三:使用autofs自动加载系统
1. 确认iptables已经关闭
2. 编辑/etc/auto.master文件,去掉对/misc一行的注释
3. 在/etc/auto.misc文件中增加一行,用于加载server1.example.com的/var/ftp/pub目录到本机的目的/server1. 可以参考ftp.example.com行的示例.
4. 重启autofs服务 service autofs restart
5. 测试/misc/server1目录


Lab 3 管理启动
目标: 定制系统服务的技巧

步骤一: 使用chkconfig禁用服务
1. 使用chkconfig检查系统服务的状态: chkconfig --list
2. 使用以下示例将isdn在所有runlevel关闭
chkconfig --del <service name>
3. 使用--help 查看chkconfig语法信息 chkconfig --help. 关闭runlevel3和runlevel5的kudzu服务
4. 观察 on 和 --add 的差异, off 和 --de 的差异
chkconfig isdn --list
chkconfig isdn on
chkconfig isdn --list
chkconfig isdn off
chkconfig isdn --list
chkconfig isdn --del
chkconfig isdn --list
chkconfig isdn --add
chkconfig isdn --list
5. 使用chkconfig查看系统服务的状态和改变状态

步骤二: 更改系统登录标题
1. 我们将设置rc.local脚本用于每次重启时出现登录标题. 打开/etc/rc.local文件找到以下行:
touch /var/lock/subsys/local
2. 在后面插入以下行:
echo “ Welcome to /n” > /etc/issue
echo “All access to this computer is monitored” >> /etc/issue
echo “Unauthorized access is prohibited” >> /etc/issue
echo >> /etc/issue
echo “Last reboot complete at $(/bin/date)” >> /etc/issue
3. 保存文件,把/etc/issue复制为/etc/issue.orig
4. 重启动系统
5. 当系统启动后,切换到虚拟控制台确认登录标题出现了. 打开/etc/issue, 注意mingetty把/n扩展为你的主机名

步骤三: 更改默认runlevel
1. 编辑/etc/inittab文件,将默认runlevel从5改为3,如下:
id:3:initdefault:
2. 重启动系统.发生了什么?
3. 把默认runlevel改为5,重启动系统

步骤四: 增加当天的消息
1. 编辑/etc/motd文件,默认应为空. 增加以下行:
##################################
# Welcome to station xx #
##################################
<date> The sysadmin is playing today.
Expect frequent system downtime.
2. 切换到虚拟控制台登录.


Lab 4 用户和组管理
目标: 用户和组管理的技巧

步骤一: 创建用户和组
1. 使用useradd命令,为以下用户创建帐号:Joshua, alex, dax, bryan, zak, ed, manager. 为每个用户设置一个密码.
2. 使用groupadd命令,增加以下组: 并使用 –g 选项设定对应的GID
group gid
sales 10000
hr 10001
web 10002
为什么不用系统默认的gid?
3. 使用usermod命令把joshua和alex增加到sales组, dax和bryan到hr组, zak和ed到web组. 把manager加入所有组. 使用 –G选项.
4. 用各帐户登录,使用id命令确认组成员身份. 还有什么方法可以确认?

步骤二: 设置共享文件夹
1. 创建/depts目录,在目录下创建sales, hr, web文件夹
mkdir –p /depts./{sales,hr,web}
2. 使用chgrp命令设置对应组拥有对应文件夹
chgrp sales /depts/sales
3. 设置/depts目录的权限为755, 子文件夹的权限为770
4. 设置各部门的子文件夹的sgid, 使得创建的文件所有权为对应的组
shmod g+s /depts/sales
5. 使用各个帐号登录,并在对应位置创建文件,检查效果.只有manager可以进入所有的目录.也可以使用su -命令,但是要加上-,并且su下一个帐户之前要退出前一个帐户.

步骤三: 设置磁盘配额
1. 创建名为filehog的帐户,并设置用户在/home目录有60个inode的软限制和100个inode的硬限制. 使用以下命令测试, 要使用su - 命令,否则会失败.
su – filehog
quota
for I in $(seq 1 100); do echo –n “file$(i)”; touch file$(i) 2 >&1; done | less
quota

quota命令会报告当前的限制和已经使用的inode. 使用循环的目的是创建100个文件. 因为当创建用户filehog的时候需要从/etc/skel复制一些文件,所以创建100个文件的命令将不会成功.
如果quota设置成功,你会看到一系列反馈的数字直到达到软限制的数目. 超过限制后,会看到一个警告,但是命令仍然能够执行.达到硬限制后,将得到出错信息,而且不能再创建任何文件. 可以用<shift><Page Up>回滚检查输出,并且用ls命令查看filehog的主目录.
2. 创建一个名为diskhog的用户, 设置用户在/home目录的软限制为4MB, 硬限制为5MB,使用以下命令测试
su – diskhog
quota
dd if=/dev/zero of=bigfile count=3 #将成功
dd if=/dev/zero of=bigfile count=4 #将成功
dd if=/dev/zero of=bigfile count=5 #将失败
观察quota命令的输出. 注意当超出inode软限制和block软限制的输出差别.达到inode软限制时用户得到警告,而达到block软限制时是不同的.

步骤四: 客户端NIS
1. 使用authconfig配置系统为NIS客户. 设置notexample为NIS域名, 服务器为192.168.0.254
2. 试用guest200x为帐号从虚拟控制台登录, x为座位编号.发生了什么? 如果登录失败,检查设置.如果还有问题,确认教师机的设置正确.当验证成功后,你将看到shell的目录为/. 因为没有在本地创建帐号,所以没有主目录. 当用户帐户使用目录服务的时候会出现这种情况,无论是NIS,LDAP还是SMB
3. 使用autofs解决主目录问题.用户主目录位于server1.example.com,我们可以加载基于NFS的共享来提供用户环境.首先编辑/etc/auto.master,增加以下行
/home/guests /etc/auto.guests --timeout=60
这条记录告诉自动加载器(内核模块之一)/home/guests由它控制.所有相关的加载设置保存在/etc/auto.guests文件中,而且如果60秒内没有活动就自动卸载.
4. 创建并编辑/etc/auto.guests文件.增加以下行:
* -rw, soft, intr 192.168.0.254:/home/guests/&
这条记录指明在目录下的所有子目录都配置为从192.168.0.254:/home/guests下的相应目录加载,并且设为:读写,如果加载不成功就返回超时,并且在加载不可用时仍允许进程访问文件.
5. 配置autofs在runlevel3,4,5时运行,并手动启动:
chkconfig autofs on
service autofs start
6. 登录并查看是否主目录自动加载.可以试验登录到附近的其他机器.你将可以在notexample域内的任何一台机器上获得自己的用户环境.
7. 在自己的机器上以root登录,,使用su –guest200x.是否提示输入密码? 这意味着本地root帐号和NIS域之间是什么关系?

linux

配额方案:
1. 编辑/etc/fstab, 用usrquota代替defaults, 然后执行 mount –o remount /home
2. 创建一个用于保存用户配额的数据库(-c 可以在第一次运行quotacheck时隐藏警告)
touch /home/aquots.user
quotacheck –c /home
3. 打开内核强制配额
quotaon /home
4. 设置EDITOR变量为你希望的文本编辑器
5. edquota filehog 设置硬节点限制为100,软节点限制为60
6. edquota diskhog 设置软块限制为4096,硬块限制为5120

Lab 5 静态网络设置
目标: 手动配置网络设置的技巧
步骤一: 设置IP地址
1. 使用ifdown命令关闭网卡
ifdown eth0

2. 用文本编辑器打开/etc/sysconfig/network-scripts/ifcfg-eth0, 按以下内容更改(x为你的座位号)
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.0.x
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
3. 查看/etc/resolv.conf的内容.里面应该包含从DHCP服务器得到的设置.如果没有.改为以下设置:
search example.com
nameserver 192.168.0.254
4. 使用ifup启动配置好的网卡
ifup eth0
5. 使用ping server1 确认网络配置
6. 重启动系统,使用ping server1 确认网络配置

Lab 6 系统管理工具
目标: 掌握系统管理工具的使用以及对CUPS的管理

步骤一: 使用at和cron
1. 设置一个提示,提醒今天中午12:00去吃午餐. 在root提示符下.使用以下命令:
at noon; (回车)
echo “Time for lunch with Joe.” (回车)
2. 使用atq命令检查任务队列,确认
3. 使用at命令在5分钟后运行 df –k命令
4. 设置今天每10分钟检查一次系统状态用于检查性能问题.你怀疑时内存或者IO问题,所以要进行相关的监控. 使用root帐户,并使用crontab –e 命令编辑cron文件
5. 在文件种加入以下行:
*/10 8-17 * * * /usr/bin/free; /usr/bin/iostat
6. 如何把来自cron的输出到一个邮件地址?
7. 以root身份使用pine,mail或mutt检查使用受到了来自at和cron任务的邮件
8. 成功后删除cron任务

步骤二: 日志记录到一个集中的位置
这个实验需要和相邻计算机的配合.
1. 首先设置syslogd接受远程消息. 编辑/etc/sysconfig/syslog文件:
SYSLOGD_OPTIONS=”-R –M 0”
2. 重启动syslogd:
service syslog restart
3. 设置syslogd把消息发向远程机器: 在/etc/syslog.conf文件种增加以下行:
user.* @stationx
4. 重启动syslogd:
service syslog restart
5. 使用logger命令生成syslog消息,测试设置:
logger –i –t yourname “this is a test”
这条消息是否出现在相邻机器的/var/log/messages文件中?

步骤三: 使用dump/restore恢复单个文件
1. 准备用dump备份/boot目录下的文件. 使用df /boot查看/boot所在的设备(以下假设为/dev/hda1)
2. 首先确认备份需要的空间. 查看一个0级备份需要的字节数,使用 -S
# dump -oS /dev/hda1
3. 备份到文件而非磁带. 确认在/var/tmp目录是否有足够的空间,执行
# dump -0u –f /var/tmp/dumpfile /dev/hda1
4. 检查/etc/dumpdates,查看完全备份的时间戳.
5. 使用restore检查备份文件的内容
# restore –tf /var/tmp/dumpfile
6. 我们可以使用restore的互动模式恢复特定文件到一个临时目录.
# mkdir /tmp/restored; cd /tmp/restored
# restore –if /var/tmp/dumpfile
7. 这时会看到一个restore > 提示符. 键入help查看可用命令的列表. 使用ls和cd命令查看备份文件的列表.
8. 使用add,选中/grub.menu.1st和/grub/grub.conf文件.列出所在目录,恢复的文件应该带有星号.
9. 键入extract命令恢复选中的文件.设置下个卷名为1, 不为解压目录设置所有者模式. quit退出restore模式.
10. 在restore运行的目录中应该有一个grub目录,包含恢复的grub.conf和menu.1st文件.

步骤四: 设置打印机,使用CUPS管理打印机.
1. 使用root帐户运行redhat-config-printer
2. 选择新建,回车
3. 在队列名称位置输入lp0
4. 选择队列类型为本地打印设备
5. 选择下一步,回车
6. 选择/dev/lp0,选择下一步
7. 选择postscript printer, 选择下一步
8. 当出现创建新队列:名称和类型画面时,选择结束,回车
9. 选择退出,回车.将询问是否保存.选择是.
10. 键入命令: cd 并键入 lpr < install.log
11. 键入命令: lpq (将会看到一个由root激活的打印任务,任务号为1)
12. 键入命令: lprm 1 删除任务
13. 键入命令: lpq (将会看到任务已经删除)

Lab 7 RPM和Kickstart
步骤一: kickstart安装
安装前阅读排故的建议
1. 编辑 /root/anaconda-ks.cfg文件,在开头插入以下行
nfs --server server1.example.com --dir /var/ftp/pub
使用以下分区方案:
clearpart --all
part / --fstype ext3 –size=256
part /boot –fstype ext3 --size=100
part /tmp –fstype ext3 --size=128
part /usr –fstype ext3 --size=2800
part /var –fstype ext3 --size=400
part /home –fstype ext3 --size=128
part swap –size=512
在%post部分增加以下内容作为一行
perl –pi –e ‘s, Welcome to %n, My kickstart system %n,’ /etc/X11/gdm/gdm.conf
2. 保存anaconda-ks.cfg文件为ks.cfg.复制到软盘
3. 用光盘或其他启动介质重启动系统, 把kickstart软盘放在软驱中
4. 当出现boot提示符时 输入linux ks=floppy 如果软盘有错系统会提示修正.
使用这个系统进行后面的实验.

排故建议:
如果安装过程中提示配置语言或者键盘,是因为ks.cfg中缺少对应的行.
如果出现Disk Druid, 说明分区配置不对.确定有足够的硬盘空间,并且分区配置包括swap分区.
Phython编译器在出错时会大量溢出信息.使用<Shift><Page Up>和<Shift><Page Down>仔细检查,即使对Phython不熟悉也会找到出错原因.
在%post部分之外的错误往往会在覆盖现有系统之前出现.可以重启动系统并修改ks.cfg文件.启动时用linux 1进入单用户模式可以加快启动速度.

步骤二 安装
Lab10 系统恢复和排故
目标:掌握排故过程中的技巧
1. 使用RPM查询以下请求:
initscripts包中有那些文件?
Bash包由哪一台主机创建?
pam包在安装后是否更改过?
哪个包名称中包含gnome?
哪个包生成了/etc/inittab文件?
哪个包生成了/etc/fstab文件?为什么?
你的内核的版本更新记录的最后一条记录是什么?
以下命令有什么差异?
rpm –ivh <package file>
rpm –Uvh <package file>
rpm –F <package file>
2. 练习检查光盘或server1上的RPM包的签名和完整性
使用 rpm –import 把Red Hat 的GPG导入系统
gpg --import /usr/share/rhn/RPM-GPG-KEY
rpm --import /usr/share/rhn/RPM-GPG-KEY
下面的命令将把包中的私钥和安装的公钥比较,确定包在创建后是否改变过
rpm -K <RPM package file>
3. 验证.
ftp://server1.example.com/pub 安装可用的更新.注意,如果要复制到本地安装则要有足够的空间,在对内核进行更新时要使用安装而非更新命令.
删除Red Hat的公钥(使用 man gpg查看方法), 用Red Hat,Inc(security@redhat.com)创建假公钥, 然后使用rpm -K查看当密钥不一致时的情况.

步骤三: 自动解析依存性
开始前, 确认以下包没有安装:
rpmdb-redhat
xsane
sane-backends
可用的文件包在加载NFS共享server1.example.com:/var/ftp/pub后找到,位于RedHat/RPMS
1. 观察没有自动解析的情况: 从共享位置安装xsane包.会出现类似提示:
error: Failed denpendencies:
libsane.so.1 is needed by xsane-0.89-3
不要试图完成安装.
2. 使用rpmdb-redhat. 安装rpmdb-redhat包,再次试图安装xsane包.这次仍然会失败,但是会给出有用的信息:
Suggested resolutions:
sane-backends-1.0.9-5.i386.rpm
3. aid. 在RPMS目录下使用rpm –ivh --aid xsane-0.*.rpm
sane-backends将被自动加载以满足依存性
注意,因为安装包和依存的包在同一目录,所以不需要指明rpm安装的方法

步骤四: GRUB
1. 重启动进入GRUB界面.如果在grub.conf文件中设置了timeout选项,可以看到画面下方的倒数.
2. 在倒数结束之前,按方向键停止计数
3. 注意显示下方的提示.使用上下键选择启动的内核,按e选择编辑grub.conf的内容.
4. 根据下方的提示,使用上下键选择有kernel字样的行并按e编辑
5. 现在进入了GRUB编辑模式.输入空格,s 然后回车.可以看到返回了前一画面,kernel行多出了文本s.如果不想保存更改,可以按ESC返回前一画面
6. 按b使用更改选项启动.在上例中将进入单用户模式(single user)
7. 重启后检查grub.conf文件.你将发现所作的更改没有保存在文件中
8. 重复以上步骤, 试验其他runlevel

Lab 8 逻辑卷和阵列
目标: 在安装后创建逻辑卷和阵列的技巧
步骤一: 使用LVM创建逻辑卷
1. 使用fdisk在未分区空间创建四个新分区,类型为Linux LVM (0x8e), 尺寸一样,为了加快速度,不要大于1G. 退出时使用w保存更改.不要重启动.
2. 编辑/etc/modules.conf中包含以下行(RHEL 可以不用做以下修改):
alias block-major-58 lvm-mod
alias char-major-109 lvm-mod
使用当前内核创建initrd
mkinintrd –f –v /boot/initrd-$(uname –r).img $(uname –r)
这个命令将使系统在启动时加载lvm-mod模块,启用LVM
3. 重启动系统
4. 用root登录, 运行vgscan初始化LVM配置文件
5. 使用pvcreate将LVM分区初始化为物理卷.假设分区为
/dev/hda9
/dev/hda10
/dev/hda11
/dev/hda12
命令为: pvcreate /dev/hda9 /dev/hda10 /dev/hda11 /dev/hda12
可以使用pddisplay查看分区信息
6. 然后创建卷组test0. 使用默认4MB的扩展尺寸,只包含一个物理卷
vgcreate test0 /dev/hda9
可以使用pddisplay查看信息

7. 创建一个小逻辑卷,不要占用所有空间. 使用vgdisplay的VG size和 PE/size信息,比如创建一个40M的逻辑卷:
lvcreate –L 40M –n data test0
可以使用 lvdisplay /dev/test0/data 确认命令执行了.
8. 在逻辑卷上创建ext3文件系统: mke2fs –j /dev/test0/data
9. 创建/data目录. mount /dev/test0/data /data
10. 复制文件到/data. 可以创建一个大文件: dd if=/dev/zero of=/data/bigfile bs=1024 count=20000
使用df检查/data的磁盘使用情况和剩余空间. 确认能够正常使用.可以编辑/etc/fstab来自动加载/data.重启动测试

步骤二: 使用逻辑卷
1. 首先, 卸载/data. 使用e2fsadm扩展分区尺寸: e2fsadm –L+50M /dev/test0/data
2. 重加载/dev/test0/data到/data, 确认文件. 运行df检查/data的磁盘使用情况和剩余空间.
3. 使用剩余扩展创建第二个逻辑分区. 运行vgdisplay查看PE /size,格式类似于166/644MB,这表示卷组包含166个扩展,664MB剩余空间. 创建一个占用166个扩展逻辑卷/dev/test0/scratch, 命令为:
lvcreate –l 166 –n scratch test0
4. 格式化新卷: mke2fs –j /dev/test0/scratch
5. 把未使用的物理卷加入卷组 vgextend test0 /dev/hda10
6. 如果再次运行vgdisplay, 可以看到增加的扩展.用20MB的扩展定义新逻辑卷.
e2fsadm –L+20M /dev/test0/scratch
使用lvdisplay和vgdisplay确认成功
7. 接下来用/data的只读快照创建新的逻辑卷. 首先用只读选项加载/data
mount –o remount,ro /data
8. 快照不需要和父卷尺寸一致,我们假设不需要保存太多数据,可以设置为5M
lvcreate –s –L 5M –n snap /dev/test0/data
9. 现在重加载/data为读写状态
mount –o remount,rw /data
10. 创建新加载点/snap, 使用 mount /dev/test0/snap /snap 比较/data和/snap,两者内容应该一致
11. 运行命令 for I in$(seq 1 10); do echo $1 > /data/$1; done 将在/data下创建十个文件,名称从1到10. 这个命令不影响/snap, 可以用lvdisplay /dev/test0/snap检查
12. 当快照逻辑卷不能容纳改变的块时,将被LVM自动删除,即使当前在加载状态.(避免这一情况的方法是尺寸和父卷一致,或者及时用lvextend扩展尺寸)可以通过以下方式看到这一现象:
rm /data/bigfile
for i in $(seq 1 10000); do echo $1 > /data/$1; done
13. 在/var/log/messages里可以看到类似信息:
Mar 19 16:30:02 station12 kernel: lvm --giving up to snapshot
/dev/test0/data on /dev/test0/snap: out of space
运行ls /snap. 快照已经不可用了,目录是空的.运行 lvdisplay /dev/test0/snap,和11步的结果比较.
14. 做完快照之后,如果数据已经备份,或者快照已被删除,都需要被卸载,否则会造成轻微的性能下降, 使用 umount /snap; lvremove /dev/test0/snap

在进行阵列试验以前清除LVM卷:
删除所有/etc/fstab中增加的记录
umount /dev/test0/data; umount /dev/test0/scratch
lvremove /dev/test0/data; lvremove /dev/test0/scratch
vgchange –an test0; vgremove test0

步骤三: 软件阵列
1. 在实验中我们将在同一磁盘创建多个分区来实现阵列,但是在实际工作中我们一般使用在不同磁盘上的分区来创建.使用fdisk将Linux LVM(0x8e)分区转换为Linux raid auto(0xfd)分区.保存更改.
2. 重启动系统.
3. 创建/etc/raidtab文件定义四个RAID-5阵列设备.根据以下示例,用实际的分区替换.chunk-size是一个重要的参数,决定了一次向阵列中每个磁盘写入数据的量.RAID-5需要一个校验算法行,一般设为left-symmetric来提高磁盘性能
raiddev /dev/md0
raid-level 5
nr-raid-disks 4
chunk-size 32
persistent-superblock 1
parity-algorithm left-symmetric
device /dev/hda9
raid-disk 0
device /dev/hda10
raid-disk 1
device /dev/hda11
raid-disk 2
device /dev/hda12
raid-disk 3

linux

4. 初始化阵列: mkraid /dev/md0. 如果阵列没有启动,手动启动 raidstart /dev/md0. 此时阵列会立即开始建立,但是已经可用了.可以在另一个虚拟控制台用 watch cat /pro/mdstat 监控建立过程.
5. 使用4k的块的ext3文件系统格式化. Stride选项应设为chunk size和阵列磁盘数的乘积, 可以加快格式化的速度
mke2fs –j –b 4096 –R stride=32 /dev/md0
6. 查看是否能 mount /dev/md0 /data. 即使仍在建立过程也可以加载. 使用df命令查看文件系统尺寸. 如果是四个同尺寸的分区组成的阵列,文件系统尺寸应该为三个分区之和.(其他空间用于储存校验信息)
7. 使用lsraid显示阵列设备的相关信息. Lsraid –A –a /dev/md0
8. 试着在/data创建文件. 可以在/etc/fstab中加入记录用来自动加载.
9. 检查/proc/mdstat, 确认阵列已经建立. 可以看到类似输出:
md0: active raid5 hda12[3] hda11[2] hda10[1] hda9[0]
2328064 blocks level 5, 32k chunk, algorithm 2[4/4] [UUUU]
10. 测试卷的破坏.用以下命令模拟: raidsetfaulty /dev/md0 /dev/hda11
在/var/log/messages中寻找出错信息, 注意/proc/mdstat文件的改变
md0: active raid5 hda12[3] hda11[2] hda10[1] hda9[0]
2328064 blocks level 5, 32k chunk, algorithm 2[4/3] [UUUU]
重启动系统, 查看启动时dmesg和/var/log/messages的出错信息
11. 模拟在重启前更换了损坏的磁盘. 使用命令替换阵列分区:
raidhotadd /dev/md0 /dev/hda11
12. 这时将看到/proc/mdstat显示阵列的重建

进阶实验:在软件阵列上创建LVM
以下实验可选,需要创建一个在两个磁盘上的物理卷建立的RAID1镜像卷基础上的RAID10. 使用同一磁盘的两个分区模拟这一情况.
1. 撤销前面的软件阵列设置: umount /dev/md0, 删除/et/fstab中的对应行.运行 raidstop /dev/md0. 从/etc/raidtab中删除/dev/md0设备.
2. 编辑/etc/raidtab创建两个RAID1镜像/dev/md0和/dev/md1, 分别由两个分区组成.示例如下:
raiddev /dev/md0
raid-level 1
nr-raid-disks 2
chunk-size 32
persistent-superblock 1
device /dev/hda9
raid-disk 0
device /dev/hda10
raid-disk 1
3. 注意: 运行命令时可能需要-f和-R重运行.因为系统会检测到上次创建的相关superblock.
4. 格式化并启动阵列设备: mkraid /dev/md0; mkraid /dev/md1
5. 设置阵列设备为物理卷: pvcreate /dev/md0 /dev/md1
6. 创建卷组: vgcreate test0 /dev/md0 /dev/md1
7. 使用vgdisplay查看有多少扩展可用
8. 设置条带的逻辑卷. 使用-i 指定构成条带逻辑卷的卷组中物理卷的数目. –I 设置条带的尺寸. 当使用-i时作用等同于RAID0阵列的chunk-size.使用 –l指定vgdisplay报告的卷组中逻辑卷可用的扩展.假设有500可用.示例为:
lvcreate –i 2 –I 64 –l 500 –n data test0
9. 使用ext3格式化/dev/test0/data ,作为条带的RAID阵列设置stride选项,
mke2fs –j –b 4096 –R stride=8 /dev/test0/data
10. 在/data加载/dev/md0/data. 把文件复制到/data,使用e2fsadm重设置尺寸.使用raidsetfaulty模拟磁盘损坏.(条带的逻辑卷在创建后可以重定义尺寸,只要不在另一个物理卷上使用这些扩展.)这样将得到RAID的冗余,条带的性能和LVM的灵活性.

Lab 9 X window系统
步骤一: 了解X的启动顺序
1. 创建并编辑/etc/X11/xinit/xinitrc.d/xeyes,加入以下行并设为可执行
#!/bin/sh
xeyes &
2. 切换到runlevel5
3. 使用显示管理器登录系统 gdm,kdm,xdm等.发生了什么?切换到虚拟控制台,运行
startx --:1
发生了什么? 为什么需要指定 -- :1?
4. 在创建的用户主目录下创建并编辑.xsession文件,增加以下行并设为可执行:
#!/bin/sh
xterm &
exec metacity
5. 使用这个帐户登录,发生什么? 使用这个帐户在虚拟控制台登录,并运行startx,发生了什么?

其他问题:
1. 列出升级视频卡的过程,包括选择卡的过程.
2. 描述当视频卡配置错误时,如何修复一个启动到runlevel5的系统.


Lab 10 系统修复和排故
目标: 熟悉系统修复的技巧

步骤一: 在rescue模式修复MBR
rescue模式提供了修复一个不能正常启动的系统的最后手段. 即使启动加载器或者根文件系统配置错误或损坏.进入该模式需要RedHat Linux的第一张光盘或者是网络路径的boot.iso镜像
任务.破坏GRUB使之不能启动.使用rescue模式重安装GRUB.
1. 使用以下命令,将MBR中的GRUB的第一部分用0覆盖.小心设置块尺寸.如果写入太多0,会覆盖分区表,造成的问题会大的多.(以下命令假设使用IDE设备)
dd if=/dev/zero of=/dev/had bs=446 count=1; reboot
恭喜---你的启动扇区已经破坏.不过你的主分区表还可用.重启确定系统不能启动.使用以下步骤修复系统.
2. 从光盘/软盘启动进入rescue模式. 当启动时输入 linux rescue
3. 修复环境将询问是否加载硬盘文件系统.选择继续.用读写模式加载.检查mount的输出保证文件系统加载正确.可以使用fdisk检查分区
mount
fdisk –l /dev/hda
4. 注意硬盘加载在/mnt/sysimage. 检查grub.conf文件确认配置正确.
cat /mnt/sysimage/boot/grub/grub.conf
5. 安装GRUB需要切换上下文,使/mnt/sysimage成为grub-install认为的系统的根.加载chroot shell, 运行grub-install, 退出.
Chroot /mnt/sysimage
grub-install /dev/had
exit
6. 输入exit退出rescue模式. 注意这会卸载加载的分区.

步骤二: 在rescue模式安装软件.
使用以下命令覆盖mount命令:
cp /bin/date /bin/mount
恭喜---你已经破坏了一个重要的执行文件. 重启动后你会发现系统不能启动.使用rescue模式,安装合适的rpm包.
1. 使用启动介质启动系统到rescue模式.
2. 系统会提示加载硬盘文件系统.使用mount检查是否正确加载.
3. 注意硬盘的文件系统加载在/mnt/sysimage. 查看哪个rpm包包含这个命令
rpm –qf --root /mnt/sysimage/bin/mount
4. 确认mount的rpm包,使用chroot安装rpm
chroot /mnt/sysimage
rpm –V mount
exit
5. rpm会报告/bin/mount被修改了.从网络重新安装mount包,要使用chroot
rpm –ivh --force --root /mnt/sysimage /mnt/source/RedHat/RPMS/mount*
6. 输入exit退出rescue模式. 注意这会卸载加载的分区.

 

发表于: 2006-09-05,修改于: 2006-09-05 09:50,已浏览780次,有评论0条 推荐 投诉
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 使用date命令显示当前日期。 2. 使用 who命令显示当前所有登录用户的信息,试依次打开多个终端,再查看输出结果的变化。 3. 查看当前linux服务器的主机名的命令。(hostname) 4. 使用clear命令,清除屏幕信息。 5. 添加一个组账号,名称为group1,打开图形界面观察执行后的结果。 6. 查询文件group中group1的记录,改变group1组的GID 为666, 再查看文件group中group1的记录。 7. 以自己的姓名全拼创建一个用户,指定UID为555, 加上用户的备注信息(姓名全称、所在院系等),初始组为group1,有效组为root,打开图形界面观察执行后的结果。 8. 为以自己姓名命名的账户设置密码,并查看passwd文件和shadow文件的最后一行。 9. 使用命令给自己的姓名账户密码冻结,用passwd查看账户相关信息,最后给账户解冻。 10. 进入以自己姓名命名的用户目录,显示当前的路径,退出当前目录,返回根目录。 11. 复制文件/etc/passwd到用户目录,文件名不变,打开图形界面Nautilus的窗口观察执行后的结果。 12. 统计文件/etc/passwd文件的字节数和行数。 13. 创建普通文件/home/abc1,创建普通文件/home/abc2。 14. 将/etc/passwd的内容追加到文件abc1中,将/etc/group的内容追加到文件abc2中。(利用附加输出重定向命令)。 15. 修改文件abc1的属性为:属主具有一切权限,同组用户和其他用户具有可读可写的权限。(使用数字模式) 16. 修改abc2的属性为:给其他用户增加可执行的权限。(使用功能模式) 17. 以长文件形式显示文件abc1的详细信息。 18. 进入/home目录,将文件abc1和abc2打包成文件ab.tar,放在以自己姓名命名的用户目录下(比如/home/liqun),打开图形界面Nautilus的窗口观察执行后的结果。 19. 创建Readme.txt文本文件,内容为你的姓名全拼、学号等信息,保存关闭后用命令查看Readme.txt文件的内容,并在每一行前加行号。 20. 在root目录创建新文件夹mydir1,其绝对路径为“/root/testdir/mydir1”(假设testdir目录事先并不存在)。 21. 在root目录创建新文件夹dir1,将其移动到 /root/testdir/mydir1目录下。在root目录创建新文件夹dir2,将其移动到 /root/testdir目录下,并将其改名为mydir2。打开图形界面Nautilus的窗口,观察以上这些步骤执行后的结果。 22. 分别删除/testdir目录下的mydir1和mydir2目录。(注意rm命令不加-r选项,系统会提示mydir1是个目录不能删除。)(尝试重复步骤20和步骤21,重建上述目录,执行步骤22,比较增加-f选项的区别)。 23. 建立文件/etc/group的符号链接文件/root/a.b,打开图形界面Nautilus的窗口观察a.b文件的特征。 24. 进入根目录的tmp目录下,建立test1和test2两个文件夹,在test1中建立文件a.log, b.log,用ls命令查看所创建的文件a.log,b.log。再把a.log 硬连接到test2目录,文件同名。把b.log 链接到test2目录下,文件同名。用ls命令查看test2文件夹下所创建的文件,并打开图形界面Nautilus的窗口观察文件夹test2下a.log文件和b.log文件的特征。 25. 由超级用户切换为以自己姓名命名的普通用户身份,并使用普通用户的环境变量,再切换回超级用户身份。
集合了 所有的 Unix命令大全 登陆服务器时输入 公帐号 openlab-open123 telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ 一、注意事项 命令和参数之间必需用空格隔开,参数和参数之间也必需用空格隔开。 一行不能超过256个字符;大小写有区分。 二、特殊字符含义 文件名以“.”开头的都是隐藏文件/目录,只需在文件/目录名前加“.”就可隐藏它。 ~/ 表示主目录。 ./ 当前目录(一个点)。 ../ 上一级目录(两个点)。 ; 多个命令一起用。 > >> 输出重定向 。将一个命令的输出内容写入到一个文件里面。如果该文件存在, 就将该文件的内容覆盖; 如果不存在就先创建该文件, 然后再写入内容。 输出重定向,意思就是说,将原来屏幕输出变为文件输出,即将内容输到文件中。 < << 输入重定向。 本来命令是通过键盘得到输入的,但是用小于号,就能够使命令从文件中得到输入。 \ 表示未写完,回车换行再继续。 * 匹配零个或者多个字符。 ? 匹配一个字符。 [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后接命令符 -a 所有(all)。 -e 所有(every),比a更详细。 -f 取消保护。 -i 添加提示。 -p 强制执行。 -r 目录管理。 分屏显示的中途操作 空格<space> 继续打开下一屏; 回车<return> 继续打开下一行; b 另外开上一屏; f 另外开下一屏; h 帮助; q或Ctrl+C 退出; /字符串 从上往下查找匹配的字符串; ?字符串 从下往上查找匹配的字符串; n 继续查找。 四、退出命令 exit 退出; DOS内部命令 用于退出当前的命令处理器(COMMAND.COM) 恢复前一个命令处理器。 Ctrl+d 跟exit一样效果,表中止本次操作。 logout 当csh时可用来退出,其他shell不可用。 clear 清屏,清除(之前的内容并未删除,只是没看到,拉回上面可以看回)。 五、目录管理命令 pwd 显示当前所在目录,打印当前目录的绝对路径。 cd 进入某目录,DOS内部命令 显示或改变当前目录。 cd回车/cd ~ 都是回到自己的主目录。 cd . 当前目录(空格再加一个点)。 cd .. 回到上一级目录(空格再加两个点)。 cd ../.. 向上两级。 cd /user/s0807 从绝对路径去到某目录。 cd ~/s0807 直接进入主目录下的某目录(“cd ~"相当于主目录的路径的简写)。 ls 显示当前目录的所有目录和文件。 用法 ls [-aAbcCdeEfFghHilLmnopqrRstux1@] [file...] ls /etc/ 显示某目录下的所有文件和目录,如etc目录下的。 ls -l (list)列表显示文件(默认按文件名排序), 显示文件的权限、硬链接数(即包含文件数,普通文件是1,目录1+)、用户、组名、大小、修改日期、文件名。 ls -t (time)按修改时间排序,显示目录和文件。 ls -lt 是“-l”和“-t”的组合,按时间顺序显示列表。 ls -F 显示文件类型,目录“/ ”结尾;可执行文件“*”结尾;文本文件(none),没有结尾。 ls -R 递归显示目录结构。即该目录下的文件和各个副目录下的文件都一一显示。 ls -a 显示所有文件,包括隐藏文件。 文件权限 r 读权限。对普通文件来说,是读取该文件的权限;对目录来说,是获得该目录下的文件信息。 w 写权限。对文件,是修改;对目录,是增删文件与子目录。 (注 删除没有写权限的文件可以用 rm -f ,这是为了操作方便,是人性化的设计)。 x 执行权限;对目录,是进入该目录 - 表示没有权限 形式 - rw- r-- r-- 其中 第一个是文件类型(-表普通文件,d表目录,l表链接文件) 第2~4个是属主,生成文件时登录的人,权限最高,用u表示 第5~7个是属组,系统管理员分配的同组的一个或几个人,用g表示 第8~10个是其他人,除属组外的人,用o表示 所有人,包括属主、属组及其他人,用a表示 chmod 更改权限; 用法 chmod [-fR] <绝对模式> 文件 ... chmod [-fR] <符号模式列表> 文件 ... 其中 <符号模式列表> 是一个用逗号分隔的表 [ugoa]{+|-|=}[rwxXlstugo] chmod u+rw 给用户加权限。同理,u-rw也可以减权限。 chmod u=rw 给用户赋权限。与加权限不一样,赋权限有覆盖的效果。 主要形式有如下几种 chmod u+rw chmod u=rw chmod u+r, u+w chmod u+rw,g+w, o+r chmod 777( 用数字的方式设置权限是最常用的) 数字表示权限时,各数位分别表示属主、属组及其他人; 其中,1是执行权(Execute),2是写权限(Write),4是读权限(Read), 具体权限相当于三种权限的数相加,如7=1+2+4,即拥有读写和执行权。 另外,临时文件/目录的权限为rwt,可写却不可删,关机后自动删除;建临时目录:chmod 777 目录名,再chmod +t 目录名。 id 显示用户有效的uid(用户字)和gid(组名) 用法 id [-ap] [user] id 显示自己的。 id root 显示root的。 id -a root 显示用户所在组的所有组名(如root用户,是所有组的组员) df 查看文件系统,查看数据区 用法 df [-F FSType] [-abeghklntVvZ] [-o FSType 特定选项] [目录 | 块设备 | 资源] df -k 以kbytes显示文件大小的查看文件系统方式 六、显示文件内容 more 分屏显示文件的内容。 用法 more [-cdflrsuw] [-行] [+行号] [+/模式] [文件名 ...]。 显示7个信息:用户名 密码 用户id(uid) 组id(gid) 描述信息(一般为空) 用户目录 login shell(登录shell) cat 显示文件内容,不分屏(一般用在小文件,大文件显示不下);合并文件,仅在屏幕上合并,并不改变原文件。 用法 cat [ -usvtebn ] [-|文件] ... tail 实时监控文件,一般用在日志文件,可以只看其中的几行。 用法 tail [+/-[n][lbc][f]] [文件] tail [+/-[n][l][r|f]] [文件] 七、文件/目录的增删 echo 显示一行内容。 touch 如果文件/目录不存在,则创建新文件/目录;如果文件存在,那么就是更新该文件的最后访问时间, 用法 touch [-acm] [-r ref_file] 文件... touch [-acm] [MMDDhhmm[yy]] 文件... touch [-acm] [-t [[CC]YY]MMDDhhmm[.SS]] file... mkdir 创建目录(必须有创建目录的权限) 用法 mkdir [-m 模式] [-p] dirname ... mkdir dir1/dir2 在dir1下建dir2 mkdir dir13 dir4 dir5 连建多个 mkdir ~/games 用户目录下建(默认在当前目录创建) mkdir -p dir6/dir7/dir8 强制创建dir8;若没有前面的目录,会自动创建dir6和dir7。 不用-p时,若没有dir6/dir7,则创建失败。 cp 复制文件/目录 cp 源文件 目标文件 复制文件;若已有文件则覆盖 cp -r 源目录 目标目录 复制目录;若已有目录则把源目录复制到目标目录下, 没有目标目录时,相当于完全复制源目录,只是文件名不同。 cp beans apple dir2 把beans、apple文件复制到dir2目录下 cp -i beans apple 增加是否覆盖的提示 mv 移动或重命名文件/目录 用法 mv [-f] [-i] f1 f2 mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 mv 源文件名 目标文件名 若目标文件名还没有,则是源文件重命名为目标文件;若目标文件已存在,则源文件覆盖目标文件。 mv 源文件名 目标目录 移动文件 mv 源目录 目标目录 若目标目录不存在,则源目录重命名;若目标目录已存在,则源目录移动到目标目录下。 rm 删除文件/目录 用法 rm [-fiRr] 文件 ... rm 文件名 删除文件。 rm -r 目录名 删除目录。 rm –f 文件 只要是该文件或者目录的拥有者,无论是否有权限删除,都可以用这个命令参数强行删除。 rm -rf * 删除所有文件及目录 rmdir 删除空目录。只可以删除空目录。 ln 创建硬链接或链接,硬链接=同一文件的多个名字;链接=快捷方式 用法 ln [-f] [-n] [-s] f1 [f2] ln [-f] [-n] [-s] f1 ... fn d1 ln [-f] [-n] -s d1 d2 ln file1 file1.ln 创建硬链接。感觉是同一文件,删除一个,对另一个没有影响;须两个都删除才算删除。 ln -s file1 file1.sln 创建链接。可跨系统操作,冲破操作权限;也是快捷方式。 八、时间显示 date 显示时间,精确到秒 用法 date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] cal 显示日历 cal 9 2008 显示2008年9月的日历; cal 显示当月的 用法 cal [ [月] 年 ] 九、帮助 man 帮助( format and display the on-line manual pages) 用法 man [-] [-adFlrt] [-M 路径] [-T 宏件包] [-s 段] 名称 ... man [-] [-adFlrt] [-M path] [-T macro-package] [-s section] name... man [-M 路径] -k 关键字 ... man [-M 路径] -f 文件 ... awk 按一定格式输出(pattern scanning and processing language) 用法 awk [-Fc] [-f 源代码 | 'cmds'] [文件] 十、vi 底行模式 /? 命令模式 i a o 输入模式 vi 的使用方法 1、光标 h 左 j 下 k 上 l 右 set nu 显示行号(set nonu) 21 光标停在指定行 21G 第N行 (G到文件尾,1G到文件头) 如果要将光标移动到文件第一行,那么就按 1G H 屏幕头 M 屏幕中间 L 屏幕底 ^ 或 shift+6 行首 $ 或 shift+4 行尾 Ctrl+f 下翻 Ctrl+b 上翻 2、输入 (输入模式) o 光标往下换一行 O (大写字母o)在光标所在行上插入一空行 i 在光标所在位置的前面插入字母 a 在光标所在位置的后面插入一个新字母 <Esc> 退出插入状态。 3、修改替换 r 替换一个字符 dd 删除行,剪切行 (5dd删除5行) 5,10d 删除 5 至 10 行(包括第 5行和第 10 行) x 删除一个字符 dw 删除词,剪切词。 ( 3dw删除 3 单词) cw 替换一个单词。 (cw 和 dw 的区别 cw 删除某一个单词后直接进入编辑模式,而dw删除词后仍处于命令模式) cc 替换一行 C 替换从光标到行尾 yy 复制行 (用法同下的 Y ,见下行) Y 将光标移动到要复制行位置,按yy。当你想粘贴的时候,请将光标移动到你想复制的位置的前一个位置,然后按 p yw 复制词 p 当前行下粘贴 1,2co3 复制行1,2在行3之后 4,5m6 移动行4,5在行6之后 u 当你的前一个命令操作是一个误操作的时候,那么可以按一下 u键,即可复原。只能撤销一次 r file2 在光标所在处插入另一个文件 ~ 将字母变成大写 J 可以将当前行与下一行连接起来 /字符串 从上往下找匹配的字符串 ?字符串 从下往上找匹配的字符串 n 继续查找 1,$s/旧串/新串/g 替换全文(或者 %s/旧串/新串/g) (1表示从第一行开始) 没有g则只替换一次,加g替换所有 3、存盘和退出 w 存盘 w newfile 存成新文件 wq 存盘再退出VI(或者ZZ或 X) q! 强行退出不存盘 查看用户 users 显示在线用户(仅显示用户名)。 who 显示在线用户,但比users更详细,包括用户名、终端号、登录时间、IP地址。 who am i 仅显示自己,(但包括用户名、端口、登录时间、IP地址;信息量=who)。 whoami 也仅显示自己,但只有用户名(仅显示自己的有效的用户名)。 w 显示比who更多内容,还包括闲置时间、占CPU、平均占用CPU、执行命令。 用法 w [ -hlsuw ] [ 用户 ] su 改变用户,需再输入密码。 用法 su [-] [ username [ arg ... ] ] su - 相当于退出再重新登录。 查找 find 查找文件 用法 find [-H | -L] 路径列表 谓词列表 find / -name perl 从根目录开始查找名为perl的文件。 find . -mtime 10 -print 从当前目录查找距离现在10天时修改的文件,显示在屏幕上。 (注 “10”表示第10天的时候;如果是“+10”表示10天以外的范围;“-10”表示10天以内的范围。) grep 文件中查找字符;有过滤功能,只列出想要的内容 用法 grep -hblcnsviw 模式 文件 . . . 如 grep abc /etc/passwd 在passwd文件下找abc字符 wc 统计 -l 统计行数; -w统计单词数; -c 统计字符数 如 grep wang /etc/passwd|wc -l 统计passwd文件含“wang”的行数 du 查看目录情况 如 du -sk * 不加-s会显示子目录,-k按千字节排序 用法 du [-a] [-d] [-h|-k] [-r] [-o|-s] [-H|-L] [文件...] 进程管理 ps 显示进程。 用法 ps [ -aAdeflcjLPyZ ] [ -o 格式 ] [ -t 项列表 ] [ -u 用户列表 ] [ -U 用户列表 ] [ -G 组列表 ] [ -p 进程列表 ] [ -g 程序组列表 ] [ -s 标识符列表 ] [ -z 区域列表 ] ps 显示自己的进程。 ps -e 显示每个进程,包括空闲进程。 ps -f 显示详情。 ps -ef 组合-e和-f,所有进程的详情。 ps -U uidlist(用户列表) 具体查看某人的进程。 kill pkill sleep jobs 用法 jobs [-l ] fg %n bg %n stop %n 挂起(仅csh能用) Ctrl+C Ctrl+Z 网络链接 ping usage ping host [timeout] usage ping -s [-l | U] [adLnRrv] [-A addr_family] [-c traffic_class] [-g gateway [-g gateway ...]] [-F flow_label] [-I interval] [-i interface] [-P tos] [-p port] [-t ttl] host [data_size] [npackets] ifconfig -a /sbin/ifconfig 查看本机的IP地址 netstat -rn rlogin ftp 帮助文件 [sd0807@localhost ~]$ help GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu) These shell commands are defined internally. Type `help' to see this list. Type `help name' to find out more about the function `name'. Use `info bash' to find out more about the shell in general. Use `man -k' or `info' to find out more about commands not in this list. A star (*) next to a name means that the command is disabled. JOB_SPEC [&] (( expression )) . filename [arguments] [ arg... ] [[ expression ]] alias [-p] [name[=value] ... ] bg [job_spec ...] bind [-lpvsPVS] [-m keymap] [-f fi break [n] builtin [shell-builtin [arg ...]] caller [EXPR] case WORD in [PATTERN [| PATTERN]. cd [-L|-P] [dir] command [-pVv] command [arg ...] compgen [-abcdefgjksuv] [-o option complete [-abcdefgjksuv] [-pr] [-o continue [n] declare [-afFirtx] [-p] [name[=val dirs [-clpv] [+N] [-N] disown [-h] [-ar] [jobspec ...] echo [-neE] [arg ...] enable [-pnds] [-a] [-f filename] eval [arg ...] exec [-cl] [-a name] file [redirec exit [n] export [-nf] [name[=value] ...] or false fc [-e ename] [-nlr] [first] [last fg [job_spec] for NAME [in WORDS ... ;] do COMMA for (( exp1; exp2; exp3 )); do COM function NAME { COMMANDS ; } or NA getopts optstring name [arg] hash [-lr] [-p pathname] [-dt] [na help [-s] [pattern ...] history [-c] [-d offset] [n] or hi if COMMANDS; then COMMANDS; [ elif jobs [-lnprs] [jobspec ...] or job kill [-s sigspec | -n signum | -si let arg [arg ...] local name[=value] ... logout popd [+N | -N] [-n] printf [-v var] format [arguments] pushd [dir | +N | -N] [-n] pwd [-LP] read [-ers] [-u fd] [-t timeout] [ readonly [-af] [name[=value] ...] return [n] select NAME [in WORDS ... ;] do CO set [--abefhkmnptuvxBCHP] [-o option] [arg ...] shift [n] shopt [-pqsu] [-o long-option] opt source filename [arguments] suspend [-f] test [expr] time [-p] PIPELINE times trap [-lp] [arg signal_spec ...] true type [-afptP] name [name ...] typeset [-afFirtx] [-p] name[=valu ulimit [-SHacdfilmnpqstuvx] [limit umask [-p] [-S] [mode] unalias [-a] name [name ...] unset [-f] [-v] [name ...] until COMMANDS; do COMMANDS; done variables - Some variable names an wait [n] while COMMANDS; do COMMANDS; done { COMMANDS ; } 输入 man help BASH_BUILTINS(1) BASH_BUILTINS(1) NAME bash, :, ., [, alias, bg, bind, break, builtin, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit, umask, una- lias, unset, wait - bash built-in commands, see bash(1) BASH BUILTIN COMMANDS Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options. For example, the :, true, false, and test builtins do not accept options. : [arguments] No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned. . filename [arguments] source filename [arguments] Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing file- name. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read. alias [-p] [name[=value] ...] Alias with no arguments or with the -p option prints the list of aliases in the form alias name=value on standard output. When arguments are supplied, an alias is defined for each name whose value is given. A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded. For each name in the argument list for which no value is supplied, the name and value of the alias is printed. Alias returns true unless a name is given for which no alias has been defined. bg [jobspec ...] Resume each suspended job jobspec in the background, as if it had been started with &. If jobspec is not present, the shell’s notion of the current job is used. bg jobspec returns 0 unless run when job control is disabled or, when run with job con- trol enabled, any specified jobspec was not found or was started without job control. bind [-m keymap] [-lpsvPSV] bind [-m keymap] [-q function] [-u function] [-r keyseq] bind [-m keymap] -f filename bind [-m keymap] -x keyseq:shell-command bind [-m keymap] keyseq:function-name bind readline-command Display current readline key and function bindings, bind a key sequence to a readline function or macro, or set a readline variable. Each non-option argument is a command as it would appear in .inputrc, but each binding or command must be passed as a sepa- rate argument; e.g., ’"\C-x\C-r": re-read-init-file’. Options, if supplied, have the following meanings: -m keymap Use keymap as the keymap to be affected by the subsequent bindings. Accept- able keymap names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command, and vi-insert. vi is equivalent to vi-command; emacs is equivalent to emacs-standard. -l List the names of all readline functions. -p Display readline function names and bindings in such a way that they can be re-read. -P List current readline function names and bindings. -v Display readline variable names and values in such a way that they can be re- read. -V List current readline variable names and values. -s Display readline key sequences bound to macros and the strings they output in such a way that they can be re-read. -S Display readline key sequences bound to macros and the strings they output. -f filename Read key bindings from filename. -q function Query about which keys invoke the named function. -u function Unbind all keys bound to the named function. -r keyseq Remove any current binding for keyseq. -x keyseq:shell-command Cause shell-command to be executed whenever keyseq is entered. The return value is 0 unless an unrecognized option is given or an error occurred. break [n] Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n is greater than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless the shell is not executing a loop when break is executed. builtin shell-builtin [arguments] Execute the specified shell builtin, passing it arguments, and return its exit sta- tus. This is useful when defining a function whose name is the same as a shell builtin, retaining the functionality of the builtin within the function. The cd builtin is commonly redefined this way. The return status is false if shell-builtin is not a shell builtin command. cd [-L|-P] [dir] Change the current directory to dir. The variable HOME is the default dir. The variable CDPATH defines the search path for the directory containing dir. Alterna- tive directory names in CDPATH are separated by a colon (:). A null directory name in CDPATH is the same as the current directory, i.e., ‘‘.’’. If dir begins with a slash (/), then CDPATH is not used. The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command); the -L option forces symbolic links to be followed. An argument of - is equivalent to $OLDPWD. If a non-empty directory name from CDPATH is used, or if - is the first argument, and the directory change is successful, the absolute path- name of the new working directory is written to the standard output. The return value is true if the directory was successfully changed; false otherwise. caller [expr] Returns the context of any active subroutine call (a shell function or a script exe- cuted with the . or source builtins. Without expr, caller displays the line number and source filename of the current subroutine call. If a non-negative integer is supplied as expr, caller displays the line number, subroutine name, and source file corresponding to that position in the current execution call stack. This extra information may be used, for example, to print a stack trace. The current frame is frame 0. The return value is 0 unless the shell is not executing a subroutine call or expr does not correspond to a valid position in the call stack. command [-pVv] command [arg ...] Run command with args suppressing the normal shell function lookup. Only builtin com- mands or commands found in the PATH are executed. If the -p option is given, the search for command is performed using a default value for PATH that is guaranteed to find all of the standard utilities. If either the -V or -v option is supplied, a description of command is printed. The -v option causes a single word indicating the command or file name used to invoke command to be displayed; the -V option produces a more verbose description. If the -V or -v option is supplied, the exit status is 0 if command was found, and 1 if not. If neither option is supplied and an error occurred or command cannot be found, the exit status is 127. Otherwise, the exit status of the command builtin is the exit status of command. compgen [option] [word] Generate possible completion matches for word according to the options, which may be any option accepted by the complete builtin with the exception of -p and -r, and write the matches to the standard output. When using the -F or -C options, the vari- ous shell variables set by the programmable completion facilities, while available, will not have useful values. The matches will be generated in the same way as if the programmable completion code had generated them directly from a completion specification with the same flags. If word is specified, only those completions matching word will be displayed. The return value is true unless an invalid option is supplied, or no matches were generated. complete [-abcdefgjksuv] [-o comp-option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] name [name ...] complete -pr [name ...] Specify how arguments to each name should be completed. If the -p option is sup- plied, or if no options are supplied, existing completion specifications are printed in a way that allows them to be reused as input. The -r option removes a completion specification for each name, or, if no names are supplied, all completion specifica- tions. The process of applying these completion specifications when word completion is attempted is described above under Programmable Completion. Other options, if specified, have the following meanings. The arguments to the -G, -W, and -X options (and, if necessary, the -P and -S options) should be quoted to protect them from expansion before the complete builtin is invoked. -o comp-option The comp-option controls several aspects of the compspec’s behavior beyond the simple generation of completions. comp-option may be one of: bashdefault Perform the rest of the default bash completions if the compspec gen- erates no matches. default Use readline’s default filename completion if the compspec generates no matches. dirnames Perform directory name completion if the compspec generates no matches. filenames Tell readline that the compspec generates filenames, so it can per- form any filename-specific processing (like adding a slash to direc- tory names or suppressing trailing spaces). Intended to be used with shell functions. nospace Tell readline not to append a space (the default) to words completed at the end of the line. plusdirs After any matches defined by the compspec are generated, directory name completion is attempted and any matches are added to the results of the other actions. -A action The action may be one of the following to generate a list of possible comple- tions: alias Alias names. May also be specified as -a. arrayvar Array variable names. binding Readline key binding names. builtin Names of shell builtin commands. May also be specified as -b. command Command names. May also be specified as -c. directory Directory names. May also be specified as -d. disabled Names of disabled shell builtins. enabled Names of enabled shell builtins. export Names of exported shell variables. May also be specified as -e. file File names. May also be specified as -f. function Names of shell functions. group Group names. May also be specified as -g. helptopic Help topics as accepted by the help builtin. hostname Hostnames, as taken from the file specified by the HOSTFILE shell variable. job Job names, if job control is active. May also be specified as -j. keyword Shell reserved words. May also be specified as -k. running Names of running jobs, if job control is active. service Service names. May also be specified as -s. setopt Valid arguments for the -o option to the set builtin. shopt Shell option names as accepted by the shopt builtin. signal Signal names. stopped Names of stopped jobs, if job control is active. user User names. May also be specified as -u. variable Names of all shell variables. May also be specified as -v. -G globpat The filename expansion pattern globpat is expanded to generate the possible completions. -W wordlist The wordlist is split using the characters in the IFS special variable as delimiters, and each resultant word is expanded. The possible completions are the members of the resultant list which match the word being completed. -C command command is executed in a subshell environment, and its output is used as the possible completions. -F function The shell function function is executed in the current shell environment. When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable. -X filterpat filterpat is a pattern as used for filename expansion. It is applied to the list of possible completions generated by the preceding options and argu- ments, and each completion matching filterpat is removed from the list. A leading ! in filterpat negates the pattern; in this case, any completion not matching filterpat is removed. -P prefix prefix is added at the beginning of each possible completion after all other options have been applied. -S suffix suffix is appended to each possible completion after all other options have been applied. The return value is true unless an invalid option is supplied, an option other than -p or -r is supplied without a name argument, an attempt is made to remove a comple- tion specification for a name for which no specification exists, or an error occurs adding a completion specification. continue [n] Resume the next iteration of the enclosing for, while, until, or select loop. If n is specified, resume at the nth enclosing loop. n must be ≥ 1. If n is greater than the number of enclosing loops, the last enclosing loop (the ‘‘top-level’’ loop) is resumed. The return value is 0 unless the shell is not executing a loop when con- tinue is executed. declare [-afFirtx] [-p] [name[=value] ...] typeset [-afFirtx] [-p] [name[=value] ...] Declare variables and/or give them attributes. If no names are given then display the values of variables. The -p option will display the attributes and values of each name. When -p is used, additional options are ignored. The -F option inhibits the display of function definitions; only the function name and attributes are printed. If the extdebug shell option is enabled using shopt, the source file name and line number where the function is defined are displayed as well. The -F option implies -f. The following options can be used to restrict output to variables with the specified attribute or to give variables attributes: -a Each name is an array variable (see Arrays above). -f Use function names only. -i The variable is treated as an integer; arithmetic evaluation (see ARITHMETIC EVALUATION ) is performed when the variable is assigned a value. -r Make names readonly. These names cannot then be assigned values by subsequent assignment statements or unset. -t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling shell. The trace attribute has no special mean- ing for variables. -x Mark names for export to subsequent commands via the environment. Using ‘+’ instead of ‘-’ turns off the attribute instead, with the exception that +a may not be used to destroy an array variable. When used in a function, makes each name local, as with the local command. If a variable name is followed by =value, the value of the variable is set to value. The return value is 0 unless an invalid option is encountered, an attempt is made to define a function using ‘‘-f foo=bar’’, an attempt is made to assign a value to a readonly variable, an attempt is made to assign a value to an array variable without using the compound assignment syntax (see Arrays above), one of the names is not a valid shell variable name, an attempt is made to turn off readonly status for a readonly variable, an attempt is made to turn off array status for an array variable, or an attempt is made to display a non-exis- tent function with -f. dirs [-clpv] [+n] [-n] Without options, displays the list of currently remembered directories. The default display is on a single line with directory names separated by spaces. Directories are added to the list with the pushd command; the popd command removes entries from the list. +n Displays the nth entry counting from the left of the list shown by dirs when invoked without options, starting with zero. -n Displays the nth entry counting from the right of the list shown by dirs when invoked without options, starting with zero. -c Clears the directory stack by deleting all of the entries. -l Produces a longer listing; the default listing format uses a tilde to denote the home directory. -p Print the directory stack with one entry per line. -v Print the directory stack with one entry per line, prefixing each entry with its index in the stack. The return value is 0 unless an invalid option is supplied or n indexes beyond the end of the directory stack. disown [-ar] [-h] [jobspec ...] Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job. echo [-neE] [arg ...] Output the args, separated by spaces, followed by a newline. The return status is always 0. If -n is specified, the trailing newline is suppressed. If the -e option is given, interpretation of the following backslash-escaped characters is enabled. The -E option disables the interpretation of these escape characters, even on systems where they are interpreted by default. The xpg_echo shell option may be used to dynamically determine whether or not echo expands these escape characters by default. echo does not interpret -- to mean the end of options. echo interprets the following escape sequences: \a alert (bell) \b backspace \c suppress trailing newline \e an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \0nnn the eight-bit character whose value is the octal value nnn (zero to three octal digits) \nnn the eight-bit character whose value is the octal value nnn (one to three octal digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) enable [-adnps] [-f filename] [name ...] Enable and disable builtin shell commands. Disabling a builtin allows a disk command which has the same name as a shell builtin to be executed without specifying a full pathname, even though the shell normally searches for builtins before disk commands. If -n is used, each name is disabled; otherwise, names are enabled. For example, to use the test binary found via the PATH instead of the shell builtin version, run ‘‘enable -n test’’. The -f option means to load the new builtin command name from shared object filename, on systems that support dynamic loading. The -d option will delete a builtin previously loaded with -f. If no name arguments are given, or if the -p option is supplied, a list of shell builtins is printed. With no other option arguments, the list consists of all enabled shell builtins. If -n is supplied, only disabled builtins are printed. If -a is supplied, the list printed includes all builtins, with an indication of whether or not each is enabled. If -s is supplied, the output is restricted to the POSIX special builtins. The return value is 0 unless a name is not a shell builtin or there is an error loading a new builtin from a shared object. eval [arg ...] The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0. exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth arg passed to command. This is what login(1) does. The -c option causes command to be executed with an empty environ- ment. If -a is supplied, the shell passes name as the zeroth argument to the exe- cuted command. If command cannot be executed for some reason, a non-interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1. exit [n] Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell termi- nates. export [-fn] [name[=word]] ... export -p The supplied names are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the names refer to functions. If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed. The -n option causes the export property to be removed from each name. If a variable name is followed by =word, the value of the variable is set to word. export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is sup- plied with a name that is not a function. fc [-e ename] [-nlr] [first] [last] fc -s [pat=rep] [cmd] Fix Command. In the first form, a range of commands from first to last is selected from the history list. First and last may be specified as a string (to locate the last command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to the current command for listing (so that ‘‘fc -l -10’’ prints the last 10 commands) and to first otherwise. If first is not speci- fied it is set to the previous command for editing and -16 for listing. The -n option suppresses the command numbers when listing. The -r option reverses the order of the commands. If the -l option is given, the commands are listed on standard output. Otherwise, the editor given by ename is invoked on a file contain- ing those commands. If ename is not given, the value of the FCEDIT variable is used, and the value of EDITOR if FCEDIT is not set. If neither variable is set, is used. When editing is complete, the edited commands are echoed and executed. In the second form, command is re-executed after each instance of pat is replaced by rep. A useful alias to use with this is ‘‘r="fc -s"’’, so that typing ‘‘r cc’’ runs the last command beginning with ‘‘cc’’ and typing ‘‘r’’ re-executes the last command. If the first form is used, the return value is 0 unless an invalid option is encoun- tered or first or last specify history lines out of range. If the -e option is sup- plied, the return value is the value of the last command executed or failure if an error occurs with the temporary file of commands. If the second form is used, the return status is that of the command re-executed, unless cmd does not specify a valid history line, in which case fc returns failure. fg [jobspec] Resume jobspec in the foreground, and make it the current job. If jobspec is not present, the shell’s notion of the current job is used. The return value is that of the command placed into the foreground, or failure if run when job control is dis- abled or, when run with job control enabled, if jobspec does not specify a valid job or jobspec specifies a job that was started without job control. getopts optstring name [args] getopts is used by shell procedures to parse positional parameters. optstring con- tains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. The colon and question mark characters may not be used as option char- acters. Each time it is invoked, getopts places the next option in the shell vari- able name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset OPTIND auto- matically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used. When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument, and name is set to ?. getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead. getopts can report errors in two ways. If the first character of optstring is a colon, silent error reporting is used. In normal operation diagnostic messages are printed when invalid options or missing option arguments are encountered. If the variable OPTERR is set to 0, no error messages will be displayed, even if the first character of optstring is not a colon. If an invalid option is seen, getopts places ? into name and, if not silent, prints an error message and unsets OPTARG. If getopts is silent, the option character found is placed in OPTARG and no diagnostic message is printed. If a required argument is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a colon (:) is placed in name and OPTARG is set to the option charac- ter found. getopts returns true if an option, specified or unspecified, is found. It returns false if the end of options is encountered or an error occurs. hash [-lr] [-p filename] [-dt] [name] For each name, the full file name of the command is determined by searching the directories in $PATH and remembered. If the -p option is supplied, no path search is performed, and filename is used as the full file name of the command. The -r option causes the shell to forget all remembered locations. The -d option causes the shell to forget the remembered location of each name. If the -t option is supplied, the full pathname to which each name corresponds is printed. If multiple name arguments are supplied with -t, the name is printed before the hashed full pathname. The -l option causes output to be displayed in a format that may be reused as input. If no arguments are given, or if only -l is supplied, information about remembered commands is printed. The return status is true unless a name is not found or an invalid option is supplied. help [-s] [pattern] Display helpful information about builtin commands. If pattern is specified, help gives detailed help on all commands matching pattern; otherwise help for all the builtins and shell control structures is printed. The -s option restricts the infor- mation displayed to a short usage synopsis. The return status is 0 unless no command matches pattern. history [n] history -c history -d offset history -anrw [filename] history -p arg [arg ...] history -s arg [arg ...] With no options, display the command history list with line numbers. Lines listed with a * have been modified. An argument of n lists only the last n lines. If the shell variable HISTTIMEFORMAT is set and not null, it is used as a format string for strftime(3) to display the time stamp associated with each displayed history entry. No intervening blank is printed between the formatted time stamp and the history line. If filename is supplied, it is used as the name of the history file; if not, the value of HISTFILE is used. Options, if supplied, have the following meanings: -c Clear the history list by deleting all the entries. -d offset Delete the history entry at position offset. -a Append the ‘‘new’’ history lines (history lines entered since the beginning of the current bash session) to the history file. -n Read the history lines not already read from the history file into the current history list. These are lines appended to the history file since the begin- ning of the current bash session. -r Read the contents of the history file and use them as the current history. -w Write the current history to the history file, overwriting the history file’s contents. -p Perform history substitution on the following args and display the result on the standard output. Does not store the results in the history list. Each arg must be quoted to disable normal history expansion. -s Store the args in the history list as a single entry. The last command in the history list is removed before the args are added. If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file. The return value is 0 unless an invalid option is encountered, an error occurs while reading or writing the history file, an invalid offset is supplied as an argument to -d, or the history expansion supplied as an argument to -p fails. jobs [-lnprs] [ jobspec ... ] jobs -x command [ args ... ] The first form lists the active jobs. The options have the following meanings: -l List process IDs in addition to the normal information. -p List only the process ID of the job’s process group leader. -n Display information only about jobs that have changed status since the user was last notified of their status. -r Restrict output to running jobs. -s Restrict output to stopped jobs. If jobspec is given, output is restricted to information about that job. The return status is 0 unless an invalid option is encountered or an invalid jobspec is sup- plied. If the -x option is supplied, jobs replaces any jobspec found in command or args with the corresponding process group ID, and executes command passing it args, returning its exit status. kill [-s sigspec | -n signum | -sigspec] [pid | jobspec] ... kill -l [sigspec | exit_status] Send the signal named by sigspec or signum to the processes named by pid or jobspec. sigspec is either a case-insensitive signal name such as SIGKILL (with or without the SIG prefix) or a signal number; signum is a signal number. If sigspec is not present, then SIGTERM is assumed. An argument of -l lists the signal names. If any arguments are supplied when -l is given, the names of the signals corresponding to the arguments are listed, and the return status is 0. The exit_status argument to -l is a number specifying either a signal number or the exit status of a process termi- nated by a signal. kill returns true if at least one signal was successfully sent, or false if an error occurs or an invalid option is encountered. let arg [arg ...] Each arg is an arithmetic expression to be evaluated (see ARITHMETIC EVALUATION). If the last arg evaluates to 0, let returns 1; 0 is returned otherwise. local [option] [name[=value] ...] For each argument, a local variable named name is created, and assigned value. The option can be any of the options accepted by declare. When local is used within a function, it causes the variable name to have a visible scope restricted to that function and its children. With no operands, local writes a list of local variables to the standard output. It is an error to use local when not within a function. The return status is 0 unless local is used outside a function, an invalid name is sup- plied, or name is a readonly variable. logout Exit a login shell. popd [-n] [+n] [-n] Removes entries from the directory stack. With no arguments, removes the top direc- tory from the stack, and performs a cd to the new top directory. Arguments, if sup- plied, have the following meanings: +n Removes the nth entry counting from the left of the list shown by dirs, start- ing with zero. For example: ‘‘popd +0’’ removes the first directory, ‘‘popd +1’’ the second. -n Removes the nth entry counting from the right of the list shown by dirs, starting with zero. For example: ‘‘popd -0’’ removes the last directory, ‘‘popd -1’’ the next to last. -n Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated. If the popd command is successful, a dirs is performed as well, and the return status is 0. popd returns false if an invalid option is encountered, the directory stack is empty, a non-existent directory stack entry is specified, or the directory change fails. printf [-v var] format [arguments] Write the formatted arguments to the standard output under the control of the format. The format is a character string which contains three types of objects: plain charac- ters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument. In addition to the standard printf(1) formats, %b causes printf to expand backslash escape sequences in the cor- responding argument (except that \c terminates output, backslashes in \', \", and \? are not removed, and octal escapes beginning with \0 may contain up to four digits), and %q causes printf to output the corresponding argument in a format that can be reused as shell input. The -v option causes the output to be assigned to the variable var rather than being printed to the standard output. The format is reused as necessary to consume all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied. The return value is zero on success, non-zero on failure. pushd [-n] [dir] pushd [-n] [+n] [-n] Adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. With no arguments, exchanges the top two directories and returns 0, unless the directory stack is empty. Arguments, if supplied, have the following meanings: +n Rotates the stack so that the nth directory (counting from the left of the list shown by dirs, starting with zero) is at the top. -n Rotates the stack so that the nth directory (counting from the right of the list shown by dirs, starting with zero) is at the top. -n Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated. dir Adds dir to the directory stack at the top, making it the new current working directory. If the pushd command is successful, a dirs is performed as well. If the first form is used, pushd returns 0 unless the cd to dir fails. With the second form, pushd returns 0 unless the directory stack is empty, a non-existent directory stack element is specified, or the directory change to the specified new current directory fails. pwd [-LP] Print the absolute pathname of the current working directory. The pathname printed contains no symbolic links if the -P option is supplied or the -o physical option to the set builtin command is enabled. If the -L option is used, the pathname printed may contain symbolic links. The return status is 0 unless an error occurs while reading the name of the current directory or an invalid option is supplied. read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...] One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values. The characters in IFS are used to split the line into words. The backslash character (\) may be used to remove any special meaning for the next character read and for line continuation. Options, if supplied, have the following meanings: -a aname The words are assigned to sequential indices of the array variable aname, starting at 0. aname is unset before any new values are assigned. Other name arguments are ignored. -d delim The first character of delim is used to terminate the input line, rather than newline. -e If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line. -n nchars read returns after reading nchars characters rather than waiting for a com- plete line of input. -p prompt Display prompt on standard error, without a trailing newline, before attempt- ing to read any input. The prompt is displayed only if input is coming from a terminal. -r Backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation. -s Silent mode. If input is coming from a terminal, characters are not echoed. -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect if read is not reading input from the terminal or a pipe. -u fd Read input from file descriptor fd. If no names are supplied, the line read is assigned to the variable REPLY. The return code is zero, unless end-of-file is encountered, read times out, or an invalid file descriptor is supplied as the argument to -u. readonly [-apf] [name[=word] ...] The given names are marked readonly; the values of these names may not be changed by subsequent assignment. If the -f option is supplied, the functions corresponding to the names are so marked. The -a option restricts the variables to arrays. If no name arguments are given, or if the -p option is supplied, a list of all readonly names is printed. The -p option causes output to be displayed in a format that may be reused as input. If a variable name is followed by =word, the value of the vari- able is set to word. The return status is 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a function. return [n] Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body. If used outside a function, but during execution of a script by the . (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by ., the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script. set [--abefhkmnptuvxBCHP] [-o option] [arg ...] Without options, the name and value of each shell variable are displayed in a format that can be reused as input for setting or resetting the currently-set variables. Read-only variables cannot be reset. In posix mode, only shell variables are listed. The output is sorted according to the current locale. When options are specified, they set or unset shell attributes. Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n. Options, if specified, have the following meanings: -a Automatically mark variables and functions which are modified or created for export to the environment of subsequent commands. -b Report the status of terminated background jobs immediately, rather than before the next primary prompt. This is effective only when job control is enabled. -e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or ││ list, or if the command’s return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits. -f Disable pathname expansion. -h Remember the location of commands as they are looked up for execution. This is enabled by default. -k All arguments in the form of assignment statements are placed in the environ- ment for a command, not just those that precede the command name. -m Monitor mode. Job control is enabled. This option is on by default for interactive shells on systems that support it (see JOB CONTROL above). Back- ground processes run in a separate process group and a line containing their exit status is printed upon their completion. -n Read commands but do not execute them. This may be used to check a shell script for syntax errors. This is ignored by interactive shells. -o option-name The option-name can be one of the following: allexport Same as -a. braceexpand Same as -B. emacs Use an emacs-style command line editing interface. This is enabled by default when the shell is interactive, unless the shell is started with the --noediting option. errtrace Same as -E. functrace Same as -T. errexit Same as -e. hashall Same as -h. histexpand Same as -H. history Enable command history, as described above under HISTORY. This option is on by default in interactive shells. ignoreeof The effect is as if the shell command ‘‘IGNOREEOF=10’’ had been exe- cuted (see Shell Variables above). keyword Same as -k. monitor Same as -m. noclobber Same as -C. noexec Same as -n. noglob Same as -f. nolog Currently ignored. notify Same as -b. nounset Same as -u. onecmd Same as -t. physical Same as -P. pipefail If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all command

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值