第四周作业—用户权限管理和正则表达式练习

权限管理、正则表达式练习

1. 复制/etc/skel目录为/home/tuser1,要求 /home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
[root@localhost ~]# cp -R /etc/skel /home/tuser1
[root@localhost ~]# ls -ld /home/tuser1
drwxr-xr-x. 4 root root 4096 5月 21 05:09 /home/tuser1
[root@localhost ~]# chmod -R go=— /home/tuser1
[root@localhost ~]# ls -ld /home/tuser1
drwx——. 4 root root 4096 5月 21 05:09 /home/tuser1
2. 编辑 /etc/group 文件,添加组 hadoop。
[root@localhost ~]# vim /etc/group
在最后一行添加: hadoop:x:2008:
[root@localhost ~]# grep “hadoop” /etc/group
hadoop:x:2008:
3. 手动编辑 /etc/passwd 文件,新增一行,添加用户 hadoop,其基本组ID为hadoop组的id号;其家目录为 /home/hadoop。
[root@localhost ~]# vim /etc/passwd
[root@localhost ~]# tail -1 /etc/passwd
hadoop:x:2008:2008::/home/hadoop:
4. 复制 /etc/skel目录为 /home/hadoop,要求修改 hadoop 目录的属组和其它用户没有任何访问权限。
[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop
drwxr-xr-x. 4 root root 4096 5月 21 05:24 /home/hadoop
[root@localhost ~]# chmod -R go=— /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop
drwx——. 4 root root 4096 5月 21 05:24 /home/hadoop
[root@localhost ~]# id hadoop
uid=2008(hadoop) gid=2008(hadoop) 组=2008(hadoop)
5. 修改 /home/hadoop 目录及其内部所有文件的属主为 hadoop,属组为hadoop。
[root@localhost ~]# ls -ld /home/hadoop
drwx——. 4 root root 4096 5月 21 05:24 /home/hadoop
[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
[root@localhost ~]# ls -la /home/hadoop
总用量 28
drwx——. 4 hadoop hadoop 4096 5月 21 05:24 .
drwxr-xr-x. 14 root root 4096 5月 21 05:24 ..
-rw——-. 1 hadoop hadoop 18 5月 21 05:24 .bash_logout
-rw——-. 1 hadoop hadoop 176 5月 21 05:24 .bash_profile
-rw——-. 1 hadoop hadoop 124 5月 21 05:24 .bashrc
drwx——. 2 hadoop hadoop 4096 5月 21 05:24 .gnome2
drwx——. 4 hadoop hadoop 4096 5月 21 05:24 .mozilla
6. 显示 /proc/meminfo 文件中以大写或小写s开头的行;用两种方式;
方法1:
[root@localhost ~]# grep -i “^s.*” /proc/meminfo
SwapCached: 8 kB
SwapTotal: 3157400 kB
SwapFree: 3157392 kB
Shmem: 4604 kB
Slab: 110976 kB
SReclaimable: 41000 kB
SUnreclaim: 69976 kB
方法2:
[root@localhost ~]# grep “^[sS].*” /proc/meminfo
SwapCached: 8 kB
SwapTotal: 3157400 kB
SwapFree: 3157392 kB
Shmem: 4604 kB
Slab: 110940 kB
SReclaimable: 41004 kB
SUnreclaim: 69936 kB
方法3:
[root@localhost ~]# grep -E “^(s|S).*” /proc/meminfo
SwapCached: 8 kB
SwapTotal: 3157400 kB
SwapFree: 3157392 kB
Shmem: 4604 kB
Slab: 110916 kB
SReclaimable: 40996 kB
SUnreclaim: 69920 kB
7. 显示 /etc/passwd 文件中其默认shell为非 /sbin/nologin 的用户;
[root@localhost ~]# grep -v “/sbin/nologin$” /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
centos:x:500:500::/home/centos:/bin/bash
fedora:x:501:501::/home/fedora:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/tcsh
user1:x:2003:2003::/home/user1:/bin/bash
user2:x:2004:2004::/home/user2:/bin/bash
user3:x:2005:2005::/home/user3:/bin/bash
a1:x:2006:2006::/home/a1:/bin/bash
a2:x:2007:2007::/home/a2:/bin/bash
hadoop:x:2008:2008::/home/hadoop:
[root@localhost ~]# grep -v “/sbin/nologin$” /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
centos
fedora
mageia
slackware
user1
user2
user3
a1
a2
hadoop
bash
testbash
basher
8. 显示/etc/passwd 文件中其默认shell为 /bin/bash的用户;
[root@localhost ~]#
[root@localhost ~]# grep “/bin/bash$” /etc/passwd
root:x:0:0:root:/root:/bin/bash
centos:x:500:500::/home/centos:/bin/bash
fedora:x:501:501::/home/fedora:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
user1:x:2003:2003::/home/user1:/bin/bash
user2:x:2004:2004::/home/user2:/bin/bash
user3:x:2005:2005::/home/user3:/bin/bash
a1:x:2006:2006::/home/a1:/bin/bash
a2:x:2007:2007::/home/a2:/bin/bash
[root@localhost ~]# grep “/bin/bash$” /etc/passwd | cut -d: -f1
root
centos
fedora
mageia
user1
user2
user3
a1
a2
9. 找出 /etc/passwd 文件中的一位数或两位数;
[root@localhost ~]# grep -E “\<[0-9]{1,2}\>” /etc/passwd –color=auto
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
10. 显示 /boot/grub/grub.conf 中以至少一个空白字符开头的行;
[root@localhost ~]# grep -E “^[[:space:]]+” /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 “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 nologin -s /sbin/nologin
[root@localhost ~]# tail -4 /etc/passwd
bash:x:2009:2009::/home/bash:/bin/bash
testbash:x:2010:2010::/home/testbash:/bin/bash
basher:x:2011:2011::/home/basher:/bin/bash
nologin:x:2012:2012::/home/nologin:/sbin/nologin
[root@localhost ~]# grep -E “(^[^:]+).*\1$” /etc/passwd -o
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
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
bash:x:2009:2009::/home/bash:/bin/bash
basher:x:2011:2011::/home/basher:/bin/bash
nologin:x:2012:2012::/home/nologin:/sbin/nologin

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/99938

发表评论

登录后才能评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.

评论列表(1条)

  • 马哥教育
    马哥教育 2018-06-06 10:43

    写的很好,可以用来当范文展示了

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1823388528@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班