RHCE 第二周作业

  • 1.创建一个目录 /data
  • 2. 创建 user1,user2,user3 三个用户,要求如下:
    • 1)user1 家目录在 /data 目录下 , 该用户的描述为 testuser;
    • 2)user2 用户的 uid 应当为 2000 ;
    • 3)user3 用户应该使用 /sbin/nologin 这个登陆 shell
  • 3. 创建 it 组 ,GID 为 3000
  • 4. 将以上三个用户加入到 it 组中
  • 5. 要求 it 组内的所有成员都可以在 /it 目录下创建文件和删除文件
  • 6. 给 it 组更名为 cloud
  • 7. 新建用户 ituser1,ituser2
  • 8. 新建一个文件 /tmp/rhcsa,并对该文件赋权。文件的拥有人必须是 ituser1,拥有组必须是 cloud,并且 ituser1 和 ituser2 是 cloud 组内的成员。要求组内成员对该文件拥有完全控制权限,拥有人拥有读写权限,其他人无任何权限(赋权后请测试)
  • 9. 在 /tmp 目录中创建 /tmp/redhat/rhel7 文件夹,创建时一并指定文件夹权限为764。
  • 10. 创建 /tmp/centos/technology 文件夹,要求递归设置文件拥有人为user1、文件拥有组为 user2。
  • 11. 创建 group1 组,指定组 ID 为 2100 , 创建 /tmp/group1 文件夹,要求设置 /tmp/group1 文件夹拥有组为 group1。
  • 12. 在user1 家目录中创建 /home/user1/user1 文件夹,同时递归指定文件夹权限为600。
  • 13. 在 /tmp/ 文件夹中创建 /tmp/demo 文件夹,并且复制 /home/user1/user1 文件夹权限。
  • 14. 将 /etc/passwd 按照 uid 的大小进行排序(降序),并将结果保存在 /root/passwd.bak 中。
  • 15. 查找 /etc/ 下所有文件内容包含 pass 字符串的文件,并显示字符串在哪个文件中的哪一行。
  • 16. 将 /etc/profile 文件中所有出现的单词进行统计词频,最后显示出每个单词出现的次数。
  • 17. 对系统中所有进程按照 cpu 使用百分比进行排序,要求从大到小,取出前10名的进程。(命令提示:ps axo %cpu ,%mem ,%comm)
  • grep 练习
    • 1、显示 /etc/passwd 文件中以 bash 结尾的行
    • 2、找出 /etc/passwd 文件中的三位或四位数
    • 3、找出 /etc/grub2.cfg 文件中,以至少一个空白字符开头,后面又跟了非空白字符的行(空白字符可以用 [:space:] 表示)。
    • 4、找出 "fdisk -l " 命令的结果中, 包含以 /dev/ 后跟 sd 或 hd 及一个字母的行
    • 5、找出 "ldd /usr/bin/cat" 命令的结果中文件路径
    • 6、找出 /proc/meminfo 文件中,所有以大写或小写 s 开头的行;至少用三种方法实现
      • 1)使用 grep 命令的 -i 选项
      • 2)使用正则表达式的 [ | ] 或者功能
      • 3) 使用正则表达式中 \b 选项
    • 7、过滤出当前系统上 root、admin 或 stu 用户的相关信息(/etc/passwd)
    • 8、echo 输出一个绝对路径,使用 egrep 取出其基名,例如 echo "/etc/httpd.conf",取出/etc/
    • 9、取出 ifconfig 中所有 ipv4 的地址
  • sed 练习
    • 1、 把 /etc/passwd 复制到 /root/test.txt , 用 sed 打印所有行
    • 2、打印 test.txt 的 3 到 10行
    • 3、 打印 test.txt 中包含 'root' 的行
    • 4、删除 test.txt 的 15 行以及以后的所有行
    • 5、删除 test.txt 中包含 'bash' 的行
    • 6、替换 test.txt 中 'root' 为 'toor' ,并打印出替换的行
    • 7、替换 test.txt 中 '/sbin/nologin' 为 '/bin/login',并打印出替换的行
    • 8、删除 test.txt 中 5 到 10 行中所有的数字
    • 9、删除 test.txt 中所有的特殊字符(除了数字以及大小写字母)
    • 10、在 test.txt 20 行前面加 'aaa'
    • 11、在 test.txt 文件中每一行行首增加 # 号
    • 12、将 test.txt 每行的第一个字符删除

1.创建一个目录 /data

