Top

NTD LNXSEC DAY02

  1. 案例1:目录/文件基础操作
  2. 案例2:复制/删除/移动文档
  3. 案例3:使用vim新建/修改文件
  4. 案例4:管理用户账号
  5. 案例5:管理组账号
  6. 案例6:配置文档归属
  7. 案例7:配置访问权限
  8. 案例8:设置文件的 x 权限

1 案例1:目录/文件基础操作

1.1 问题

本例要求熟悉Linux系统中的目录/文件基础操作命令,在虚拟机上完成下列任务。
1)列出目录及文件属性

  • 查看根目录 / 下有哪些文档或子目录
  • 列出当前目录下的所有文档(包括隐藏文档)
  • 列出 /root/ 目录下以 ana开头的文档,识别文档大小

2)使用cat查看文件/etc/hostname 的内容
3)使用less分页阅读文件 /proc/cpuinfo 的内容
4)创建目录结构 /notes/linux

1.2 步骤

实现此案例需要按照如下步骤进行。
步骤一:列出目录及文件属性
1)查看根目录 / 下有哪些文档或子目录,这些文档的颜色有什么规律
[root@svr7 ~]# ls /
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr

在白底黑字的命令行终端上的ls显示结果中,黑色表示常规文件、深蓝色表示目录、浅蓝色表示快捷方式、绿色表示可执行文件、红色表示压缩包文件、黄色表示设备。
2)列出当前目录下的所有文档(包括隐藏文档)
[root@svr7 ~]# ls
anaconda-ks.cfg 公共 视频 文档 音乐
initial-setup-ks.cfg 模板 图片 下载 桌面
[root@svr7 ~]# ls -A
anaconda-ks.cfg .cshrc pass.txt 图片
.bash_history .dbus .ssh 文档
.bash_logout .esd_auth .tcshrc 下载
.bash_profile .ICEauthority .Xauthority 音乐
.bashrc initial-setup-ks.cfg 公共 桌面
.cache .local 模板
.config .mozilla 视频

3)列出 /root/ 目录下以 ana开头的文档,识别文档大小
[root@svr7 ~]# ls -lh /root/ana*
-rw———-. 1 root root 1.7K 4月 15 11:46 /root/anaconda-ks.cfg

步骤二:使用cat命令查看文件/etc/hostname 的内容
通过查看文件 /etc/hostname 的内容,可以了解当前系统的主机名。
[root@svr7 ~]# cat /etc/hostname
svr7.tedu.cn

步骤三:使用less分页阅读文件 /proc/cpuinfo 的内容
通过查看文件 /proc/cpuinfo,可以了解当前计算机的CPU处理器信息。
[root@svr7 ~]# less /proc/cpuinfo
processor : 0 //CPU核心编号
vendor_id : GenuineIntel //厂商ID
cpu family : 6
model : 61
model name : Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
stepping : 4
microcode : 0x21
cpu MHz : 2194.923 //CPU主频
cache size : 3072 KB //缓存
physical id : 0 //物理CPU编号
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 20
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt arat
.. .. //按q键可退出浏览
[root@svr7 ~]#

步骤四:创建目录结构 /notes/linux
创建多层目录结构时,注意添加 -p 选项。
1)创建新目录
[root@svr7 ~]# ls -ld /notes/linux
ls: 无法访问/notes/linux: 没有那个文件或目录
[root@svr7 ~]# mkdir /notes/linux
mkdir: 无法创建目录”/notes/linux”: 没有那个文件或目录
[root@svr7 ~]# mkdir -p /notes/linux

2)确认创建结果
[root@svr7 ~]# ls -ld /notes/linux/
drwxr-xr-x. 2 root root 6 12月 19 11:44 /notes/linux/

2 案例2:复制/删除/移动文档

2.1 问题

本例要求从命令行完成文档的复制/删除/移动等操作,相关说明如下:

  1. 在当前目录下创建一个子目录 mulu1
  2. 将文件夹 /boot/grub2/ 复制到目录mulu1下
  3. 将目录 /root/ 下以 .cfg 结尾的文件复制到mulu1下
  4. 将文件 /etc/redhat-release 复制为 /root/ver.txt
  5. 将文件 /root/ver.txt 移动到mulu1目录下
  6. 删除 mulu1 目录下的 grub2 子目录

    2.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:在当前目录下创建一个子目录 mulu1
    1)确认当前目录
    [root@svr7 ~]# pwd
    /root

