N25第四周 chmod chown以及 grep命令的常用示例

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

    [root@localhost ~]# cp -r /etc/skel /home/tuser1
    [root@localhost ~]# ls -al /home/tuser1/skel
    total 12
    drwxr-xr-x. 3 root root  74 Dec 22 04:09 .
    drwx------. 3 root root  17 Dec 22 04:09 ..
    -rw-r--r--. 1 root root  18 Dec 22 04:09 .bash_logout
    -rw-r--r--. 1 root root 193 Dec 22 04:09 .bash_profile
    -rw-r--r--. 1 root root 231 Dec 22 04:09 .bashrc
    drwxr-xr-x. 4 root root  37 Dec 22 04:09 .mozilla
    [root@localhost ~]# chmod -R go-rx /home/tuser1
    [root@localhost ~]# ls -al /home/tuser1/skel
    total 12
    drwx------. 3 root root  74 Dec 22 04:09 .
    drwx------. 3 root root  17 Dec 22 04:09 ..
    -rw-------. 1 root root  18 Dec 22 04:09 .bash_logout
    -rw-------. 1 root root 193 Dec 22 04:09 .bash_profile
    -rw-------. 1 root root 231 Dec 22 04:09 .bashrc
    drwx------. 4 root root  37 Dec 22 04:09 .mozilla
    [root@localhost ~]#

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

    [root@localhost etc]# vim /etc/group
    admins:x:5035:slackware
    clouds:x:5036:
    nova:x:5037:openstack
    mysql:x:227:
    hadoop:x:5038:

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

    [root@localhost etc]# vim passwd

    jow:x:4067:4067::/home/jow:/bin/bash
    centos:x:4068:4068::/home/centos:/bin/bash
    user1:x:4069:4069::/home/user1:/bin/bash
    user2:x:4070:4070::/home/user2:/bin/bash
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    pcp:x:228:228:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
    named:x:25:25:Named:/var/named:/sbin/nologin
    magia:x:1100:1100::/home/linux:/bin/bash
    slackware:x:2002:2016::/home/slackware:/bin/tcsh
    openstack:x:3003:5036::/home/openstack:/bin/bash
    mysql:x:227:227::/home/mysql:/sbin/nologin
    hadoop:x:3004:5038::/home/hadoop:/bin/bash

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

    [root@localhost ~]# cp -r /etc/skel /home/hadoop
    [root@localhost ~]# ls -l /home | grep hadoop
    drwxr-xr-x.  3 root     root       74 Dec 22 04:31 hadoop
    [root@localhost ~]# chmod -R go= /home/hadoop
    [root@localhost ~]# ls -al /home/hadoop 
    total 16
    drwx------.  3 root root   74 Dec 22 04:31 .
    drwxr-xr-x. 26 root root 4096 Dec 22 04:31 ..
    -rw-------.  1 root root   18 Dec 22 04:31 .bash_logout
    -rw-------.  1 root root  193 Dec 22 04:31 .bash_profile
    -rw-------.  1 root root  231 Dec 22 04:31 .bashrc
    drwx------.  4 root root   37 Dec 22 04:31 .mozilla
    [root@localhost ~]# ls -al /home | grep hadoop
    drwx------.  3 root     root       74 Dec 22 04:31 hadoop
    [root@localhost ~]#

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

    [root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
    [root@localhost ~]# ls -al /home | grep hadoop
    drwx------.  3 hadoop   hadoop     74 Dec 22 04:31 hadoop
    [root@localhost ~]#

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

第一种:
    [root@localhost ~]# cat /proc/meminfo | grep  "^[sS].*"
    SwapCached:            0 kB
    SwapTotal:       2098172 kB
    SwapFree:        2098172 kB
    Shmem:              7264 kB
    Slab:             114028 kB
    SReclaimable:      65800 kB
    SUnreclaim:        48228 kB

第二种:
    [root@localhost ~]# cat /proc/meminfo | grep -i "^s.*"
    SwapCached:            0 kB
    SwapTotal:       2098172 kB
    SwapFree:        2098172 kB
    Shmem:              7264 kB
    Slab:             114028 kB
    SReclaimable:      65800 kB
    SUnreclaim:        48228 kB

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

    [root@localhost ~]# cat /etc/passwd | cut -d: -f1,7 
    root:/bin/bash
    bin:/sbin/nologin
    daemon:/sbin/nologin
    adm:/sbin/nologin
    lp:/sbin/nologin
    sync:/bin/sync
    shutdown:/sbin/shutdown
    halt:/sbin/halt
    mail:/sbin/nologin
    [root@localhost ~]# cat /etc/passwd | cut -d: -f1,7 | grep -v "/sbin/nologin"
    root:/bin/bash
    sync:/bin/sync
    shutdown:/sbin/shutdown
    halt:/sbin/halt

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

    [root@localhost ~]# cat /etc/passwd | cut -d: -f1,7 | grep "/bin/bash"
    root:/bin/bash
    admin:/bin/bash
    gentoo:/bin/bash
    cx:/bin/bash
    gestuser:/bin/bash
    moose:/bin/bash
    moosefs:/bin/bash

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

   [root@localhost ~]#  grep "\b[0-9]\{1,2\}\b" /etc/passwd
    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

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

   [root@localhost ~]# grep "^[[:space:]]*$" /boot/grub/grub.conf

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

    [root@localhost rc.d]# grep "^#[[:space:]]\+[^[:space:]]\+"  /etc/rc.d/rc.local
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    # It is highly advisable to create own systemd services or udev rules
    # to run scripts during boot instead of using this file.
    # In contrast to previous versions due to parallel execution during boot
    # this script will NOT be run after all other services.
    # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    # that this script will be executed during boot.
    [root@localhost rc.d]#

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

    [root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
    tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::22                   :::*                    LISTEN     
    tcp6       0      0 ::1:631                 :::*                    LISTEN     
    tcp6       0      0 ::1:25                  :::*                    LISTEN

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

    [root@localhost ~]# useradd bash
    [root@localhost ~]# useradd testbash
    [root@localhost ~]# useradd basher
    [root@localhost ~]# useradd nologin
    [root@localhost ~]# tail -10 /etc/passwd
    named:x:25:25:Named:/var/named:/sbin/nologin
    magia:x:1100:1100::/home/linux:/bin/bash
    slackware:x:2002:2016::/home/slackware:/bin/tcsh
    openstack:x:3003:5036::/home/openstack:/bin/bash
    mysql:x:227:227::/home/mysql:/sbin/nologin
    hadoop:x:3004:5038::/home/hadoop:/bin/bash
    bash:x:4071:4071::/home/bash:/bin/bash
    testbash:x:4072:4072::/home/testbash:/bin/bash
    basher:x:4073:4073::/home/basher:/bin/bash
    nologin:x:4074:4074::/home/nologin:/bin/bash
    [root@localhost ~]# man usermod
    [root@localhost ~]# usermod -s /sbin/nologin nologin
    [root@localhost ~]# grep -E "^([^:]+\b).*\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
    bash:x:4071:4071::/home/bash:/bin/bash
    nologin:x:4074:4074::/home/nologin:/sbin/nologin
    [root@localhost ~]#

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

(0)
diglinuxdiglinux
上一篇 2016-12-22
下一篇 2016-12-23

相关推荐

  • N25第7周作业

    1、创建一个10G分区,并格式为ext4文件系统;   (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;    (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; ]# fdisk /dev/sdb #划出一个…

    Linux干货 2017-02-21
  • Bashe Shell之数组及bash配置文件解析

    数组   数据结构,数据序列,保存了连续的多个数据,可以使用索引获取相关元素,相当于多个变量的集合   §数组名和索引 索引:编号从0开始,属于数值索引   注意:所以可支持使用自定义的格式,而不仅是数值格式,即关联索引,bash4.0版本之后开始支持,bash的数组支持稀疏格式(索引不连续)   §声明数组 &nbs…

    Linux干货 2016-08-24
  • 文本处理三剑客之 Sed

         Sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed主要用来自…

    2017-08-26
  • 脚本初学者

    期待已久的脚本终于在这一周学到了,对于脚本,我感觉就是有两点,第一:考验一个人的思维能力,如果能把你想要的结果用脚本正确的表示出来。第二:以前学的基础命令,怎么组合起来使用。达到以上两点,再难写的脚本都可以写出来的。 一、编写,执行一个脚本        脚本的格式  &nbsp…

    2017-08-05
  • sed与vim相关练习

    sed 练习 以行为单位的新增/删除功能 1将 /etc/passwd 的内容列出并且打印行号,同时,请将第 2~5 行删除! 2在第二行后(亦即是加在第三行)加上“the is xing line?”字样! 3在第二行前(亦即是加在第而行)加上“the is xing line?”字样! 4在第二行后面加入两行字,例如“the is xing line &…

    Linux干货 2016-08-10
  • 16个很有用的在线工具

    1. ExplainShell.com 命令解释 对于Linux用户来说每天都会写各种命令和脚本,那么你可以使用这个网站工具来查看命令式如何工作的,这样可以避免不必要的错误出现;也是一个很好的学习命令的方式 2. BashrcGenerator.com 定制个性命令提示符 简单说就是个性化生成命令提示符,可将生成的代码…

    Linux干货 2015-03-13

评论列表(1条)

  • 马哥教育
    马哥教育 2017-01-03 17:08

    不错,完成的很好,可以看出对基础知识这块掌握的委扎实。再接再励