[root@localhost ~]# mkdir /data                 -- 在根目录下创建目录 data
[root@localhost ~]# ls /                       -- 通过 ls 命令查看根目录下文档
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@localhost ~]#             -- 验证,data 目录创建成功

2. 创建 user1,user2,user3 三个用户,要求如下:

1)user1 家目录在 /data 目录下 , 该用户的描述为 testuser;

[root@localhost ~]# id user1      -- 创建用户前确认系统中没有 user1 用户
id: ‘user1’: no such user
[root@localhost ~]# useradd -c test -d /data/user1 user1    -- 创建用户 user1 ,指定描述信息为testuser ,用户家目录为 /data 下;
[root@localhost ~]# id user1         -- 验证,用户 user1 创建成功;
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost ~]# su - user1       -- 切换至用户 user1;
[user1@localhost ~]$ pwd        -- 查看用户家目录为 /data/user1
/data/user1
[user1@localhost ~]$ exit       -- 退出 user1 用户登陆
logout
[root@localhost ~]# passwd user1    -- 为新用户 user1 配置密码,以便可在图形登陆界面看到用户描述信息
Changing password for user user1.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.    -- 用户密码配置成功
[root@localhost ~]#


查看可看到新增用户 user1 的登陆描述信息为 test,验证操作成功。

2)user2 用户的 uid 应当为 2000 ;

[root@localhost ~]# id user2    -- 创建用户前确认系统无 user2 用户;
id: ‘user2’: no such user
[root@localhost ~]# useradd -u 2000 user2   -- 创建用户 user2 并指定用户 uid 为 2000;
[root@localhost ~]# id user2       -- 验证新增用户
uid=2000(user2) gid=2000(user2) groups=2000(user2)   -- 新用户 user2 创建成功且 uid 为 2000;
[root@localhost ~]#

3)user3 用户应该使用 /sbin/nologin 这个登陆 shell

[root@localhost ~]# id user3        --  创建用户前确认系统无 user3 用户;
id: ‘user3’: no such user
[root@localhost ~]# useradd -s /sbin/nologin user3   -- 创建用户 user3 并指定 shell 为 /sbin/nologin;
[root@localhost ~]# id user3          -- 验证创建用户 user3 成功
uid=2001(user3) gid=2001(user3) groups=2001(user3)
[root@localhost ~]# cat /etc/passwd | grep user3   -- 查看 /etc/passwd 文件中 user3 用户信息
user3:x:2001:2001::/home/user3:/sbin/nologin   -- 验证 user3 用户 shell 为 /sbin/nologin
[root@localhost ~]#

3. 创建 it 组 ,GID 为 3000

[root@localhost ~]# cat /etc/group | grep -w it   -- 创建前确认系统中无 it 组;
[root@localhost ~]# groupadd -g 3000 it           -- 创建 it 组并指定 GID 为 3000;
[root@localhost ~]# cat /etc/group | grep -w it   -- 再次查看
it:x:3000:                                        -- it 组创建成功,且 GID 为 3000;
[root@localhost ~]#

4. 将以上三个用户加入到 it 组中

[root@localhost ~]# usermod -aG it user1     -- 将用户 user1 添加至 it 组中;
[root@localhost ~]# usermod -aG it user2     -- 将用户 user2 添加至 it 组中;
[root@localhost ~]# usermod -aG it user3     -- 将用户 user3 添加至 it 组中;
[root@localhost ~]# cat /etc/group | grep -w it   -- 查看组配置文件
it:x:3000:user1,user2,user3                     -- 验证成功,it 组中增加 user1 , user2 ,user3;
[root@localhost ~]#

5. 要求 it 组内的所有成员都可以在 /it 目录下创建文件和删除文件

