《Linux实验要求》

 

实验 1:登录和使用基本的 Linux 命令

 

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。 有另外一个无特权用户 student,密码 student 的账户存在。

 

实验目标:

熟悉基本命令,使用基本的命令执行基本的 linux 任务和命令。

 

实验背景:

首先,练习使用 Linux 命令来登录、改变口令、转换成超级用户、使用 cat 命令来查看文件、使用 nano 命令来改变文件。

 

实验要求:

1、登录和使用基本的 linux 命令

2、修改密码,转换成超级用户

3、查看和修改文件

 

实验详解:

1、按 Ctrl+Alt+F2 切换到虚拟控制台(tty2)

:

Red Hat Enterprise Linux Server release 6.0 (Santiago)

Kernel 2.6.32-71.e16.x86_64 on an x86_64 SA2

desktopX login:

 

2、登录为用户 student: login:

在这个提示下输入 student,

然后按回车; Password:

在这个提示后输入 student 的口令 。默认情况下口令为 student:

desktopX login:student

Password:

[student@desktopX ~]$

注意:口令在你输入的时候是不会出现在屏幕上的。

 

3、使用 passwd 来设置口令。passwd 命令会首先向你询问当前的口令。输入当前密码:

[student@desktopX ~]$

Changing password for user student.

Changing password for student

(current) UNIX password:

  

 

4、passwd 命令会检查你输入的口令的强度以确保它达到一定的难猜程度。试着输入一个坏口令来测试这一功能:把口令设置成你的用户名 student:

New UNIX password:

BAD PASSWORD: it is based on your username

New UNIX password:

注意:口令被拒绝。你会被提示输入一个好一点的口令。

 

5、再试一次。这次设置一个复杂的口令。混合使用大小写字母、数字和标点。至少使用八个字符。你会被提示把口令再输入一次。如果你选择的口令足够强健,并且两次输入的口令相同,口令就会被成功改变,你就会看到这样的输出:

New UNIX password:

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

注意:如果你的口令被拒绝了,就继续尝试,知道成功为止。

 

6、运行 exit 命令来注销:

[student@desktopX ~]$ exit

使用你的新口令重新登录

desktopX login: student

Password:

[student@desktopX ~]$

 

7、现在你就登录为用户 student 了,这是一个不具有特权的用户。在本次实验的后半部,你需要超级用户的特权来运行命令。因此,首先使用 su 命令编程超级用户,在提示输入的口令的时候输入 :

[student@desktopX ~]$ su –

Password:

[root@desktopX ~]#

注意:在使用 su 命令是使用了“-”这个参数。加了这个减号的目的是使环境变量和欲转换的用户相同,不加是取得用户的临时权限。

注意命令提示符的变化:所显示的用户名现在是 root,提示后的最后一个字符是一个#而不是$。这两个外观上的变化表明你现在已有超级用户特权了。从现在起,直到你从超级用户 shell 退出,你所运行的命令都是会带有完全的特权。

 

8、使用 passwd 命令把 student 帐号的口令改为 student:

[root@desktopX ~]# passwd student

Changing password for user student.

New UNIX password:

BAD PASSWORD: it is based on a directory word

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

 

9、使用 cat 命令查看/etc/issue:

[root@desktopX ~]# cat /etc/issue

Red Hat Enterprise Linux Server release 6.0 (Santigo)

Kernel on an m

[root@desktopX ~]#

用户能够设置自己的口令,系统不允许 他 们 根 据 字 典 中 的 词

(如”redhat”)来选择不安全的口令。虽然根用户不受这些规则的制

约,企图设置不安全口令时也会产生警告信息。我们强烈建议你在课堂之

外的环境中不要忽略这些信息

/etc/issue 文件在登录提示前显示。默认的内容如上所示(根据版本和配置的不同,内容会略有出入)

 

10、使用 nano 命令编辑/etc/issue:

[root@desktopX ~]# nano /etc/issue

注意:当你开始打字时,文本在闪烁的光标处被输入。在页面底部是一个命令菜单。^

符号代表你需要同时按住 Ctrl 键。例如,Ctrl+X 会导致 nano 退出。

 

11、在/etc/issue 顶端新添加一行,使欢迎信息更为有好:

Welcome!

按 Ctrl+X 来保存改变。nano 命令会向你询问是否要保存改变(“保存被修改的缓存”。)

输入 y 来保存改变。

nano 命令会建议你保存到/etc/issue 这个文件,这正是我们打算做的。按 Enter 键来确认,保存文件。

 

12、再查看一下该文件,你会看到:

[root@desktopX ~]# cat /etc/issue

Red Hat Enterprise Linux Server release 6.0 (Santigo)

Kernel on an m

[root@desktopX ~]#

现在你就可以查看所做改变的效果。但是对/etc/issue 文件的改变直到下次登陆时才会生效。强迫登陆提示重置的最快方法是转换到Ctrl+Alt+F2 到 Ctrl+Alt+F6 这些控制台,然后在每个提示后逐一按 Ctrl+D,这会导致登陆提示终止并重新启动,

重新读取/etc/issue 文件,显示其中的新内容。

 

13、清除:

按 Ctrl+Alt+F2 返回刚才登陆的虚拟控制台。输入 exit 来推出超级用户。

注意:提示符的变化。

再输入 exit 来注销。登录界面就会返回,包括你新改变的欢迎信息。

按 Ctrl+Alt+F7 来返回图形化界面。

 

实验 2:获取对使用命令的帮助

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。有另外一个无特权用户 student,密码 student 的账户存在。

 

实验目标:

熟悉手头可用的资源,以便回答有关红帽企业版 Linux 命令的问题。

 

实验背景:

你想再次修改/etc/issue 文件(参见上一次实验)。这一次,你想查看一下有没有能够显示系统主机名的转义符。

 

实验要求:

1、使用帮助工具

2、使用 man 命令来解决问题

实验详解:

1、使用口令 student 登录为用户 student。如果你使用的是图形化环境,点击[应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、首先参考 man 说明书中的/etc/issus 项目:

[student@desktopX ~]$ man issue

 

注意:其中说明转义符要根据 mingetty 而定。

还请注意 See Also 部分让你参考 mingetty(8)。

 

3、跳到有关转义符(escape)的部分,输入:

/escape

你的光标应该位于标题 ISSUE ESCAPE 上。

如果不是, n 来把光标移动到这个标题上。

 

4、切换到 root 用户,在 nano 中打开/etc/issue 这个文件:

[student@desktopX ~]$ su -

[root@desktopX ~]# nano /etc/issue

 

5、修改你前面添加的欢迎行,这一次使用转义符来包括主机名:

/etc/issue 文件现在应该类似:

转义符代表系统的主机名

Welcome to !

Red Hat Enterprise Linux Server release 6.0 (Santigo)

Kernel on an m

 

6、按 Ctrl+x 来保存文件,退出 nano:

Kernel on an m

[root@desktopX ~]#

 

7、使用 exit 命令关闭 shell 并注销。现在你就应该能够看到新修改的登录提示了!访问其他虚拟终端来查看这些内容的变化。记住,你需要按 Enter 键才能看到更新的内容。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

实验 3:搜索和处理文件

 

实验目标:

培养对 find 工具的更深理解,掌握如何使用 find 工具搜索并处理文件。

 

实验背景:

你在使用 linux 时遇到了一些问题,这时候你可能需要参考 find 命令的说明书页。

当 find 命令试图进入你没有读取权限的目录时,你会看到许多 ”Permission  denied (权限不够)”的消息,此时你不必在乎这些消息,在 find 命令后面添加 2> /dev/null 会隐藏它们。

 

实验详解:

1、使用口令 student 登录为用户 student。如果你使用的是图形化环境,点击[应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、列举所有/var/lib 目录下的所属能过户为人品 rpm 的文件:

[student@desktopX ~]$ find /var/lib -user rpm  2> /dev/null

 

3、列举所有/var 目录下的所属用户为 root、所属组群为 mail 的文件:

[student@desktopX ~]$ find /var/ -user -group mail 2> /dev/null

 

4、生成一个 ls –l 格式的列表,列举系统上所属用户不是 root、bin 或 student 的所有文件:

[student@desktopX ~]$ find / -not -user root -not -user bin -not -user student -ls 2> /dev/null

或者

[student@desktopX ~]$ find / ! -user root ! -user bin ! -user student -ls 2 > /dev/null

注意:这会花很长的一段时间,因此建议大家在一个终端上运行这条命令,在另一个终端上做下一道题目。

 

5、生成一个 ls –l 格式的列表,列举/usr/bin 目录下所有大于 50KB 的文件:

[student@desktopX ~]$ find /usr/bin -size +50k -ls 2> /dev/null

 

6、对/etc/mail 目录下的所有文件执行 file 命令:

[student@desktopX ~]$ find /etc/mail -exec file {} ; 2> /dev/null

 

7、生成一个 ls -l 格式的列表,列举/etc 目录下的所有符号链接:

[student@desktopX ~]$ find /etc/ -type | -ls 2> /dev/null

 

8、生成一个 ls -l 格式的列表,列举/tmp 目录下的所属用户为 student、修改时间早于120 分钟前的所有“常规”文件:

[student@desktopX ~]$ find /tmp -user student -mmin +120 -type f -ls 2> /dev/null

 

9、列举/bin 和/usr/bin 目录下所有启用了 SetUID 权限位的文件:

[student@desktopX ~]$ find /bin /usr/bin -perm -4000 2> /dev/null 或者

[student@desktopX ~]$ find /bin /usr/bin -perm -u+s 2> /dev/null

 

 

 

 

 

 

 

实验 4:组织目录和文件

实验目标:

熟悉几个基本的操作系统文件和目录的命令的功能、 语法和用法, 整理出一个更有条理的主目录,每个文件都位于恰当的子目录。

 

实验背景:

你的主目录中已经积压了一些文件,你决定开始规整它们。你打算新建几个子目录,然后复制或转移文件来适应这个新方案。此外,你还有一些不需要的文件,它们必须被删除。

 

实验要求:

1、使用 ls 查看文件及其属性

2、使用 cd 切换路径

3、使用 touch、mkdir 创建相应的文件及文件夹

4、使用 rm、rmdir 删除文件及文件夹

 

实验详解:

1、使用口令 student 登录为用户 student。如果你使用的是图形化环境,点击[应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、登录到系统之后,你应该就位于你的主目录中。使用”pwd”(打印工作目录)命令

来校验:

[student@desktopX ~]$ pwd

/home/student

 

3、使用一下每个命令来检查你的主目录中的文件:

ls

ls –a

ls -al

观察返回的文件数量有何不同。

 

4、现在,你将使用 touch 命令来创建该练习序列的文件,输入:

[student@desktopX ~]$ touch {report,graph}_{jan,feb,mar}

 

5、使用 ls 命令来检查前一个命令执行的结果。你应该看到你的主目录中新建了一下六个空文件:

[student@desktopX ~]$ ls

graph_feb

graph_jan

graph_mar

report_feb

report_jan

report_mar

 

6、为了组织文件,你必须首先新建一些目录。使用 mkdir 命令来新建目录。在更改目录时,请确定当前工作目录和预料中一样:

[student@desktopX ~]$ mkdir Projects

[student@desktopX ~]$ mkdir Projects/graphs

[student@desktopX ~]$ cd Projects

[student@desktopX Projects]$ cd Projects

[student@desktopX Projects]$ mkdir reports

[student@desktopX Projects]$ cd reports

[student@desktopX reports]$ mkdir ../Backups

使用 ls 命令来检查你的工作结果:

[student@desktopX reports]$ cd

[student@desktopX ~]$ ls -l

 

 

7、首先,把所有文件名中带有 graph 的文件都转移到 Projects 目录中的 graphs 子目录中。分两个步骤来完成:

第一步:转移一个文件

第二步:转移两个文件

[student@desktopX ~]$ mv graph_jan Projects/graphs

[student@desktopX ~]$ mv graph_feb graph_mar Projects/graphs

[student@desktopX ~]$ ls -l Projects/graphs

 

 

8、下一步,把两个”report”文件移动到 Projects 目录中的 reports 子目录中。使用这些命令来转移这些文件:

[student@desktopX ~]$ mv report_jan report_feb Projects/reports

[student@desktopX ~]$ ls –l Projects/reports

totol 2

-rw-rw-r-- 1 student student 0 Sep 30 21:08 report_feb

-rw-rw-r-- 1 student student 0 Sep 30 21:08 report_jan

 

 

9、删除剩下的 report 文件:

[student@desktopX ~]$ rm report_mar

[student@desktopX ~]$ ls Projects

 

10、转换到 Backups 的目录,把 January 文件复制到这个目录中。一个文件使用绝对路径名复制,另一个文件使用相对路径名复制:

[student@desktopX ~]$ cd Projects/Backups

[student@desktopX Backups]$ pwd

/home/student/Projects/Backups

[student@desktopX Backups]$ cp ../reports/report_jan .

[student@desktopX Backups]$ cp /home/student/Projects/graphs/graph .

//’.’表示当前工作的目录

 

11、注销,或运行 exit 命令来关闭图形化终端。

 

 

 

实验 5:权限的设置和更改

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。有另外一个无特权用户 student,密码 student 的账户存在,还有一个无特权的用户 visitor,密码 password的账户存在。

 

实验目标:

了解 linux 的安全模型,熟悉用户账号的组群帐号,设置并修改文件权限。

 

实验背景:

在 Linux 系统中,各个用户(除超级用户外)的空间之间是隔离的,一个用户是不能进入其他用户的注册空间的。通常普通用户可以通过超级用户在/usr 目录下建立共享的用户目录,

然后将该共享目录的属主权移交给普通用户。普通用户可将该目录可设置成允许同组用户访问,也可以设置成允许所有用户访问。并在该目录中提供允许共享的文件。

 

实验要求:

1、转换用户帐号,查看 id 值

2、使用 chown 修改文件属主

3、使用 chmod 采用两种不同方法修改文件权限

4、使用 umask 修改默认的权限

 

实验详解:

1、使用口令 password 登录为用户 visitor。如果你使用的是图形化环境,点击[应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、登录到系统之后,你应该就位于你的主目录中。使用”id”命令来查看你的相关信息:

[student@desktopX ~]$ id

uid=501(visitor) gid=501(visitor) group=501(visitor)

„„

[student@desktopX ~]$ pwd

/home/visitor

 

3、运行 su – student 命令来转换成用户 student,在运行上一步:

[student@desktopX ~]$ id

uid=500(visitor) gid=500(visitor) group=500(visitor)

„„

[student@desktopX ~]$ pwd

/home/student

思考:为什么我们要用 su -而不直接用 su 。

 

4、现在切换到 root 用户,在/usr 中创建两个文件夹,分别为 student 和 visitor:

[student@desktopX ~]$ su -

Password:

[root@desktopX ~]# cd /usr

[root@desktopX usr]# mkdir student visitor

[root@desktopX usr]# ls –ld student visitor

 

5、使用 chown 命令来修改这两个文件夹的属主:

[root@desktopX usr]# chown student:student student

[root@desktopX usr]# chown visitor:visitor visitor

 

6、回到 student 文件夹,用 student 账户创建两个文本文件,分别为 tf1 和 tf2,并赋予

不同的权限:

[student@desktopX student]$ cat >tf1

This file is for all user

[student@desktopX student]$ cat >tf2

This file is for the users in the same groups

[student@desktopX student]$ ls –l tf1 tf2

 

[student@desktopX student]$ chmod 750 tf2

[student@desktopX student]$ ls –l tf1 tf2

 

7、切换到 visitor 用户,分别访问刚才创建的两个文件 tf1 和 tf2:

[student@desktopX student]$ su visitor

Password:

[visitor@desktopX student]$ cat tf1

This file is for all user

[visitor@desktopX student]$ cat tf2

cat: tf2: Permission denied

 

8、切换到 root 用户,使用 chmod 命令增加 visitor 的访问权限:

[visitor@desktopX student]$ su

[root@ desktopX student]# chmod o+r tf2

 

9、回到 visitor 用户,继续访问 tf2:

[visitor@desktopX student]$ cat tf2

This file is for the users in the same groups

 

10、使用 umask 修改文件的默认权限:

[student@desktopX student]$ umask 0002

[student@desktopX student]$ mkdir dir1

[student@desktopX student]$ umask –p 007

[student@desktopX student]$ mkdir dir2

[student@desktopX student]$ ls –ld dir1 dir2

 

这时候如果切换到 visitor 用户访问 dir2 将会提示权限不够

[visitor@desktopX student]$ cd dir2

dir2: .: Permission denied

 

 

 

实验 6:Linux 文件系统

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。有另外一个无特权用户 student,密码 student 的账户存在。

 

实验目标:

更好的理解 Linux 文件系统基础,包括创建和使用链接;使用 locate 和 find 命令查找文件;归档和压缩文件。

 

实验背景:

每次启动的时候,你的系统的主硬盘驱动器都开始发出讨厌的噪音。

你怀疑硬盘可能要寿终正了,其中的人数据也要跟着陪葬了。由于你之前没有进行过数据备份的操作,所以你决定手工备份几个至关重要的文件。假设/tmp 目录所在的分区位于另一个驱动器,因此你决定暂时把备份存放在那里。

 

实验要求:

1、使用 ln 分别创建软连接、硬链接

2、使用 dh 查看磁盘用量

3、使用 tar、gzip、bzip2 备份配置文件

 

实验详解:

1、使用口令 student 登录为用户 student。如果你使用的是图形化环境,点击[应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、使用 cp 命令把 usr/share/dict/word 文件复制到你的主目录中:

[student@desktopX ~]$ cp /usr/share/dict/words .

注意:此处’.’表示当前目录。

 

3、查看/usr/share/dict/words 的相关信息:

[student@desktopX ~]$ ls /usr/share/dict/words

-rw-r—r-- 1 root root 409305 Sep 30 21:08 linux.words

lrwxrwxrwx 1 root root 11 Sep 30 21:08 word -> linux.word

这里的文件 word 是一个符号链接:文件模式的第一个字符是代表符号链接的’l’;并且文件名包括了显示链接目标的”-> linux.word”。

 

4、在主目录中创建一个符号链接和一个硬链接,都指向你的主目录中的 words 文件:

[student@desktopX ~]$ ln -s word soft

[student@desktopX ~]$ ln word hard

 

5、

测试一下新建的连接是否正确地指向 words 中的数据,

我们使用 head 命令显示文件中的前 10 行:

 

[student@desktopX ~]$ head hard soft

我们可以看到,两者输出相同,就说明我们的链接创建正确。

 

6、详细查看两个文件的相关信息,比较两种链接的区别:

[student@desktopX ~]$ ls -il hard soft

84040 -rw-r--r-- 2 student student 4950996 Aug 22 14:43 hard

84021 lrwxrwxrwx 1 student student 5 Aug 22 15:18 soft -> words

 

[student@desktopX ~]$ stat hard soft

File: `hard'

Size: 4950996

Blocks: 9712

IO Block: 4096 regular file

Device: fd01h/64769d Inode: 84040

Links: 2

Access: (0644/-rw-r--r--) Uid: ( 500/ student) Gid: ( 500/ student)

Access: 2011-08-22 15:22:48.000000000 +0800

Modify: 2011-08-22 14:43:10.000000000 +0800

Change: 2011-08-22 15:17:55.000000000 +0800

File: `soft' -> `words'

Size: 5

Blocks: 2

IO Block: 4096 symbolic link

Device: fd01h/64769d Inode: 84021

Links: 1

Access: (0777/lrwxrwxrwx) Uid: ( 500/ student) Gid: ( 500/ student)

Access: 2011-08-22 15:36:42.000000000 +0800

Modify: 2011-08-22 15:18:35.000000000 +0800

Change: 2011-08-22 15:18:35.000000000 +0800

 

7、使用 df 命令来判断每个文件系统上的空余空间总量:

[student@desktopX ~]$ dh

[student@desktopX ~]$ dh -h

[student@desktopX ~]$ dh -H

比较这三者输出的差别。

 

8、使用 tar 命令把/etc 的内容打包,保存在/tmp 中:

[student@desktopX ~]$ su

[student@desktopX ~]$ tar -cvf /tmp/confbackup.tar /etc

 

9、查看压缩文件的属性,特别注意 tar 包的大小:

[student@desktopX ~]$ ls -lh /tmp/confbackup.tar

 

10、使用 gzip 命令来压缩归档文件,注意这个新文件的大小:

[student@desktopX ~]$ cd /tmp

[student@desktopX tmp]$ gzip -v confbackup.tar

[student@desktopX tmp]$ ls -lh confbackup.tar.gz

 

11、给文件解压,用 bzip2 重新压缩,比较压缩文件的大小:

[student@desktopX tmp]$ gunzip confbackup.tar.gz

[student@desktopX tmp]$ bzip2 -v confbackup.tar

[student@desktopX tmp]$ ls -lh confbackup.tar.bz2

12、注销,清除。

 

 

 

 

实验 7:配置 Bash Shell

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统有另外一个无特权用户 student,密码 student 的账户存在。

 

实验目标:

逐渐掌握更多定制 Bash shell 的操作的技能,包括创建等值的别名,

修改 bash 提示以及简单的 shell 编程。

 

实验背景:

一般我们在 linux 下直接用命令行直接进行操作的。这种方法很快捷很方便,一旦大批量的操作时单纯的命令行操作就显得不足。我们可以采用批处理的方法帮助我们搞定这样的难题。

 

实验要求:

1、使用 alias 创建别名

2、使用 PS1 修改 bash 的提示符

3、编写简单的 shell 小程序

 

实验详解:

1、使用口令 student 登录为用户 student。如果你使用的是图形化环境,点击[应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、使用 alias 命令创建一个别名,当你输入 c 后系统会运行 clear 命令来清除屏幕:

[student@desktopX ~]$ alias c=’clear’

[student@desktopX ~]$ alias

[student@desktopX ~]$ c

注意: 我们刚才创建的别名’c’并没有保存,注销后就别名就会丢失。要使这个别名在用户 student 每次登录都能使用,需要保存。

 

3、修改.bashrc,保存创建的别名:

[student@desktopX ~]$ vim .bashrc

 

找到包含以下文本的那一行

#User specific aliases and functions

把你的别名直接添加到那一行下面

alias c=’clear’

保存文件退出

:wq

 

4、注销后重新登录终端,输入以下命令测试别名是否保存:

[student@desktopX ~]$ alias

[student@desktopX ~]$ c

观察屏幕是否被清除。

 

5、使用 PS1 命令改变 bash 提示:

[student@desktopX ~]$ PS1=’Red Hat -> ’

Red Hat -> ls

 

6、修改 bash 恢复为主机名和传统的美元符号:

Red Hat -> PS1=’h $’

desktopX $

 

7、在 bash 中添加历史提示:

desktopX $ PS1=’[u@h w (!) ]$ ’

[student@desktopX ~ (21)]$

 

8、编写一个 shell 脚本,当你输入 yes 时它会返回 no:

[student@desktopX ~ (21)]$ cat >test_script

#! /bin/bash

#if you enter yes it will echo no ,

#if you enter no it will echo yes .

echo "You want YES or NO:"

read ANSWER

if [ "$ANSWER" = "YES" ] || [ "$ANSWER" = "yes" ]

then

echo "Your idea is NO";

elif [ "$ANSWER" = "NO" ] || [ "$ANSWER" = "no" ]; then

echo "Your idea is YES";

else

echo "you are wrong" ;

fi

 

9、运行 shell 脚本 test_script:

[student@desktopX ~ (21)]$ ./test_script

You want YES or NO:

YES

Your idea is NO

  

10、注销,清除。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

实验 8:RHEL6 启动管理

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。

 

实验目标:

熟悉 Red Hat Enterprise Linux 启动过程,了解 grub 配置及运行级别。

 

实验背景:

你的台式机原来是用于家用的,开机时默认采用图形化界面的登录方式。

现在你想把它的模式改为用于服务器的完全多用户级别。这时候我们就需要修改运行级别。

 

实验要求:

1、查看 grub,熟悉启动项相关事宜

2、修改运行级别

实验详解:

1、以 root 用户的身份登录系统。如果你使用的是图形化环境,点击 [应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、使用 vim 编辑器查看自己的 grub 配置文件:

[root@desktopX ~]$ vim /boot/grub/grub.conf

 

default=0

timeout=5

splashimage=(hd0,0)/grub/splash.xpm.gz

hiddenmenu

title Red Hat Enterprise Linux Server (2.6.18-164.el5)

root (hd0,0)

kernel /vmlinuz-2.6.18-164.el5 ro root=/dev/vol0/root rhgb quiet

initrd /initrd-2.6.18-164.el5.img

title Red Hat Enterprise Linux Server (2.6.18-164.el5xen)

root (hd0,0)

kernel /xen.gz-2.6.18-164.el5

module /vmlinuz-2.6.18-164.el5xen ro root=/dev/vol0/root rhgb quiet

module /initrd-2.6.18-164.el5xen.img

title Install Red Hat Enterprise Linux 5

root (hd0,0)

kernel /vmlinuz-5 ks=http://192.168.0.254/workstation.cfg ksdevice=eth0 noipv6

initrd /initrd-5

password --md5 $1$FSUEU/$uhUUc8USBK5QAXc.BfW4m.

 

在 grub.conf 中我们可以看到启动菜单的停留时间 timeout=5,以及启动项顺序。你可以根据自身情况修改启动项的顺序。

注意:上述 grub.conf 的信息与各位的电脑的分区和系统有关,每个人并非完全相同。

 

3、更改运行级别,设置一个默认引导到运行级别 3 的系统:

[root@desktopX ~]$ vim /etc/inittab

 

# Default runlevel. The runlevels used by RHS are:

# 0 - halt (Do NOT set initdefault to this)

# 1 - Single user mode

# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)

# 3 - Full multiuser mode

# 4 - unused

# 5 - X11

# 6 - reboot (Do NOT set initdefault to this)

#

id:5:initdefault:

# System initialization.

si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0

l1:1:wait:/etc/rc.d/rc 1

l2:2:wait:/etc/rc.d/rc 2

l3:3:wait:/etc/rc.d/rc 3

l4:4:wait:/etc/rc.d/rc 4

l5:5:wait:/etc/rc.d/rc 5

l6:6:wait:/etc/rc.d/rc 6

 

找到这一行将 5 改成 3

id:5:initdefault:

id:3:initdefault:

重启系统,启动后系统的运行级别就成为完全多用户模式。

 

4、将默认的运行级别恢复成 5:

修改方法与上述相同,这里不再重复。

 

 

 

 

 

 

实验 9:安装和管理软件

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。

实验目标:

挂载光盘,配置 yum 源,并通过 yum 安装更新软件。

 

实验背景:

你想要将你的系统连接到专用 yum 库来安装和升级软件。

 

实验要求:

1、配置 yun

2、尝试使用 yum 的基本语法

3、使用 yum 安装 wireshark

 

实验详解:

1、以 root 用户的身份登录系统。如果你使用的是图形化环境,点击 [应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、将 RHEL6 中的 Server 文件夹拷贝至/yum 下:

[root@desktopX ~]# mount /dev/cdrom /mnt

mount: block device /dev/cdrom is write-protected, mountinf read-only

 

[root@desktopX ~]# cp -rv /mnt/Server /yum

‘/mnt/Server/’ -> ‘/yum/Server’

‘/mnt/Server/’ -> ‘/yum/Server’

„„

注意:请保证你的/yum 目录有足够的空间,本实验约占用 2.6G。

 

3、编辑/etc/yum.repo.d 下的文件:

[root@desktopX ~]# cd /etc/yum.repo.d

[root@desktopX yum.repo.d]# ls

rhel-debuginfo.repo

[root@desktopX yum.repo.d]# cp rhel* local.repo

[root@desktopX yum.repo.d]# vim lo*

将源文件内容做如下修改

[Server]

name = RHEL local Server

baseurl = file:///yum/Server

enable = 1

gpgcheck = 1

gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

注意:

如果需要检查软件签名,

请设置 gpgcheak=1。

关于 baseurl,

如果 yum 源在本地,

则使用 file://本地地址;如果 yum 源在远端,则应该使用 http://或 ftp://。

 

4、进入 yum 目录,安装 createrepo:

[root@desktopX ~]# cd /yum/Server

[root@desktopX Server]# rpm -ivh createrepo*

warning: „„

注意:createrepo 是用来创建 yum 数据库的软件包。

 

5、使用 createrepo 命令生成 yum 仓库依赖关系:

[root@desktopX Server]# createrepo -g /yum/Server

„„

 

6、清空当前系统保存的 yum 信息:

[root@desktopX ~]# cd /etc/yum.repo.d

[root@desktopX yum.repo.d]# yum clean all

Loaded plugins: rhnplugin, security

„„

注意:每次修改 yum 数据库的内容,就需要清除客户机缓存信息,否则会出现不可预

料的错误。

 

7、刷新 yum 源列表:

[root@desktopX yum.repo.d]# yum list

Loaded plugins: rhnplugin, security

This system is not registered with RHM.

RHM support will be disabled.

„„

 

8、使用基本的 yum 命令搜索软件 wireshark:

[root@desktopX yum.repo.d]# yum search wireshark

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

========================= Matcheded: wireshark ===============================

wireshark.i386

:Network traffic analyzer

wireshark-gnome.i386 :Gnome desktop integration for wireshark and wireshark-usermode

 

9、查看 wireshark 的详细信息:

[root@desktopX yum.repo.d]# yum info wireshark

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

 

RHN support will be disabled.

Available Package

Name: wireshark

Arch: i386

Version: 1.0.8

Release: 1.el5_3.1

 

10、使用 yum 命令安装 wireshark:

[root@desktopX yum.repo.d]# yum install wireshark

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Setting up Insrall Process

Resolving Dependencices

--> Running transaction check

---> Package wireshark.i386 0:1.0.8-1.e15_3.1 set to be updated

--> Processing Dependency: libsmi.so.2 for package: wireshark

--> Running libsmi.i386 0:8.4.5-2.e15 set to be updated

 

yum 在安装过程中会自动处理依赖关系,遇到缺少的软件包会自动下载并安装。

当屏幕出现 Complete 时,表示安装完成。

 

 

 

yum 主要命令参考手册

参数

功能

help 显示使用信息

list 显示已安装和可用的软件包

search KEYWORD 按关键字列出软件包

info PACKAGENAME 列出软件包的详细信息

install 获取并安装包,包括所有依赖项

PACKAGENAME

remove 删除安装的软件包,包括所有受支持的包

PACKAGENAME

update 获取并安装更新版本的软件包,包括所有依赖项

PACKAGENAME

provide PATHNAME 可显示与指定的路径名相匹配的软件包(通常包括通配符)

 

 

 

 

 

 

实验 10:Linux 进程管理

实验环境:

安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证的系统。

实验目标:

进一步学习 Linux 下的进程管理,更加有利于对操作系统的认识和了解。

实验背景:

你想查看你的工作站上运行有点缓慢,因此你想查看以下你的工作站进行检测,查看你的系统上正在运行的哪些进程,又有哪些是不必要的,需要关掉的进程。

 

实验要求:

1、完成简单的进程查看、件事、调度和控制

2、找出系统中使用最多 CPU 时间的进程

 

实验详解:

1、以 root 用户的身份登录系统。如果你使用的是图形化环境,点击 [应用程序(Applications)]->[附件(System Tools)]->[终端(Terminal)]来打开终端:

 

2、使用 PS 命令来查看进程信息:

[root@desktop2 ~]# ps

 

为了查看更完整的进程信息,可以使用参数命令

[root@desktop2 ~]# ps axuf

[root@desktop2 ~]# ps axo pid,comm

3、搜索并列出使用 tty1 的进程:

[root@desktop2 ~]# ps axo comm,tty | grep tty1

使用管道符将 ps 命令的输出结果发送给 grep 命令进行筛选,这是最灵活的处理方法。

 

4、查看并调整进程优先级:

[root@desktop2 ~]# ps ao comm,nice

 

注意:进程优先级决定了 CPU 对进程调度顺序。在 Linux 中,进程优先级受 nice 值(nice valve)的影响,nice 值得范围是-20~19,默认为 0。nice 值越小,则进程对 CPU 的使用权越具有优先性。

 

5、使用 nice 命令改变调度优先级:

[root@desktop2 ~]# nice -n 5 vim

 

[root@desktop2 ~]# ps ao comm,nice,pid

这是在进程启动时使用的命令,对于已经启动的进程,需使用 renice 命令

 

[root@desktop2 ~]# renice 8 15396

注意:启动后的进程需要使用 renice 命令结合 pid 来改变优先级。

 

6、在命令行后面添加’&’符使之在后台运行:

[root@desktop2 ~]# vi &

[2] 15471

[root@desktop2 ~]# jobs

//列举后台作业的号码和名称

[1]-

Stopped

nice -n 5 vim

[2]+

Stopped

vi

 

[root@desktop2 ~]# fg 2

//使后台进程在前台恢复,需跟随作业号码

Ctrl+z

[root@desktop2 ~]# cat /dev/urandom > /dev/null

//为了实验后台恢复运行具有持续性的命令

Ctrl+z

 

 

[root@desktop2 ~]# jobs

[1]

Stopped

[2]-

Stopped

[3]+

Stopped

[root@desktop2 ~]# bg 3

[root@desktop2 ~]# jobs

[1]-

Stopped

[2]+

Stopped

[3]

Running

nice -n 5 vim

vi

cat /dev/urandom > /dev/null

//在后台恢复进程的运行,

需跟随作业的号码

nice -n 5 vim

vi

cat /dev/urandom > /dev/null

 

7、终止之前后台所有的进程:

[root@desktop2 ~]# killall cat

[root@desktop2 ~]# kill -9 %1

[root@desktop2 ~]# kill -9 %1

注意:kill 命令用来给进程发送控制信号,kill 命令后跟 PID 号或者有’%’前缀的 job 作业

号。killall 与 kill 命令有着同样的作用。

 

8、查找系统中占用 CPU 时间最多的进程:

[root@desktop2 ~]# ps axo pid,comm,pcpu

//查看进程的 PID、名称以及 CPU 占用率

[root@desktop2 ~]# ps axo pid,comm,pcpu -sort=pcpu

// sort 参数以 pcpu 为对象对输出的内容进行整理

[root@desktop2 ~]# ps axo pid,comm,pcpu -sort=pcpu | tail -n1

//将结果输出到 tail 命令筛选出最后一条

11695

makewhatis

 

ps 主要命令参考手册

参数

功能

a 包括所有终端中的进程

x 包括不连接终端的进程

u 显示进程所有者的信息

f 显示进程的父进程

o 属性定制:pid、comm、%cpu、%mem、state、tty、euser、

                         ruser

 

进程状态(State)

状态名   含义

Running 运行:进程正在运行

Sleeping 休眠:在内存中,但什么也不做

Uninterruptable 等待:正在等待资源,处于不可中断的休眠状态

Zombie 僵尸:已经终止了但尚未从进程列表中清除

 

 

 

 

 

Freecode : www.cnblogs.com/yym2013

原文地址:https://www.cnblogs.com/yym2013/p/3652695.html