马哥运维班第四周做业

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

[root@localhost home]# cp -r /etc/skel /home/tsuser1 && ls -ld /home/tsuser1
drwxr-xr-x. 4 root root 4096 Aug 28 05:46 /home/tsuser1
[root@localhost home]# chmod g-r,o-r /home/tsuser1 &&  ls -ld /home/tsuser1
drwx--x--x. 4 root root 4096 Aug 28 05:46 /home/tsuser1

[root@localhost home]# su - nat   //切换用户nat测试其余用户是否有访问权限,提示权限不容许。
Welcome 500 
[nat@localhost ~]$ ls /home/tsuser1
ls: cannot open directory /home/tsuser1: Permission denied


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

    1.打开文件,由于须要直接在最后一行添加新组,用选项+,直接打开文件光标定位到最后一行非空白行bash

[root@localhost ~]# vi + /etc/passwd

    2.打开文件,首先查找是否存在hadoop用户,冒号模式下 :?hadoop 显示E486: Pattern not found: hadoop,tcp

说明此时系统没有hadoop组,group文件的格式:group_name:passwd:GID:user_list,能够直接新添一行 :o   在当前行行下新添一行 hadoop:x:503:ide

slocate:x:21:
nat:x:500:
yannic:x:501:
user1:x:502:
hadoop:x:503:

    3.:wq保存退出oop

    4.测试是否添加组成功:测试

[root@localhost ~]# useradd test1 -g hadoop
[root@localhost ~]#

若是不存在提示useradd: group 'hadoop' does not exist,这里没报错说明添加组成功ui


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

    1.打开文件3d

[root@localhost ~]# vi + /etc/passwd

    2.冒号模式下 :?hadoop 显示E486: Pattern not found: hadoop

    3.因为组成比较复杂,我这里直接复制上面一行,yy 复制一行 p:粘贴,而后

nat:x:500:500:nat:/home/nat:/bin/bash
yannic:x:501:501::/home/yannic:/bin/bash
user1:x:502:502::/home/user1:/bin/bash
user2:x:503:503::/home/user2:/bin/bash
test1:x:504:503::/home/test1:/bin/bash
test1:x:504:503::/home/test1:/bin/bash

    4.修改,按i进入输入模式,UID能够本身定义,centos6 普通用户uid 500+,我这里定义504.gid定义为hadoop的gid503,家目录修改成/home/hadoop

nat:x:500:500:nat:/home/nat:/bin/bash
yannic:x:501:501::/home/yannic:/bin/bash
user1:x:502:502::/home/user1:/bin/bash
user2:x:503:503::/home/user2:/bin/bash
test1:x:504:503::/home/test1:/bin/bash
hadoop:x:505:503::/home/hadoop:/bin/bash
-- INSERT --

    5.:wq保存退出


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

[root@localhost ~]#  cp -r /etc/skel /home/hadoop && ls -ld /home/hadoop
drwxr-xr-x. 4 root root 4096 Aug 28 21:30 /home/hadoop
[root@localhost ~]# chmod g-r,o-r /home/hadoop && ls -ld /home/hadoop
drwx--x--x. 4 root root 4096 Aug 28 21:30 /home/hadoop


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

//chown OWNER:GROUP filename:修改文件属主和属组
//-R 选项,表示递归,文件以及下子文件和文件目录和父目录权限一致
[root@localhost hadoop]# chown -R hadoop:hadoop /home/hadoop && ls -ld /home/hadoop
drwx--x--x. 4 hadoop hadoop 4096 Aug 28 21:39 /home/hadoop
//用root用户测试,建立的新文件属主为hadoop,属组为hadoop
[root@localhost hadoop]# touch a.txt && ls -l a.txt
-rw-r--r--. 1 hadoop hadoop 0 Aug 28 21:42 a.txt


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

    1.使用-i选项,忽略大小写

[root@localhost hadoop]# grep -i '^s' /proc/meminfo 
SwapCached:         1000 kB
SwapTotal:       2031608 kB
SwapFree:        2025636 kB
Shmem:              4380 kB
Slab:             204440 kB
SReclaimable:     134516 kB
SUnreclaim:        69924 kB

    2.使用[]通配符匹配,匹配[]内任意单个字符

[root@localhost hadoop]# grep '^[sS]' /proc/meminfo 
SwapCached:         1000 kB
SwapTotal:       2031608 kB
SwapFree:        2025636 kB
Shmem:              4380 kB
Slab:             204432 kB
SReclaimable:     134516 kB
SUnreclaim:        69916 kB

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

    //添加两个用户,一个默认shell为/sbin/nologin,另外一个为/bin/nologin

[root@localhost hadoop]# useradd test2 -s "/sbin/nologin"
[root@localhost hadoop]# useradd test3 -s "/bin/nologin"

    //-v 选项显示不匹配的行,$行尾匹配grep "/bin/bash$" /etc/passwd | cut -d: -f1

[root@localhost hadoop]# grep -v "/sbin/nologin$" /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
nat
yannic
user1
user2
test1
hadoop
test3



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

[root@localhost hadoop]# grep "/bin/bash$" /etc/passwd | cut -d: -f1
root
nat
yannic
user1
user2
test1
hadoop


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

    [0-9]:匹配数字,{1,2}:匹配次数,1~2次

    \<:词首锚定,\>:词尾锚定,这里要注意的、必定要加上这个位置锚定,题目要求1位或两位,若是不加上限制则会匹配三位数、四位数等

    -o:只显示匹配到的字符串

[root@localhost hadoop]# grep -o "\<[0-9]\{1,2\}\>" /etc/passwd
0
0
1
1
2
2
3
4
4
7

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

    ^:行首匹配

    [[:space:]]:匹配空白字符或tab字符

[root@localhost hadoop]# grep "^[[:space:]]" /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=33ddc759-09de-4936-845a-811c03ea3b08 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img

    其实这样就能够了,题目中要求至少一个空白字符,有一个空白字符就已经知足了,固然你不放心,加上\+:匹配次数,至少一次,效果也是同样的

[root@localhost hadoop]# grep "^[[:space:]]\+" /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=33ddc759-09de-4936-845a-811c03ea3b08 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img


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

[root@localhost hadoop]# grep "^#.*[[: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.
#remount /dev/shm to set attributes from fstab #669700
#remount /proc to set attributes from fstab #984003
# Check SELinux status


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

    [:space:]:空白字符,*匹配任意次数,能够0次或屡次

    $:行尾锚定,加上位置锚定后,后面就不能跟其余字符

[root@localhost hadoop]# netstat -tan | grep "LISTEN[[:space:]]*$"
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      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN

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

    1.添加用户

[root@localhost hadoop]# useradd bash;useradd testbash;useradd basher;useradd -s "/sbin/nologin" nologin

    2.查看刚添加的用户是否正确

[root@localhost hadoop]# tail -4 /etc/passwd
bash:x:508:508::/home/bash:/bin/bash
testbash:x:509:509::/home/testbash:/bin/bash
basher:x:510:510::/home/basher:/bin/bash
nologin:x:511:511::/home/nologin:/sbin/nologin

    3.使用()分组,\1后向引用最外层小括号的内容

[root@localhost hadoop]#  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:508:508::/home/bash:/bin/bash
nologin:x:511:511::/home/nologin:/sbin/nologin

若有错误之处,烦请看官评论里指点一下,小女子不胜感激。