[root@localhost ~]# ls -l / |grep -w it    -- 查看根目录下是否已存在 it 目录;
[root@localhost ~]# mkdir /it            -- 在根目录下创建 it 目录;
[root@localhost ~]# ls -l / |grep -w it    -- 查看目录 it 创建成功;
drwxr-xr-x.   2 root root    6 Oct 18 16:26 it
[root@localhost ~]# chown :it /it          -- 修改 it 目录的所属组为 it 组;
[root@localhost ~]# ls -l / |grep -w it    -- 验证修改成功;
drwxr-xr-x.   2 root it      6 Oct 18 16:26 it
[root@localhost ~]# chmod g+w /it          -- 修改 it 目录的所属组权限,增加 w 修改权限;
[root@localhost ~]# ls -l / |grep -w it    -- 验证权限修改成功;
drwxrwxr-x.   2 root it      6 Oct 18 16:26 it
[root@localhost ~]# su - user1          -- 切换至 it 组中的用户 user1;
[user1@localhost ~]$ mkdir /it/test     -- 测试通过 user1 用户创建 test 目录
[user1@localhost ~]$ ls -l /it/         -- 查看目录创建结果;
total 0
drwxrwxr-x. 2 user1 user1 6 Oct 18 16:27 test  -- test 目录创建成功;
[user1@localhost ~]$ rm -rf /it/test    -- 测试删除 test 目录;
[user1@localhost ~]$ ls -l /it/         -- 查看目录删除结果;
total 0                                 -- test 目录删除成功
[user1@localhost ~]$

6. 给 it 组更名为 cloud

[root@localhost ~]# cat /etc/group | grep -wE "it|cloud"    -- 查看组配置文件;
it:x:3000:user1,user2,user3                             -- 有 it 组,无 cloud 组;
[root@localhost ~]# groupmod -n cloud it                -- 将 it 组更名为 cloud;
[root@localhost ~]# cat /etc/group | grep -wE "it|cloud"   -- 查看组配置文件;
cloud:x:3000:user1,user2,user3                          -- 更名修改成功;
[root@localhost ~]#

7. 新建用户 ituser1,ituser2

[root@localhost ~]# id ituser1    -- 查看 itsuer1 是否已存在;
id: ‘ituser1’: no such user
[root@localhost ~]# id ituser2    -- 查看 ituser2 是否已存在;
id: ‘ituser2’: no such user
[root@localhost ~]# useradd ituser1      -- 新建用户 ituser1;
[root@localhost ~]# id ituser1           -- 查询 ituser1 创建成功;
uid=2003(ituser1) gid=2003(ituser1) groups=2003(ituser1)
[root@localhost ~]# useradd ituser2      -- 新建用户 ituser2
[root@localhost ~]# id ituser2           -- 查询 ituser2 创建成功
uid=2004(ituser2) gid=2004(ituser2) groups=2004(ituser2)
[root@localhost ~]#

8. 新建一个文件 /tmp/rhcsa,并对该文件赋权。文件的拥有人必须是 ituser1,拥有组必须是 cloud,并且 ituser1 和 ituser2 是 cloud 组内的成员。要求组内成员对该文件拥有完全控制权限,拥有人拥有读写权限,其他人无任何权限(赋权后请测试)

[root@localhost ~]# touch /tmp/rhcsa             -- 在 tmp 目录下创建 rhcsa 文件;
[root@localhost ~]# chown ituser1:cloud /tmp/rhcsa   -- 修改 rhcsa 文件属主和属组;
[root@localhost ~]# ls -l /tmp/rhcsa             -- 查看确认修改成功;
-rw-r--r--. 1 ituser1 cloud 0 Oct 19 16:28 /tmp/rhcsa
[root@localhost ~]# usermod -aG cloud ituser1         -- 将用户 ituser1 加入 cloud 组;
[root@localhost ~]# usermod -aG cloud ituser2         -- 将用户 ituser2 加入 cloud 组;
[root@localhost ~]# chmod u=rw,g=rwx,o= /tmp/rhcsa      --  修改 rhcsa 文件权限
[root@localhost ~]# ls -l /tmp/rhcsa             -- 查看确认权限修改成功
-rw-rwx---. 1 ituser1 cloud 0 Oct 19 16:28 /tmp/rhcsa
[root@localhost ~]# su - ituser1           --  切换至文件属主 ituser1 用户测试读取和修改文件
[ituser1@localhost ~]$ ls -l /tmp/rhcsa
-rw-rwx---. 1 ituser1 cloud 0 Oct 19 16:28 /tmp/rhcsa
[ituser1@localhost ~]$ echo hello > /tmp/rhcsa
[ituser1@localhost ~]$ cat /tmp/rhcsa
hello
[ituser1@localhost ~]$ exit
logout
[root@localhost ~]# su - ituser2         -- 切换至文件属组 ituser2 用户测试读取和修改文件
[ituser2@localhost ~]$ echo hello > /tmp/rhcsa
[ituser2@localhost ~]$ echo hello >> /tmp/rhcsa
[ituser2@localhost ~]$ cat /tmp/rhcsa
hello
hello
[ituser2@localhost ~]$ exit
logout
[root@localhost ~]# useradd ituser3        -- 新建 ituser3 用户,测试其他用户对 rhcsa文件的权限
[root@localhost ~]# su - ituser3
[ituser3@localhost ~]$ echo hellp >> /tmp/rhcsa    -- 编辑操作权限不足
-bash: /tmp/rhcsa: Permission denied
[ituser3@localhost ~]$ cat /tmp/rhcsa              -- 读取操作权限不足
cat: /tmp/rhcsa: Permission denied
[ituser3@localhost ~]$ exit
logout
[root@localhost ~]#