2)创建子目录
[root@svr7 ~]# ls -ld /root/mulu1
ls: 无法访问/root/mulu1: 没有那个文件或目录
[root@svr7 ~]# mkdir /root/mulu1
[root@svr7 ~]# ls -ld /root/mulu1
drwxr-xr-x. 2 root root 6 4月 23 14:33 /root/mulu1

步骤二:将文件夹 /boot/grub2/ 复制到目录mulu1下
1)复制文件夹
[root@svr7 ~]# ls /root/mulu1/
[root@svr7 ~]# cp /boot/grub2/ /root/mulu1/
cp: 略过目录”/boot/grub2/“
[root@svr7 ~]# ls /root/mulu1/
[root@svr7 ~]# cp -r /boot/grub2/ /root/mulu1/

2)确认复制结果
[root@svr7 ~]# ls /root/mulu1/
grub2

步骤三、将目录 /root/ 下以 .cfg 结尾的文件复制到mulu1下
1)复制文件
[root@svr7 ~]# ls /root/.cfg
/root/anaconda-ks.cfg /root/initial-setup-ks.cfg
[root@svr7 ~]# ls /root/mulu1/
grub2
[root@svr7 ~]# cp /root/
.cfg /root/mulu1/

2)确认结果
[root@svr7 ~]# ls /root/mulu1/
anaconda-ks.cfg grub2 initial-setup-ks.cfg

步骤四:将文件 /etc/redhat-release 复制到 /root/ 下,同时改名为 ver.txt
1)复制文件
[root@svr7 ~]# ls /etc/redhat-release /root/ver.txt
ls: 无法访问/root/ver.txt: 没有那个文件或目录
/etc/redhat-release
[root@svr7 ~]# cp /etc/redhat-release /root/ver.txt

2)确认结果
[root@svr7 ~]# ls /root/ver.txt
/root/ver.txt

步骤五:将文件 /root/ver.txt 移动到mulu1目录下
1)移动文件
[root@svr7 ~]# ls /root/ver.txt /root/mulu1/ver.txt
ls: 无法访问/root/mulu1/ver.txt: 没有那个文件或目录
/root/ver.txt
[root@svr7 ~]# mv /root/ver.txt /root/mulu1/

2)确认结果
[root@svr7 ~]# ls /root/ver.txt /root/mulu1/ver.txt
ls: 无法访问/root/ver.txt: 没有那个文件或目录
/root/mulu1/ver.txt

步骤六:删除 mulu1 目录下的 grub2 子目录
1)删除子目录
[root@svr7 ~]# ls /root/mulu1/
anaconda-ks.cfg grub2 initial-setup-ks.cfg ver.txt
[root@svr7 ~]# rm -rf /root/mulu1/grub2/

2)确认结果
[root@svr7 ~]# ls /root/mulu1/
anaconda-ks.cfg initial-setup-ks.cfg ver.txt

3 案例3:使用vim新建/修改文件

3.1 问题

本例要求使用vim新建文件、修改文件,相关说明如下:
首先,使用vim编辑器在/bin/目录下新建文件hello,操作要求如下:

  1. 录入内容echo Hello World
  2. 保存后使用 cat 命令确认文件内容

然后,修改系统文件/etc/hosts,操作要求如下:

  1. 在末尾增加一行内容127.0.0.1 www.baidu.com
  2. 保存后使用 cat 命令确认文件内容

    3.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:使用vim创建新文件
    1)使用vim编辑器新建文件 /bin/hello
    [root@svr7 ~]# vim /bin/hello

2)按 i 键切换为输入模式,在第一行录入文本
echo Hello World

3)保存文件
完成录入后,先按Esc键回到命令模式,再输入 :wq 保存并退出编辑器。
:wq
[root@svr7 ~]#

4)使用 cat 命令确认文件内容
[root@svr7 ~]# cat /bin/hello
echo Hello World

