N21_第4周作业grep

第四周作业

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

[root@iZ28zld7ztoZ home]# cp -a /etc/skel/ /home/tuser1
[root@iZ28zld7ztoZ ~]# chmod g-wxr,o-wxr -R /home/tuser1/

1470491892971707.jpg

或
[root@iZ28zld7ztoZ home]# cp -a /etc/skel/ /home/tuser1
[root@iZ28zld7ztoZ ~]# cd /home
[root@iZ28zld7ztoZ home]# chmod -R 700 tuser1/

1470491950763017.jpg

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

[root@iZ28zld7ztoZ ~]# vi /etc/group
#增加
hadoop:x:901
[root@iZ28zld7ztoZ ~]# tail -fn 1 /etc/group

1470492270987449.jpg

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

[root@iZ28zld7ztoZ ~]# vi /etc/passwd
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
[root@iZ28zld7ztoZ ~]# id hadoop
uid=501(hadoop) gid=501(hadoop) groups=501(hadoop)

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

跟第1题一样。

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

[root@iZ28zld7ztoZ ~]# cd /home
[root@iZ28zld7ztoZ home]# ll
total 20
drwxr-xr-x  3 root root 4096 Jun 30 17:02 apache-tomcat01
drwxr-xr-x  3 root root 4096 Jun 30 17:04 apache-tomcat02
drwxr-xr-x  3 root root 4096 Jul 23 22:41 test
drwxr-xr-x  2 root root 4096 Jul 23 22:41 test2
drwx------. 2 root root 4096 Jan 30  2015 tuser1
[root@iZ28zld7ztoZ home]# mv tuser1/ hadoop
[root@iZ28zld7ztoZ home]# chown hadoop.hadoop -R hadoop/
[root@iZ28zld7ztoZ home]# ll
total 20
drwxr-xr-x  3 root   root   4096 Jun 30 17:02 apache-tomcat01
drwxr-xr-x  3 root   root   4096 Jun 30 17:04 apache-tomcat02
drwx------. 2 hadoop hadoop 4096 Jan 30  2015 hadoop
drwxr-xr-x  3 root   root   4096 Jul 23 22:41 test
drwxr-xr-x  2 root   root   4096 Jul 23 22:41 test2

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

drwxr-xr-x  2 root   root   4096 Jul 23 22:41 test2
[root@iZ28zld7ztoZ home]# grep ^[Ss] /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57268 kB
SReclaimable:      37088 kB
SUnreclaim:        20180 kB
[root@iZ28zld7ztoZ home]# grep -i '^s' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57276 kB
SReclaimable:      37092 kB
SUnreclaim:        20184 kB
[root@iZ28zld7ztoZ home]# grep '^\(S\|s\)' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57276 kB
SReclaimable:      37092 kB
SUnreclaim:        20184 kB

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

[root@iZ28zld7ztoZ home]# cat /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
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
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
centos:x:900:900::/home/centos:/bin/bash
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
[root@iZ28zld7ztoZ home]# 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
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
centos:x:900:900::/home/centos:/bin/bash

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

[root@iZ28zld7ztoZ home]# grep '/bin/bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
centos:x:900:900::/home/centos:/bin/bash

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

[root@iZ28zld7ztoZ home]# cat /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
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
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
centos:x:900:900::/home/centos:/bin/bash
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
[root@iZ28zld7ztoZ home]# grep -wE '[0-9]{1,2}' /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
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
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
或:
[root@iZ28zld7ztoZ home]# grep -E '\<[0-9]{1,2}\>' /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
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
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash

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

[root@iZ28zld7ztoZ ~]# grep -E '^[[:space:]]' /boot/grub/grub.conf 
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-431.23.3.el6.x86_64 ro root=UUID=94e4e384-0ace-437f-bc96-057dd64f42ee rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /boot/initramfs-2.6.32-431.23.3.el6.x86_64.img
[root@iZ28zld7ztoZ ~]# grep -E '^[[:space:]]+' /boot/grub/grub.conf 
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-431.23.3.el6.x86_64 ro root=UUID=94e4e384-0ace-437f-bc96-057dd64f42ee rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /boot/initramfs-2.6.32-431.23.3.el6.x86_64.img

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

[root@iZ28zld7ztoZ ~]# grep -E '^#[[:space:]]+[^[:space:]]+' /etc/rc.d/rc.sysinit
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
或:
[root@iZ28zld7ztoZ ~]# grep -E ^#[[:space:]]\{1\}[^[:space:]]\{1\} /etc/rc.d/rc.sysinit
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.

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

[root@iZ28zld7ztoZ ~]# netstat -anpt | grep 'LISTEN[[:space:]]*'

1470494913608890.jpg

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

[root@iZ28zld7ztoZ ~]# useradd bash
[root@iZ28zld7ztoZ ~]# useradd testbash
[root@iZ28zld7ztoZ ~]# useradd basher
[root@iZ28zld7ztoZ ~]# useradd -s /sbin/nologin nologin
[root@iZ28zld7ztoZ ~]# grep '^\([^:]\+\):.*/\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:901:902::/home/bash:/bin/bash
nologin:x:904:905::/home/nologin:/sbin/nologin
或:
[root@iZ28zld7ztoZ ~]# grep -E  "(^[[:alpha:]]+):.*\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:901:902::/home/bash:/bin/bash
nologin:x:904:905::/home/nologin:/sbin/nologin

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

[root@iZ28zld7ztoZ ~]# grep '^[Ss]' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57560 kB
SReclaimable:      37380 kB
SUnreclaim:        20180 kB
[root@iZ28zld7ztoZ ~]# grep -i '^s' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57564 kB
SReclaimable:      37384 kB
SUnreclaim:        20180 kB
[root@iZ28zld7ztoZ ~]# grep '^\(S\|s\)' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57560 kB
SReclaimable:      37380 kB
SUnreclaim:        20180 kB
[root@iZ28zld7ztoZ ~]# awk '/^[Ss]/{print}' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57564 kB
SReclaimable:      37384 kB
SUnreclaim:        20180 kB
[root@iZ28zld7ztoZ ~]# sed -n '/^[Ss]/p' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57560 kB
SReclaimable:      37384 kB
SUnreclaim:        20176 kB
[root@iZ28zld7ztoZ ~]# grep -v '^[^Ss]' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Shmem:               168 kB
Slab:              57560 kB
SReclaimable:      37384 kB
SUnreclaim:        20176 kB

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

[root@iZ28zld7ztoZ ~]# 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
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
centos:x:900:900::/home/centos:/bin/bash
bash:x:901:902::/home/bash:/bin/bash
testbash:x:902:903::/home/testbash:/bin/bash
basher:x:903:904::/home/basher:/bin/bash

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

[root@iZ28zld7ztoZ ~]# grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
postgres:x:500:500:PostgreSQL:/home/PostgreSQL/9.1:/bin/bash
hadoop:x:501:501:zhouyong,zhangkong,18061779507,025:/home/hadoop:/bin/bash
centos:x:900:900::/home/centos:/bin/bash
bash:x:901:902::/home/bash:/bin/bash
testbash:x:902:903::/home/testbash:/bin/bash
basher:x:903:904::/home/basher:/bin/bash

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

[root@iZ28zld7ztoZ ~]# grep '\<[0-9]\{1,2\}\>' /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

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

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-08 16:31

    写的很好,排版也很棒,加油