9. 在 /tmp 目录中创建 /tmp/redhat/rhel7 文件夹,创建时一并指定文件夹权限为764。

[root@192 ~]# mkdir -pm 764 /tmp/redhat/rhel7     -- 创建 rhel7 文件夹,并指定权限为764,同时直接创建父目录;
[root@192 ~]# ls -l /tmp/redhat/           -- 查看目录信息
total 0
drwxrw-r--. 2 root root 6 Oct 19 22:51 rhel7     -- 文件夹创建成功且权限为 764;
[root@192 ~]#

10. 创建 /tmp/centos/technology 文件夹,要求递归设置文件拥有人为user1、文件拥有组为 user2。

[root@192 ~]# mkdir -p /tmp/centos/technology     -- 在 /tmp 下递归创建 centos 和 technology 文件夹;
[root@192 ~]# chown -R user1:user2 /tmp/centos/   -- 递归设置文件夹属主为 user1 、属组为 user2;
[root@192 ~]# ls -l /tmp |grep -w centos         -- 查看 centos 文件夹属主和属组;
drwxr-xr-x. 3 user1 user2     24 Oct 19 22:57 centos
[root@192 ~]# ls -l /tmp/centos/                 -- 查看 technology 文件夹属主和属组;
total 0
drwxr-xr-x. 2 user1 user2 6 Oct 19 22:57 technology
[root@192 ~]#

11. 创建 group1 组,指定组 ID 为 2100 , 创建 /tmp/group1 文件夹,要求设置 /tmp/group1 文件夹拥有组为 group1。

[root@192 ~]# groupadd -g 2100 group1     -- 创建 group1 组,并指定 gid 为 2100;
[root@192 ~]# cat /etc/group | grep -w group1     --  查看创建成功;
group1:x:2100:
[root@192 ~]# mkdir /tmp/group1           -- 在 /tmp 目录下创建 group1 文件夹;
[root@192 ~]# chown :group1 /tmp/group1        -- 修改 group1 文件夹属组为 group1;
[root@192 ~]# ls -l /tmp |grep -w group1       -- 查看并验证 group1 文件夹属组修改成功;
drwxr-xr-x. 2 root  group1      6 Oct 19 23:06 group1
[root@192 ~]#

12. 在user1 家目录中创建 /home/user1/user1 文件夹,同时递归指定文件夹权限为600。

[root@192 ~]# mkdir /home/user1/user1   -- 在 user1 家目录下创建 user1 文件夹;
[root@192 ~]# chmod -R 600 /home/user1    -- 递归指定 user1 文件夹权限为 600;
[root@192 ~]# ls -l /home/ |grep -w user1 |grep -v user2  -- 验证查看权限设置
drw-------.  4 user1 user1   91 Oct 19 23:10 user1
[root@192 ~]# ls -l /home/user1/
total 0
drw-------. 2 root root 6 Oct 19 23:10 user1   -- 验证递归权限设置

13. 在 /tmp/ 文件夹中创建 /tmp/demo 文件夹,并且复制 /home/user1/user1 文件夹权限。

[root@192 ~]# mkdir /tmp/demo         -- 在 /tmp 目录新建 demo 文件夹;
[root@192 ~]# chmod --reference=/home/user1/user1 /tmp/demo   -- 将 /home/user1/user1 文件夹权限复制到 demo 文件夹;
[root@192 ~]# ls -l /tmp |grep -w demo   -- 验证权限复制
drw-------. 2 root  root        6 Oct 19 23:18 demo   -- 验证成功,文件夹权限为 600;
[root@192 ~]#

14. 将 /etc/passwd 按照 uid 的大小进行排序(降序),并将结果保存在 /root/passwd.bak 中。