步骤二:使用vim修改文件
1)修改文件 /etc/hosts
在末尾增加一行内容127.0.0.1 www.baidu.com。
[root@svr7 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.baidu.com

2)使用 cat 命令确认文件内容
[root@svr7 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.baidu.com

步骤三:启动 vimtutor 教学程序,练习常用编辑操作
1)启动vimtutor教学程序
[root@svr7 ~]# vimtutor
===============================================================================
= 欢 迎 阅 读 《 V I M 教 程 》 —— 版本 1.7 =
===============================================================================

  1. Vim 是一个具有很多命令的功能非常强大的编辑器。限于篇幅,在本教程当中<br /> 就不详细介绍了。本教程的设计目标是讲述一些必要的基本命令,而掌握好这<br /> 些命令,您就能够很容易地将 Vim 当作一个通用编辑器来使用了。
  2. 完成本教程的内容大约需要25-30分钟,取决于您训练的时间。
  3. 注意:<br /> 每一节的命令操作将会更改本文。推荐您复制本文的一个副本,然后在副本上<br /> 进行训练(如果您是通过"vimtutor"来启动教程的,那么本文就已经是副本了)。
  4. 切记一点:本教程的设计思路是在使用中进行学习的。也就是说,您需要通过<br /> 执行命令来学习它们本身的正确用法。如果您只是阅读而不操作,那么您可能<br /> 会很快遗忘这些命令的!<br />.. ..

2)命令模式常用操作
根据以下操作要点自行组织练习,具体过程略。
++ 光标移动

  • 上下左右:方向键
  • 行内跳转:Home键、End键
  • 全文翻页:PgUP键、PgDn键
  • 行间调整:gg、G

++ 复制/粘贴/删除

  • 复制:yy、5yy
  • 粘贴:p、P
  • 删除:Delete键、dd、5dd

++ 文本查找(关键词 vim 或 tutor)

  • 查找关键词:/tutor
  • 切换匹配结果:n、N

4 案例4:管理用户账号

4.1 问题

本例要求从命令行管理Linux用户账号,相关说明如下:

  1. 新建名为nvshen的用户账号,将密码设置为1234567,测试远程登录
  2. 彻底删除用户nvshen,检查其ID信息,检查其家目录是否可用

    4.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:新建名为 nvshen 的用户账号
    1)新建用户
    [root@svr7 ~]# id nvshen
    id: nvshen: no such user
    [root@svr7 ~]# useradd nvshen
    [root@svr7 ~]# id nvshen //确认添加结果
    uid=1001(nvshen) gid=1001(nvshen) 组=1001(nvshen)

2)为用户设置密码为 1234567
[root@svr7 ~]# passwd nvshen
更改用户 nvshen 的密码 。
新的 密码: //输入第一遍密码
无效的密码: 密码少于 8 个字符
重新输入新的 密码: //输入第二遍密码确认
passwd:所有的身份验证令牌已经成功更新。

3)测试以用户 nvshen 远程登录到系统
在Windows真机中打开putty.exe工具,连接到此Linux虚拟机,以账号nvshen可以登录成功。
步骤二:彻底删除名为 nvshen 的用户账号
1)删除用户
[root@svr7 ~]# userdel -r nvshen
[root@svr7 ~]#

2)检查已删除用户的ID信息,查看提示结果(已没有此用户)
[root@svr7 ~]# id nvshen
id: nvshen: no such user

3)检查其家目录是否可用
[root@svr7 ~]# ls /opt/nvshen
ls: 无法访问/opt/nvshen: 没有那个文件或目录

5 案例5:管理组账号

5.1 问题

本例要求从命令行管理Linux组账号,相关说明如下:

  1. 新建名为 stugrp 的组账号
  2. 确认用户 nvshen 可用(或重新添加)
  3. 将用户 nvshen 添加为 stugrp 组的成员,确认结果
  4. 将 stugrp 组账号删除,再次检查用户 nvshen 的组属性

    5.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:新建名为 stugrp 的组账号
    [root@svr7 ~]# groupadd stugrp

步骤二:确认用户 nvshen 可用(或重新添加)
1)添加新用户帐号
[root@svr7 ~]# id nvshen
id: nvshen: no such user
[root@svr7 ~]# useradd nvshen

2)确认用户账号的默认属性
[root@svr7 ~]# id nvshen
uid=1001(nvshen) gid=1002(nvshen) 组=1002(nvshen)

步骤三:将用户 nvshen 添加为 stugrp 组的成员,确认结果
1)添加组成员
[root@svr7 ~]# gpasswd -a nvshen stugrp
正在将用户“nvshen”加入到“stugrp”组中

