首页 > 代码库 > 第四周作业!
第四周作业!
1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
答:先复制skel目录下的文件到/home/tuser1下,然后再使用权限修改命令修改/home/tuser1目录及其内部文件的权限;
cp -r /etc/skel /home/tuser1 && chmod -R 700 /home/tuser1
[root@yang ~]# cp -r /etc/skel /home/tuser1 && chmod -R 700 /home/tuser1 [root@yang ~]# ls -a /etc/skel . .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla [root@yang ~]# ls -a /home/tuser1/ . .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla [root@yang ~]# ls -al /home/tuser1/ total 28 drwx------. 4 root root 4096 Aug 26 10:57 . drwxr-xr-x. 4 root root 4096 Aug 26 10:57 .. -rwx------. 1 root root 18 Aug 26 10:57 .bash_logout -rwx------. 1 root root 176 Aug 26 10:57 .bash_profile -rwx------. 1 root root 124 Aug 26 10:57 .bashrc drwx------. 2 root root 4096 Aug 26 10:57 .gnome2 drwx------. 4 root root 4096 Aug 26 10:57 .mozilla
2、编辑/etc/group文件,添加组hadoop。
答:1、使用vim命令打开/etc/group,按G命令跳转到最后一行,按o命令在末行新增一行同时添加hadoop:x:2000:字段,按esc键输入:wq保存退出;
[root@localhost ~]# vim /etc/group
2、使用nano打开,尾行添加hadoop:x:2000:字段,按Ctrl+O保存,按Ctrl+X退出;
[root@localhost ~]# nano /etc/group
3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。
答:使用vim命令,打开/etc/passwd,按G,o,在尾部添加一行输入 hadoop:x:2000:2000::/home/hadoop:/bin/bash
[root@localhost ~]# vim /etc/passwd
4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。
答:cp -r /etc/skel /home/hadoop && chmod -u hadoop 700 /home/hadoop
[root@localhost ~]# cp -r /etc/skel /home/hadoop && chmod 700 /home/hadoop [root@localhost ~]# ls -dl /home/hadoop drwx------. 2 root root 59 8月 18 00:27 /home/hadoop
5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。
答:chown -R hadoop:hadoop /home/hadoop
[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop [root@localhost ~]# ls -al /home/hadoop 总用量 12 drwx------. 2 hadoop hadoop 59 8月 18 00:27 . drwxr-xr-x. 4 root root 31 8月 18 00:27 .. -rw-r--r--. 1 hadoop hadoop 18 8月 18 00:27 .bash_logout -rw-r--r--. 1 hadoop hadoop 193 8月 18 00:27 .bash_profile -rw-r--r--. 1 hadoop hadoop 231 8月 18 00:27 .bashrc
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;
答:答案如下:
[root@localhost proc]# cat /proc/meminfo | grep "^[sS]" #方法一使用管道传递参数给grep SwapCached: 0 kB SwapTotal: 2097148 kB SwapFree: 2097148 kB Shmem: 6796 kB Slab: 76228 kB SReclaimable: 35724 kB SUnreclaim: 40504 kB [root@localhost proc]# grep "^[sS]" /proc/meminfo #方法二 SwapCached: 0 kB SwapTotal: 2097148 kB SwapFree: 2097148 kB Shmem: 6796 kB Slab: 76228 kB SReclaimable: 35724 kB SUnreclaim: 40504 kB [root@localhost proc]# grep -i "^s" /proc/meminfo #方法三 -i 参数为不区分大小写 SwapCached: 0 kB SwapTotal: 2097148 kB SwapFree: 2097148 kB Shmem: 6796 kB Slab: 76240 kB SReclaimable: 35732 kB SUnreclaim: 40508 kB
7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
答:这里显示除默认shell为/sbin/nologin的用户,可以用grep -v命令来显示除模式匹配到的以外的行,passwd文档中默认shell在行尾,所有用$进行行尾锚定,然后对行进行切分,结果如下:
[root@localhost ~]# grep -v "/sbin/nologin$" /etc/passwd | cut -d":" -f1 root sync shutdown halt gentoo mageia slackware opentack openstacd openstack archlinux [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 gentoo:x:500:500::/home/gentoo:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh opentack:x:3003:2603::/home/opentack:/bin/bash openstacd:x:3004:3004::/home/openstacd:/bin/bash openstack:x:3005:3005::/home/openstack:/bin/bash archlinux:x:3006:3006::/home/archlinux:/bin/bash
8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
答:此题需用管道,先用grep筛选出所有符合条件的行,然后用cut命令以":"进行切分,第一列即答案;
[root@localhost proc]# grep "/bin/bash$" /etc/passwd | cut -d":" -f 1 root user1 hadoop
9、找出/etc/passwd文件中的一位数或两位数;
答:这里需要用到grep的单词词首词尾锚定,一位或两位数字即单个数字至少重复一次,至多两次,即\<\{1,2\}\>,结果如下
[root@localhost proc]# 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 operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/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 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
也可以使用egrep:
[root@localhost proc]# egrep "\<[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 operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/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 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;
答:这里只需要确定每行行首有一个空白字符就符合要求,回答如下:
[root@localhost ~]# grep "^[[:space:]]" /boot/grub/grub.conf root (hd0,0) kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 r d_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img
11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
答:grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.sysinit或者egrep "^#[[:space:]]+[^[:space:]] /etc/rc.d/rc.sysinit
[root@localhost ~]# 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. # Check SELinux status # Print a text banner. # Only read this once. # Initialize hardware # Set default affinity # Load other user-defined modules # Load modules (for backward compatibility with VARs) # Configure kernel parameters # Set the hostname. # Sync waiting for storage. # Device mapper & related initialization # Start any MD RAID arrays that haven‘t been started yet # Remount the root filesystem read-write. # Clean up SELinux labels # If relabeling, relabel mount points. # Mount all other filesystems (except for NFS and /proc, which is already # mounted). Contrary to standard usage, # filesystems are NOT unmounted in single user mode. # The ‘no‘ applies to all listed filesystem types. See mount(8). # Check to see if a full relabel is needed # Update quotas if necessary # Initialize pseudo-random number generator # Configure machine if necessary. # Clean out /. # Do we need (w|u)tmpx files? We don‘t set them up, but the sysadmin might... # Clean up /var. # Clean up utmp/wtmp # Clean up various /tmp bits # Make ICE directory # Start up swapping. # Set up binfmt_misc # Boot time profiles. Yes, this should be somewhere else. # Now that we have all of our basic modules loaded and the kernel going, # let‘s dump the syslog ring somewhere so we can find it later # create the crash indicator flag to warn on crashes, offer fsck with timeout # Let rhgb know that we‘re leaving rc.sysinit [root@localhost ~]#
12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[root@localhost ~]# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:35722 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 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 tcp 0 0 192.168.199.147:22 192.168.199.198:59938 ESTABLISHED tcp 0 0 :::51118 :::* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN [root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$" tcp 0 0 0.0.0.0:35722 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 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 tcp 0 0 :::51118 :::* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN
13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
答:使用useradd命令按照要求添加用户,最后一个使用-s参数指定默认shell,难点在筛选出用户名和其默认shell一致的用户的信息,用户名在每行行首用^进行行首锚定,且可视为为一个单词,可以由多个字母和数字组成,而其shell在行尾因此要用$进行行尾锚定,且与用户名一致,可用grep的后向引用,命令如下:
useradd bash && useradd testbash && useradd basher && useradd -s /sbin/nologin nologin && grep "^\(\<[[:alnum:]]*\>\).*\1$" /etc/passwd
[root@localhost ~]# useradd bash && useradd testbash && useradd basher && useradd -s /sbin/nologin nologin && grep "^\(\<[[:alnum:]]*\>\).*\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:3007:3007::/home/bash:/bin/bash nologin:x:3010:3010::/home/nologin:/sbin/nologin
本文出自 “小憩清风” 博客,请务必保留此出处http://xiaoqiqingfeng.blog.51cto.com/3720264/1843488
第四周作业!