[root@192 ~]# cat /etc/passwd | sort -t : -k 3 -rn > /root/passwd.bak   -- 将 /etc/passwd 以 : 为分隔符,对第三列 UID 值进行降序排列,将结果输出至 /root/passwd.bak 中;
[root@192 ~]# cat /root/passwd.bak | tail    -- 验证查看已按照 UID 排序后写入 /root/passwd.bak 文件中;
operator:x:11:0:operator:/root:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@192 ~]#

15. 查找 /etc/ 下所有文件内容包含 pass 字符串的文件,并显示字符串在哪个文件中的哪一行。

[root@192 ~]# grep -rn pass /etc/ | head       -- 使用 grep -c 查找 /etc 下所有文件内容包含 pass 字符串的文件,并通过 -n 选项显示在文件中的行号,为便于查看只显示了前 10 行;
/etc/libreport/events.d/bugzilla_anaconda_event.conf:6:         # filing a less usable bug is surely better than publishing passwords
/etc/libreport/forbidden_words.conf:24:pass
/etc/libreport/forbidden_words.conf:27:password
/etc/libreport/ignored_words.conf:16:gnome-ssh-askpass
/etc/libreport/ignored_words.conf:47:ksshaskpass
/etc/libreport/ignored_words.conf:48:irqbypass
/etc/libreport/ignored_words.conf:55:lxqt-openssh-askpass
/etc/libreport/ignored_words.conf:59:passed
/etc/libreport/report_event.conf:40:# All stdout and stderr output is captured and passed to abrt
/etc/rhsm/rhsm.conf:34:# password for basic http proxy auth, if needed
[root@192 ~]#

16. 将 /etc/profile 文件中所有出现的单词进行统计词频,最后显示出每个单词出现的次数。

[root@localhost ~]# egrep -o '\<[[:alpha:]]+\>' /etc/profile | sort | uniq -c | head2 a3 after1 aliases2 and1 are1 as2 Bash5 bashrc1 better7 bin
[root@localhost ~]# 

17. 对系统中所有进程按照 cpu 使用百分比进行排序,要求从大到小,取出前10名的进程。(命令提示:ps axo %cpu ,%mem ,%comm)

[root@192 ~]# ps -eo %cpu,%mem,command |tail -n +2 | sort -rn |head0.9  0.9 /usr/libexec/packagekitd0.0  4.0 /usr/bin/gnome-shell0.0  1.5 /usr/libexec/ibus-x11 --kill-daemon0.0  1.5 /usr/bin/Xwayland :1024 -rootless -terminate -accessx -core -listen 4 -listen 5 -displayfd 60.0  1.1 /usr/sbin/libvirtd0.0  1.0 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid0.0  0.9 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files0.0  0.8 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P0.0  0.7 /usr/lib/polkit-1/polkitd --no-debug0.0  0.7 /usr/libexec/gsd-media-keys
[root@192 ~]#

grep 练习

1、显示 /etc/passwd 文件中以 bash 结尾的行

[root@localhost ~]# cat /etc/passwd | grep 'bash$'
root:x:0:0:root:/root:/bin/bash
admin:x:1000:1000:admin:/home/admin:/bin/bash
user2:x:2000:2000::/home/user2:/bin/bash
user1:x:2002:2002::/home/user1:/bin/bash
ituser1:x:2003:2003::/home/ituser1:/bin/bash
ituser2:x:2004:2004::/home/ituser2:/bin/bash
ituser3:x:2005:2005::/home/ituser3:/bin/bash
[root@localhost ~]#

2、找出 /etc/passwd 文件中的三位或四位数

[root@localhost ~]# cat /etc/passwd | egrep '[[:digit:]]{3,4}' | head
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
libstoragemgmt:x:996:992:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
[root@localhost ~]#

3、找出 /etc/grub2.cfg 文件中,以至少一个空白字符开头,后面又跟了非空白字符的行(空白字符可以用 [:space:] 表示)。

[root@localhost ~]# cat /etc/grub2.cfg | egrep '^[[:space:]]+.*' | headload_env -f ${config_directory}/grubenvload_envset default="${next_entry}"set next_entry=save_env next_entryset boot_once=trueset default="${saved_entry}"menuentry_id_option="--id"menuentry_id_option=""set saved_entry="${prev_saved_entry}"
[root@localhost ~]#

4、找出 "fdisk -l " 命令的结果中, 包含以 /dev/ 后跟 sd 或 hd 及一个字母的行

[root@localhost ~]# fdisk -l |grep 'dev[sd|hd][a-zA-Z]'
[root@localhost ~]#