2)确认成员用户的属性
[root@svr7 ~]# id nvshen
uid=1001(nvshen) gid=1002(nvshen) 组=1002(nvshen),1005(stugrp)

步骤四:将 stugrp 组账号删除,再次检查用户 nvshen 的组属性
1)删除组账号
[root@svr7 ~]# groupdel stugrp

2)检查原组成员用户的属性
[root@svr7 ~]# id nvshen
uid=1001(nvshen) gid=1002(nvshen) 组=1002(nvshen)

6 案例6:配置文档归属

6.1 问题

本例要求理解文档的归属关系,并通过归属变更了解其重要性,相关说明如下。
首先新建测试用户guojing、huangrong,属于taohuadao组。
然后完成下列归属测试操作:

  1. 检查用户guojing的家目录的归属
  2. 以用户huangrong登录,尝试进入guojing的家目录
  3. 把guojing的家目录的属主更改为huangrong
  4. 以用户huangrong登录,再次尝试进入guojing的家目录
  5. 以用户guojing登录,结果是什么?
  6. 重新将guojing的家目录的属主恢复为guojing

    6.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:准备测试账号
    1)新建用户guojing、huangrong
    添加账号:
    [root@svr7 ~]# useradd guojing
    [root@svr7 ~]# useradd huangrong

设置密码:
[root@svr7 ~]# echo 1234567 | passwd —stdin guojing
更改用户 guojing 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@svr7 ~]# echo 1234567 | passwd —stdin huangrong
更改用户 huangrong 的密码 。
passwd:所有的身份验证令牌已经成功更新。

2)将guojing、huangrong添加到taohuadao组
新建组账号taohuadao:
[root@svr7 ~]# groupadd taohuadao

添加组成员:
[root@svr7 ~]# gpasswd -a guojing taohuadao
正在将用户“guojing”加入到“taohuadao”组中
[root@svr7 ~]# gpasswd -a huangrong taohuadao
正在将用户“huangrong”加入到“taohuadao”组中

步骤二:更改归属测试
1)检查用户guojing的家目录的归属
[root@svr7 ~]# ls -ld ~guojing
drwx———. 3 guojing guojing 78 7月 23 12:03 /home/guojing

2)以用户huangrong登录,尝试进入guojing的家目录
[root@svr7 ~]# su - huangrong
[huangrong@svr7 ~]$ cd ~guojing
-bash: cd: /home/guojing: 权限不够 //进入失败
[huangrong@svr7 ~]$ exit
登出

3)把guojing的家目录的属主更改为huangrong
[root@svr7 ~]# chown huangrong ~guojing
[root@svr7 ~]# ls -ld ~guojing
drwx———. 3 huangrong guojing 78 7月 23 12:03 /home/guojing

4)以用户huangrong登录,再次尝试进入guojing的家目录
[root@svr7 ~]# su - huangrong
上一次登录:一 7月 23 17:38:27 CST 2018pts/1 上
[huangrong@svr7 ~]$ cd ~guojing //成功进入
[huangrong@svr7 guojing]$ exit
登出

5)以用户guojing登录,结果是什么?
[root@svr7 ~]# su - guojing
su: 警告:无法更改到 /home/guojing 目录: 权限不够 //有家难归
-bash: /home/guojing/.bash_profile: 权限不够
-bash-4.2$ exit
登出
-bash: /home/guojing/.bash_logout: 权限不够
[root@svr7 ~]#

6)重新将guojing的家目录的属主恢复为guojing
[root@svr7 ~]# chown guojing ~guojing
[root@svr7 ~]# ls -ld ~guojing
drwx———. 3 guojing guojing 78 7月 23 12:03 /home/guojing

7 案例7:配置访问权限

7.1 问题

本例要求理解目录的r、w、x权限,学会从命令行设置文档权限,相关说明如下:
首先,通过下列操作测试对目录的r、x权限:

  1. 修改 guojing 的家目录属性,使其他人有读取、可执行权限
  2. 以用户 huangrong 登录,尝试 ls -A、cd到 guojing 家目录

然后,通过下列操作测试对目录的w权限:

  1. 由管理员新建目录 /public ,并新建文件 /public/root.txt
  2. 将目录 /public 的属组改为 taohuadao,将组的权限设为rwx
  3. 对比目录 /public 和文件 /public/root.txt 的归属及权限
  4. 以用户 huangrong 登录,尝试查看/修改/删除 root.txt 文件

    7.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:测试对目录的r、x权限
    1)修改 guojing 的家目录属性,使其他人有读取、可执行权限
    [root@svr7 ~]# chmod o+rx ~guojing
    [root@svr7 ~]# ls -ld ~guojing
    drwx—-r-x. 3 guojing guojing 78 7月 23 12:03 /home/guojing

