Linux基础知识(四)-正则表达式grep,egrep

1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

[root@localhost ~]# cp -r /etc/skel /home/tuser1
drwxr-xr-x. 2 root root 59 Oct  8 17:53 /home/tuser1/
[root@localhost ~]# chmod -R 700 /home/tuser1
[root@localhost ~]# ll -d /home/tuser1/
drwx------. 2 root root 59 Oct  8 17:53 /home/tuser1/
[root@localhost ~]# chmod  go=  /home/tuser1
[root@localhost ~]# ll -d /home/tuser1/
drwx------. 2 root root 59 Oct  8 17:53 /home/tuser1/

2、编辑/etc/group文件,添加组hadoop。

可以使用vim进行编辑,添加一行 hadoop:x:3005:
或者使用>>追加的方式

[root@localhost ~]# echo "hadoop:x:3005:" >> /etc/group
[root@localhost ~]# tail -1 /etc/group
hadoop:x:3005:

3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。

可以使用vim编辑器编辑:

或者使用>>追加

[root@localhost ~]# echo "hadoop:x:3005:3005::/home/hadoop:/bin/bash" >> /etc/passwd
[root@localhost ~]# tail -1 /etc/passwd
hadoop:x:3005:3005::/home/hadoop:/bin/bash

4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# ls -a /home/hadoop
.  ..  .bash_logout  .bash_profile  .bashrc
[root@localhost ~]# chmod -R 700 /home/hadoop

5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。

[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop
drwx------. 2 hadoop hadoop 59 Oct  8 18:22 /home/hadoop

从题3到题5的过程结束后,在给hadoop账户一个密码,hadoop账户
就可以登录了,是一个手动添加账户的过程

[root@localhost ~]# echo "111111" | passwd --stdin hadoop

6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

[root@localhost ~]# grep -E "^[s|S]" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7016 kB
Slab:              70112 kB
SReclaimable:      27504 kB
SUnreclaim:        42608 kB
###或者###
[root@localhost ~]# grep -E "^[s|S]" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7016 kB
Slab:              70112 kB
SReclaimable:      27504 kB
SUnreclaim:        42608 kB

7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

[root@localhost ~]# grep -v  "/sbin/nologin" /etc/passwd
##只显示用户名##
[root@localhost ~]# grep -v  "/sbin/nologin" /etc/passwd | cut -d: -f1

8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;

[root@localhost ~]# grep  "/bin/bash" /etc/passwd
[root@localhost ~]# grep  "/bin/bash" /etc/passwd | cut -d: -f1

9、找出/etc/passwd文件中的一位数或两位数;

[root@localhost ~]# grep -Eo "\<[0-9]{1,2}\>" /etc/passwd

10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

[root@localhost ~]# grep -E "^[[:space:]]+" /boot/grub/grub.conf
 ##或者##
[root@localhost ~]# grep -E "^[[:space:]]{1,}" /boot/grub/grub.conf

11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

[root@localhost ~]# grep -E "^#[[:space:]]+[^[:space:]]+" /etc/rc.d/rc.sysinit

12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

[root@localhost ~]# netstat -tan | grep -E "LISTEN[[:space:]]*$"

13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s /sbin/nologin nologin
[root@localhost ~]# egrep "^([^:]+).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nobody:x:99:99:Nobody:/:/sbin/nologin
hadoop:x:3005:3005::/home/hadoop:/bin/bash
bash:x:3006:3006::/home/bash:/bin/bash
basher:x:3008:3008::/home/basher:/bin/bash
nologin:x:3009:3009::/home/nologin:/sbin/nologin

原创文章,作者:luoluo,如若转载,请注明出处:http://www.178linux.com/52086

(0)
luoluoluoluo
上一篇 2016-10-17
下一篇 2016-10-17

相关推荐

  • iptables详解

    iptables命令: iptables [-t table] {-A|-C|-D} chain rule-specification iptables [-t table] -I chain [rulenum] rule-specification iptables [-t table] -R chain rulenum rule-specificatio…

    Linux干货 2017-11-12
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@centos7 ~]# who|cut -d' ' -f1|sort -u fedora gentoo root 2、取出最后登录到当前系统的用户的相关信息。 [root@ce…

    Linux干货 2017-01-16
  • RAID and LVM

    RAID          Redundant Arrays of  inexpensive(Independent)Disks,RAID:容错式廉价磁盘阵列,容错式独立磁盘阵列,简称RAID,由加利福尼亚大学伯克利分校(University of California-Berkeley)在1988…

    Linux干货 2016-09-02
  • vi(vim)编辑器的使用

    vim编辑器:全屏编辑器,模式编辑器 vim模式:     编辑模式:     输入模式:     末行模式:     转换模式:      &nbs…

    Linux干货 2016-08-15
  • 位置变量&特殊变量总结

    位置变量 常用的位置变量有 $1, $2, $3 ……,表示命令行传给脚本的第一个参数,第二个参数,第三个参数。。。 $0 表示脚本的文件名,比如a.sh 位置变量在脚本中的主要作用,是让脚本通过他们来获取命令行传递给脚本的参数。 变量位置调整 shift [n] 用于调整变量位置 第n+1个位置变量会被重新命名为$1…

    Linux干货 2016-08-15
  • SSL应用系列之一:CA证书颁发机构(中心)安装图文详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/140518        如果你需要在组织里发布exchange,或者需要给IIS配置SSL的访问方…

    Linux干货 2015-03-26

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-20 19:35

    很棒,看的出来真的是在很用心的完成作业,继续保持。