5、找出 “ldd /usr/bin/cat” 命令的结果中文件路径

[root@localhost ~]# ldd /usr/bin/cat | grep  '\/.*'libc.so.6 => /lib64/libc.so.6 (0x00007f51d0fd3000)/lib64/ld-linux-x86-64.so.2 (0x00007f51d159f000)
[root@localhost ~]#

6、找出 /proc/meminfo 文件中,所有以大写或小写 s 开头的行;至少用三种方法实现

1)使用 grep 命令的 -i 选项

[root@localhost ~]# cat /proc/meminfo | grep -i ^s
SwapCached:            0 kB
SwapTotal:       4141052 kB
SwapFree:        4141052 kB
Shmem:             27792 kB
Slab:             263496 kB
SReclaimable:     104220 kB
SUnreclaim:       159276 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
[root@localhost ~]#

2)使用正则表达式的 [ | ] 或者功能

[root@localhost ~]# cat /proc/meminfo | grep '^[s|S]'
SwapCached:            0 kB
SwapTotal:       4141052 kB
SwapFree:        4141052 kB
Shmem:             27792 kB
Slab:             263496 kB
SReclaimable:     104220 kB
SUnreclaim:       159276 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
[root@localhost ~]#

3) 使用正则表达式中 \b 选项

[root@localhost ~]# cat /proc/meminfo | grep '\b[s|S]'
SwapCached:            0 kB
SwapTotal:       4141052 kB
SwapFree:        4141052 kB
Shmem:             27796 kB
Slab:             264392 kB
SReclaimable:     104580 kB
SUnreclaim:       159812 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
[root@localhost ~]#

7、过滤出当前系统上 root、admin 或 stu 用户的相关信息(/etc/passwd)

[root@localhost ~]# cat /etc/passwd | egrep ^'root|admin|stu'
root:x:0:0:root:/root:/bin/bash
admin:x:1000:1000:admin:/home/admin:/bin/bash
[root@localhost ~]#

8、echo 输出一个绝对路径,使用 egrep 取出其基名,例如 echo “/etc/httpd.conf”,取出/etc/

[root@localhost ~]# echo /home/user1 | egrep ^'\/.+\/'
/home/user1
[root@localhost ~]#

9、取出 ifconfig 中所有 ipv4 的地址

[root@localhost ~]# ifconfig | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
192.168.91.132
255.255.255.0
192.168.91.255
127.0.0.1
255.0.0.0
[root@localhost ~]#

sed 练习

1、 把 /etc/passwd 复制到 /root/test.txt , 用 sed 打印所有行

[root@localhost ~]# cp /etc/passwd /root/test.txt && sed p /root/test.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

2、打印 test.txt 的 3 到 10行

[root@localhost ~]# cat /root/test.txt | sed -n '3,10p'
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

3、 打印 test.txt 中包含 ‘root’ 的行

[root@localhost ~]# cat /root/test.txt | sed -n '/root/p'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

4、删除 test.txt 的 15 行以及以后的所有行

[root@localhost ~]# nl /root/test.txt | sed '15,$d'1  root:x:0:0:root:/root:/bin/bash2    bin:x:1:1:bin:/bin:/sbin/nologin3   daemon:x:2:2:daemon:/sbin:/sbin/nologin4    adm:x:3:4:adm:/var/adm:/sbin/nologin5   lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6   sync:x:5:0:sync:/sbin:/bin/sync7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8   halt:x:7:0:halt:/sbin:/sbin/halt9   mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10    operator:x:11:0:operator:/root:/sbin/nologin11  games:x:12:100:games:/usr/games:/sbin/nologin12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin13   nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin14 dbus:x:81:81:System message bus:/:/sbin/nologin
[root@localhost ~]#

5、删除 test.txt 中包含 ‘bash’ 的行

[root@localhost ~]# cat /root/test.txt | sed '/bash/d' | grep bash
[root@localhost ~]#

6、替换 test.txt 中 ‘root’ 为 ‘toor’ ,并打印出替换的行

[root@localhost ~]# cat /root/test.txt | sed 's/root/toor/g' | sed -n '/toor/p'
toor:x:0:0:toor:/toor:/bin/bash
operator:x:11:0:operator:/toor:/sbin/nologin
[root@localhost ~]#

7、替换 test.txt 中 ‘/sbin/nologin’ 为 ‘/bin/login’,并打印出替换的行