2)以用户 huangrong 登录,尝试 ls -A、cd到 guojing 家目录
[root@svr7 ~]# su - huangrong
上一次登录:一 7月 23 17:40:33 CST 2018pts/1 上
[huangrong@svr7 ~]$ ls -A ~guojing //成功列出资源
.bash_logout .bash_profile .bashrc .mozilla
[huangrong@svr7 ~]$ cd ~guojing //成功进入目录
[huangrong@svr7 guojing]$ exit
登出

步骤二:测试对目录的w权限
1)由管理员新建目录 /public ,并新建文件 /public/root.txt
[root@svr7 ~]# mkdir /public
[root@svr7 ~]# vim /public/root.txt
Top Secret.

2)将目录 /public 的属组改为 taohuadao,将组的权限设为rwx
[root@svr7 ~]# chown :taohuadao /public/
[root@svr7 ~]# chmod g=rwx /public/

3)对比目录 /public 和文件 /public/root.txt 的归属及权限
[root@svr7 ~]# ls -ld /public/ /public/root.txt
drwxrwxr-x. 2 root taohuadao 22 7月 23 17:49 /public/
-rw-r—r—. 1 root root 0 7月 23 17:49 /public/root.txt

4)以用户 huangrong 登录,尝试查看/修改/删除 root.txt 文件
[root@svr7 ~]# su - huangrong
上一次登录:一 7月 23 18:19:15 CST 2018pts/1 上

[huangrong@svr7 ~]$ cat /public/root.txt //可以查看
Top Secret.
[huangrong@svr7 ~]$ vim /public/root.txt
Top Secret.
.. ..
“/public/root.txt” [只读] 1L, 12C //无法修改

[huangrong@svr7 ~]$ rm -rf /public/root.txt
[huangrong@svr7 ~]$ ls /public/root.txt
ls: 无法访问/public/root.txt: 没有那个文件或目录 //可以删除
[huangrong@svr7 ~]$ exit
登出
[root@svr7 ~]#

8 案例8:设置文件的 x 权限

8.1 问题

本例要求完成如下两个小测试,进一步理解文件的x权限的重要性。
第一个小测试,验证x权限对程序文件的作用:

  1. 去掉/bin/hostname的x权限,执行hostname命令
  2. 重新为/bin/hostname添加x权限
  3. 再执行hostname,查看效果

第二个小测试,编写一个可执行的脚本文件:

  1. 新建文件/bin/hello,内容为“echo Hello World”
  2. 执行hello ,查看效果
  3. 为文件/bin/hello添加x权限,再次执行hello

    8.2 步骤

    实现此案例需要按照如下步骤进行。
    步骤一:验证x权限对程序文件的作用
    1)去掉/bin/hostname的x权限,执行hostname命令
    [root@svr7 ~]# chmod -x /bin/hostname //去掉x权限
    [root@svr7 ~]# ls -lh /bin/hostname //确认结果
    -rw-r—r—. 1 root root 16K 6月 10 2014 /bin/hostname
    [root@svr7 ~]# hostname //无法执行
    -bash: /usr/bin/hostname: 权限不够

2)重新为/bin/hostname添加x权限
[root@svr7 ~]# chmod +x /bin/hostname //添加x权限
[root@svr7 ~]# ls -lh /bin/hostname //确认结果
-rwxr-xr-x. 1 root root 16K 6月 10 2014 /bin/hostname

3)再执行hostname,查看效果
[root@svr7 ~]# hostname //成功执行
svr7.tedu.cn

步骤二:编写一个可执行的脚本文件
1)新建文件/bin/hello,内容为“echo Hello World”
[root@svr7 ~]# cat /bin/hello
echo Hello World

2)执行hello ,查看效果
[root@svr7 ~]# hello //无法执行
-bash: /usr/bin/hello: 权限不够

3)为文件/bin/hello添加x权限,再次执行hello
[root@svr7 ~]# chmod +x /bin/hello //可以执行
[root@svr7 ~]# hello
Hello World