N25_第四周博客作业_leon
1. 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限
复制
[root@localhost ~]# cp -rp /etc/skel /home/tuser1
查看目录里文件的权限
[root@localhost ~]# ls -la /home/tuser1
total 24
drwxr-xr-x. 3 root root 4096 Nov 14 22:39 .
drwxr-xr-x. 8 root root 4096 Jan 12 19:09 ..
-rw-r–r–. 1 root root 18 May 11 2012 .bash_logout
-rw-r–r–. 1 root root 176 May 11 2012 .bash_profile
-rw-r–r–. 1 root root 124 May 11 2012 .bashrc
drwxr-xr-x. 2 root root 4096 Nov 12 2010 .gnome2
查看目录本身的权限
[root@localhost ~]# ls -ld /home/tuser1
drwxr-xr-x. 3 root root 4096 Nov 14 22:39 /home/tuser1
更改目录及目录或文件的权限
[root@localhost ~]# chmod -R go= /home/tuser1
查看目录本身的权限
[root@localhost ~]# ls -ld /home/tuser1
drwx——. 3 root root 4096 Nov 14 22:39 /home/tuser1
查看目录里文件的权限
[root@localhost ~]# ls -la /home/tuser1
total 24
drwx——. 3 root root 4096 Nov 14 22:39 .
drwxr-xr-x. 8 root root 4096 Jan 12 19:09 ..
-rw——-. 1 root root 18 May 11 2012 .bash_logout
-rw——-. 1 root root 176 May 11 2012 .bash_profile
-rw——-. 1 root root 124 May 11 2012 .bashrc
drwx——. 2 root root 4096 Nov 12 2010 .gnome2
2. 编辑/etc/group文件,添加组hadoop
查看当前系统组信息
[root@localhost ~]# tail -2 /etc/group
nova:x:2020:openstack
mysql:x:498:
添加hadoop组
[root@localhost ~]# echo "hadoop:x:888:" >> /etc/group
查看添加后的组信息
[root@localhost ~]# tail -3 /etc/group
nova:x:2020:openstack
mysql:x:498:
hadoop:x:888:
3. 手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop
查看当前系统用户信息
[root@localhost ~]# tail -5 /etc/passwd
test:x:500:500::/home/test:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/bash
openstack:x:3003:2019::/home/openstack:/bin/bash
mysql:x:498:498::/home/mysql:/sbin/nologin
添加hadoop用户
[root@localhost ~]# echo "hadoop:x:800:888::/home/hadoop:/bin/bash" >> /etc/passwd
查看添加的hadoop用户
[root@localhost ~]# tail -5 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/bash
openstack:x:3003:2019::/home/openstack:/bin/bash
mysql:x:498:498::/home/mysql:/sbin/nologin
hadoop:x:800:888::/home/hadoop:/bin/bash
4. 复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限
复制
[root@localhost ~]# cp -rp /etc/skel/ /home/hadoop
查看权限
[root@localhost ~]# ls -ld /home/hadoop
drwxr-xr-x. 3 root root 4096 Nov 14 22:39 /home/hadoop
[root@localhost ~]# ls -la /home/hadoop
total 24
drwxr-xr-x. 3 root root 4096 Nov 14 22:39 .
drwxr-xr-x. 9 root root 4096 Jan 12 19:31 ..
-rw-r–r–. 1 root root 18 May 11 2012 .bash_logout
-rw-r–r–. 1 root root 176 May 11 2012 .bash_profile
-rw-r–r–. 1 root root 124 May 11 2012 .bashrc
drwxr-xr-x. 2 root root 4096 Nov 12 2010 .gnome2
更改权限
[root@localhost ~]# chmod -R go= /home/hadoop
查看权限
[root@localhost ~]# ls -ld /home/hadoop
drwx——. 3 root root 4096 Nov 14 22:39 /home/hadoop
[root@localhost ~]# ls -la /home/hadoop
total 24
drwx——. 3 root root 4096 Nov 14 22:39 .
drwxr-xr-x. 9 root root 4096 Jan 12 19:31 ..
-rw——-. 1 root root 18 May 11 2012 .bash_logout
-rw——-. 1 root root 176 May 11 2012 .bash_profile
-rw——-. 1 root root 124 May 11 2012 .bashrc
drwx——. 2 root root 4096 Nov 12 2010 .gnome2
5. 修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop
查看属主属组
[root@localhost ~]# ls -ld /home/hadoop
drwx——. 3 root root 4096 Nov 14 22:39 /home/hadoop
[root@localhost ~]# ls -la /home/hadoop
total 24
drwx——. 3 root root 4096 Nov 14 22:39 .
drwxr-xr-x. 9 root root 4096 Jan 12 19:31 ..
-rw——-. 1 root root 18 May 11 2012 .bash_logout
-rw——-. 1 root root 176 May 11 2012 .bash_profile
-rw——-. 1 root root 124 May 11 2012 .bashrc
drwx——. 2 root root 4096 Nov 12 2010 .gnome2
更改属主属组
[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
查看属主属组
[root@localhost ~]# ls -ld /home/hadoop
drwx——. 3 hadoop hadoop 4096 Nov 14 22:39 /home/hadoop
[root@localhost ~]# ls -la /home/hadoop
total 24
drwx——. 3 hadoop hadoop 4096 Nov 14 22:39 .
drwxr-xr-x. 9 root root 4096 Jan 12 19:31 ..
-rw——-. 1 hadoop hadoop 18 May 11 2012 .bash_logout
-rw——-. 1 hadoop hadoop 176 May 11 2012 .bash_profile
-rw——-. 1 hadoop hadoop 124 May 11 2012 .bashrc
drwx——. 2 hadoop hadoop 4096 Nov 12 2010 .gnome2
6. 显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式
第一种
[root@localhost ~]# grep "^[sS]" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4095992 kB
SwapFree: 4095992 kB
Shmem: 212 kB
Slab: 90468 kB
SReclaimable: 35188 kB
SUnreclaim: 55280 kB
第二种
[root@localhost ~]# grep -i "^s" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4095992 kB
SwapFree: 4095992 kB
Shmem: 212 kB
Slab: 90460 kB
SReclaimable: 35196 kB
SUnreclaim: 55264 kB
7. 显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户
选出非/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
test:x:500:500::/home/test:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/bash
openstack:x:3003:2019::/home/openstack:/bin/bash
hadoop:x:800:888::/home/hadoop:/bin/bash
切割用户名
[root@localhost ~]# grep -v "\/sbin\/nologin\>$" /etc/passwd | cut -d : -f1
root
sync
shutdown
halt
test
mageia
slackware
openstack
hadoop
8. 显示/etc/passwd文件中其默认shell为非/bin/bash的用户
选出非/bin/bash的行的行
[root@localhost ~]# grep -v "\/bin\/bash\>$" /etc/passwd
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
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
mysql:x:498:498::/home/mysql:/sbin/nologin
切割用户名
[root@localhost ~]# grep -v "\/bin\/bash\>$" /etc/passwd | cut -d : -f1
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
avahi
sshd
tcpdump
mysql
9. 找出/etc/passwd文件中的一位数或两位数
找出/etc/passwd文件中的一位数或两位数的行
[root@localhost ~]# 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
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
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
只显示匹配到的值
[root@localhost ~]# grep -o "\<[0-9]\{1,2\}\>" /etc/passwd
0
0
1
1
2
2
3
4
4
7
5
0
6
0
7
0
8
12
10
14
11
0
12
13
30
14
50
99
99
81
81
69
69
68
68
38
38
76
89
89
70
70
74
74
72
72
去重
[root@localhost ~]# grep -o "\<[0-9]\{1,2\}\>" /etc/passwd | sort -u
0
1
10
11
12
13
14
2
3
30
38
4
5
50
6
68
69
7
70
72
74
76
8
81
89
99
10. 显示/boot/grub/grub.conf中以至少一个空白字符开头的行
[root@localhost grub]# grep "^[[:space:]]\{1\}" /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-279.el6.x86_64 ro root=UUID=e6060ca1-a25c-4cc4-8812-a87c6ee54a42 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 /initramfs-2.6.32-279.el6.x86_64.img
[root@localhost grub]# grep "^[[:space:]]\{1,\}" /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-279.el6.x86_64 ro root=UUID=e6060ca1-a25c-4cc4-8812-a87c6ee54a42 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 /initramfs-2.6.32-279.el6.x86_64.img
[root@localhost grub]# grep "^[[:space:]]\{1,\}.*" /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-279.el6.x86_64 ro root=UUID=e6060ca1-a25c-4cc4-8812-a87c6ee54a42 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 /initramfs-2.6.32-279.el6.x86_64.img
11. 显示/etc/rc.d/rc.sysinit文件中以#开头,后面至少一个空白字符,而后又有至少一个非空白 字符的行
[root@localhost grub]# grep "^#[[:space:]]\{1,\}[^[[:space:]]].*" /etc/rc.d/rc.sysinit
12. 打出netstat –tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行
[root@localhost grub]# netstat -tan | grep "LISTEN[[:space:]]+$"
13. 添加用户bash,testbash,basher,nologin(此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户信息
[root@localhost ~]# 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:3004:3004::/home/bash:/bin/bash
nologin:x:3007:3007::/home/nologin:/sbin/nologin
14. 显示/proc/meminfo文件中以大写或小写S开头的行;用三种方式
第一种
[root@localhost ~]# grep "^[sS]" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4095992 kB
SwapFree: 4095992 kB
Shmem: 212 kB
Slab: 90468 kB
SReclaimable: 35188 kB
SUnreclaim: 55280 kB
第二种
[root@localhost ~]# grep -i "^s" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4095992 kB
SwapFree: 4095992 kB
Shmem: 212 kB
Slab: 90460 kB
SReclaimable: 35196 kB
SUnreclaim: 55264 kB
15. 显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户
选出非/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
test:x:500:500::/home/test:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/bash
openstack:x:3003:2019::/home/openstack:/bin/bash
hadoop:x:800:888::/home/hadoop:/bin/bash
切割用户名
[root@localhost ~]# grep -v "\/sbin\/nologin\>$" /etc/passwd | cut -d : -f1
root
sync
shutdown
halt
test
mageia
slackware
openstack
hadoop
16. 显示/etc/passwd文件中其默认shell为非/bin/bash的用户
选出非/bin/bash的行的行
[root@localhost ~]# grep -v "\/bin\/bash\>$" /etc/passwd
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
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
mysql:x:498:498::/home/mysql:/sbin/nologin
切割用户名
[root@localhost ~]# grep -v "\/bin\/bash\>$" /etc/passwd | cut -d : -f1
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
avahi
sshd
tcpdump
mysql
17. 找出/etc/passwd文件中的一位数或两位数
找出/etc/passwd文件中的一位数或两位数的行
[root@localhost ~]# 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
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
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
只显示匹配到的值
[root@localhost ~]# grep -o "\<[0-9]\{1,2\}\>" /etc/passwd
0
0
1
1
2
2
3
4
4
7
5
0
6
0
7
0
8
12
10
14
11
0
12
13
30
14
50
99
99
81
81
69
69
68
68
38
38
76
89
89
70
70
74
74
72
72
去重
[root@localhost ~]# grep -o "\<[0-9]\{1,2\}\>" /etc/passwd | sort -u
0
1
10
11
12
13
14
2
3
30
38
4
5
50
6
68
69
7
70
72
74
76
8
81
89
99
原创文章,作者:leon,如若转载,请注明出处:http://www.178linux.com/66662