[root@localhost ~]# cat /root/test.txt | sed 's/\/sbin\/nologin/\/bin\/login/g' | sed -n '/\/bin\/login/p'
bin:x:1:1:bin:/bin:/bin/login
daemon:x:2:2:daemon:/sbin:/bin/login
adm:x:3:4:adm:/var/adm:/bin/login
lp:x:4:7:lp:/var/spool/lpd:/bin/login
mail:x:8:12:mail:/var/spool/mail:/bin/login
operator:x:11:0:operator:/root:/bin/login
games:x:12:100:games:/usr/games:/bin/login
ftp:x:14:50:FTP User:/var/ftp:/bin/login
nobody:x:65534:65534:Kernel Overflow User:/:/bin/login
dbus:x:81:81:System message bus:/:/bin/login
systemd-coredump:x:999:997:systemd Core Dumper:/:/bin/login
systemd-resolve:x:193:193:systemd Resolver:/:/bin/login

8、删除 test.txt 中 5 到 10 行中所有的数字

[root@localhost ~]# nl /root/test.txt |sed '5,10 s/[0-9]/ /g'1    root:x:0:0:root:/root:/bin/bash2    bin:x:1:1:bin:/bin:/sbin/nologin3   daemon:x:2:2:daemon:/sbin:/sbin/nologin4    adm:x:3:4:adm:/var/adm:/sbin/nologinlp:x: : :lp:/var/spool/lpd:/sbin/nologinsync:x: : :sync:/sbin:/bin/syncshutdown:x: : :shutdown:/sbin:/sbin/shutdownhalt:x: : :halt:/sbin:/sbin/haltmail:x: :  :mail:/var/spool/mail:/sbin/nologinoperator:x:  : :operator:/root:/sbin/nologin11 games:x:12:100:games:/usr/games:/sbin/nologin12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin13   nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin14 dbus:x:81:81:System message bus:/:/sbin/nologin

9、删除 test.txt 中所有的特殊字符(除了数字以及大小写字母)

[root@localhost ~]# cat /root/test.txt | sed 's/[^a-zA-Z0-9]/ /g'
root x 0 0 root  root  bin bash
bin x 1 1 bin  bin  sbin nologin
daemon x 2 2 daemon  sbin  sbin nologin
adm x 3 4 adm  var adm  sbin nologin
lp x 4 7 lp  var spool lpd  sbin nologin
sync x 5 0 sync  sbin  bin sync
shutdown x 6 0 shutdown  sbin  sbin shutdown

10、在 test.txt 20 行前面加 ‘aaa’

[root@localhost ~]# nl  /root/test.txt | sed '20i aaa'1   root:x:0:0:root:/root:/bin/bash2    bin:x:1:1:bin:/bin:/sbin/nologin3   daemon:x:2:2:daemon:/sbin:/sbin/nologin4    adm:x:3:4:adm:/var/adm:/sbin/nologin5   lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6   sync:x:5:0:sync:/sbin:/bin/sync7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8   halt:x:7:0:halt:/sbin:/sbin/halt9   mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10    operator:x:11:0:operator:/root:/sbin/nologin11  games:x:12:100:games:/usr/games:/sbin/nologin12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin13   nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin14 dbus:x:81:81:System message bus:/:/sbin/nologin15   systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin16    systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin17    tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin18   polkitd:x:998:996:User for polkitd:/:/sbin/nologin19    geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
aaa20   rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin21   pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

11、在 test.txt 文件中每一行行首增加 # 号

[root@localhost ~]# cat /root/test.txt | sed 's/^/\#/g'
#root:x:0:0:root:/root:/bin/bash
#bin:x:1:1:bin:/bin:/sbin/nologin
#daemon:x:2:2:daemon:/sbin:/sbin/nologin
#adm:x:3:4:adm:/var/adm:/sbin/nologin
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#sync:x:5:0:sync:/sbin:/bin/sync
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt

12、将 test.txt 每行的第一个字符删除

[root@localhost ~]# cat /root/test.txt | sed 's/^./ /g'oot:x:0:0:root:/root:/bin/bashin:x:1:1:bin:/bin:/sbin/nologinaemon:x:2:2:daemon:/sbin:/sbin/nologindm:x:3:4:adm:/var/adm:/sbin/nologinp:x:4:7:lp:/var/spool/lpd:/sbin/nologinync:x:5:0:sync:/sbin:/bin/synchutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

RHCE 第二周作业相关推荐

  1. 2019年春季学期第二周作业(文件指针)

    2019年春季学期第二周作业(基础作业) 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始).并将最大值和对应的最小下标数值写入文件. 输入: 请建立以自 ...

  2. 软件工程 第二周作业

    ##软件工程第二周作业 提出问题 1. 一般来说,想要自己的程序跑得又快又好,就要减少函数的反复调用,但有所得则必有所失,效能提高就有可能伴随着程序的稳定性的降低,这两者应该如何权衡呢? 2. 关于5 ...

  3. 2017-2018-1 20179215《Linux内核原理与分析》第二周作业

    20179215<Linux内核原理与分析>第二周作业 这一周主要了解了计算机是如何工作的,包括现在存储程序计算机的工作模型.X86汇编指令包括几种内存地址的寻址方式和push.pop.c ...

  4. 学习linux第二周作业

    第二周作业: 本周作业内容: 1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. touch,rm,mv,cp,file,ls,chmod,chown,ln,rename, ...

  5. 序列模型第二周作业1:Operations on word vectors

    来自吴恩达深度学习系列视频:序列模型第二周作业1:Operations on word vectors.如果英文对你来说有困难,可以参照:[中文][吴恩达课后编程作业]Course 5 - 序列模型 ...

  6. 20189221 2018-2019-2 《密码与安全新技术专题》第二周作业

    20189221 2018-2019-2 <密码与安全新技术专题>第二周作业 课程:<密码与安全新技术专题> 班级: 201892 姓名: 郭开世 学号:20189221 上课 ...

  7. 「数据结构」普林斯顿算法课第二周作业

    「数据结构」普林斯顿算法课第二周作业 Algorithm I, Princeton 编程作业: Deques and Randomized Queues 思路 Deque.java Randomize ...

  8. 20189200余超 2018-2019-2 移动平台应用开发实践第二周作业

    2018-2019-2 移动平台应用开发实践第二周作业 实验部分,在linux中用git命令将代码传到码云上 成功上传的截图 码云上的截图 遇到如下的问题 解决方法 我发现在进行第一步git clon ...

  9. 实时控制软件第二周作业 停车场门禁控制系统的状态机设计

    实时控制软件第二周作业 停车场门禁控制系统的状态机设计 信号: 灯的信号(红绿): 起落杆的信号: 传感器的信号(外部输入): 状态机信号: 车辆进入 车辆出去 #include <iostre ...

最新文章

  1. druid sql黑名单 报异常 sql injection violation, part alway true condition not allow
  2. python爬虫——利用BeautifulSoup4爬取糗事百科的段子
  3. 网易云易盾与A10 Networks达成战略合作 携手打造抗DDoS攻击的解决方案
  4. php如何做浏览量,php+ajax实现的点击浏览量加1
  5. 怎么去除idea中代码的波浪线(黄色警告线)
  6. win server 缓冲区队列不足_有赞延迟队列设计
  7. Leetcode每日一题:41.first-missing-positive(缺失的第一个正数)
  8. 黄聪:C#获取网页HTML内容的三种方式
  9. 干货!flask登录注册token验证接口开发详解
  10. Eclipse自动生成作者、日期注释等功能设置
  11. JAVA与C当中基本数据类型和基本运算符的区别
  12. 网站静态页面克隆 | 学习笔记
  13. 无纸化会议系统服务器是什么意思,无纸化会议系统是什么?
  14. 渗透测试信息收集笔记(信息搜集、后台查找)
  15. Spring基于XMLMysql | 注解Mysql的简单IOC案例
  16. 盘点机器人四大家族——KUKA机器人
  17. 保险行业CRM客户关系管理系统解决方案
  18. 超详细版:Python 这样安装如此简单(Windows)
  19. 发送文件的过程计算机,用电脑给别人传文件的方法步骤图
  20. 如何让子元素居于父元素底部

热门文章

  1. setResulttransformer过期NativeQueryImpl,cannot be cast to org.hibernate.query.internal.NativeQueryImpl
  2. 构建MFS+Keepalived双机高可用热备方案`
  3. Glide使用详解(一)
  4. 一、采样频率到底是选择2倍还是10倍?让我用python来给你展示
  5. 一个强悍的web性能测试工具--WebPageTest
  6. 精雕细琢见真章《STM32Cube高效开发教程》
  7. Bzoj3441 乌鸦喝水
  8. Linux下网络流量实时监控
  9. CA6140车床拨叉工艺及铣30X80面夹具设计
  10. linux dumpe2fs命令