第四周作业
1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
使用到命令chmod
chmod 修改文件或目录权限
chmod [option] MODE file
option
-R 递归修改
[root@localhost ~]# cp -r /etc/skel /home/tuser1 [root@localhost ~]# ll /home/tuser1 -d drwxr-xr-x 2 root root 59 Feb 2 12:30 /home/tuser1 [root@localhost ~]# chmod 700 /home/tuser1 [root@localhost ~]# ll /home/tuser1/ -d drwx------ 2 root root 59 Feb 2 12:30 /home/tuser1/
2、编辑/etc/group文件,添加组hadoop。
[root@localhost ~]# echo "hadoop:x:3007:" >>/etc/group [root@localhost ~]# tail -1 /etc/group
3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。
[root@localhost ~]# echo "hadoop:x:3007::3007::/home/hadoop:/bin/bash" >> /etc/passwd
4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。
[root@localhost ~]# cp -a /etc/skel/ /home/hadoop [root@localhost ~]# chmod 700 /home/hadoop [root@localhost ~]# ll -d /home/hadoop/ drwx------. 2 root root 59 Oct 27 18:20 /home/hadoop/
5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。
[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop [root@localhost ~]# ll -d /home/hadoop/ drwx------. 2 hadoop hadoop 59 Oct 27 18:20 /home/hadoop/
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;
使用到命令grep 文本过滤工具
grep [option] PATTERN file
option
-i 忽略大小写
-o 只显示匹配到的文本
-v 只显示不匹配的文本
-E 支持扩展正则表达式
-q 不输出任何内容
-A # 后#行
-B # 前#行
-C # 前后各#行
PATTERN支持使用正则表达式和扩展的正则表达式
方法1
[root@localhost ~]# grep -i ^s /proc/meminfo SwapCached: 0 kB SwapTotal: 1048572 kB SwapFree: 1048572 kB Shmem: 4676 kB Slab: 51208 kB SReclaimable: 26556 kB SUnreclaim: 24652 kB
方法2
[root@localhost ~]# grep ^[s,S] /proc/meminfo SwapCached: 0 kB SwapTotal: 1048572 kB SwapFree: 1048572 kB Shmem: 4676 kB Slab: 51208 kB SReclaimable: 26556 kB SUnreclaim: 24652 kB
方法3
[root@localhost ~]# grep -E "^(s|S)" /proc/meminfo SwapCached: 0 kB SwapTotal: 1048572 kB SwapFree: 1048572 kB Shmem: 4676 kB Slab: 51208 kB SReclaimable: 26556 kB SUnreclaim: 24652 kB
7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
[root@localhost ~]# grep -v "/sbin/nologin" /etc/passwd| cut -d: -f 1 root sync shutdown halt mageia slackware clouds openstack hadoop
8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
[root@localhost ~]# grep "/bin/bash" /etc/passwd| cut -d: -f 1 root mageia clouds openstack hadoop
9、找出/etc/passwd文件中的一位数或两位数;
[root@localhost ~]# 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 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 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/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 -E ^" " /boot/grub2/grub.cfg load_env set default="${next_entry}" set next_entry= save_env next_entry set boot_once=true set default="${saved_entry}" menuentry_id_option="--id" menuentry_id_option="" set saved_entry="${prev_saved_entry}" save_env saved_entry set prev_saved_entry= save_env prev_saved_entry set boot_once=true if [ -z "${boot_once}" ]; then saved_entry="${chosen}" save_env saved_entry fi if [ x$feature_all_video_module = xy ]; then insmod all_video else insmod efi_gop insmod efi_uga insmod ieee1275_fb insmod vbe insmod vga insmod video_bochs insmod video_cirrus fi set timeout_style=menu set timeout=5 set timeout=5 source ${prefix}/user.cfg if [ -n ${GRUB2_PASSWORD} ]; then set superusers="root" export superusers password_pbkdf2 root ${GRUB2_PASSWORD} fi source ${config_directory}/custom.cfg source $prefix/custom.cfg; [root@localhost ~]# grep ^" " /boot/grub2/grub.cfg load_env set default="${next_entry}" set next_entry= save_env next_entry set boot_once=true set default="${saved_entry}" menuentry_id_option="--id" menuentry_id_option="" set saved_entry="${prev_saved_entry}" save_env saved_entry set prev_saved_entry= save_env prev_saved_entry set boot_once=true if [ -z "${boot_once}" ]; then saved_entry="${chosen}" save_env saved_entry fi if [ x$feature_all_video_module = xy ]; then insmod all_video else insmod efi_gop insmod efi_uga insmod ieee1275_fb insmod vbe insmod vga insmod video_bochs insmod video_cirrus fi set timeout_style=menu set timeout=5 set timeout=5 source ${prefix}/user.cfg if [ -n ${GRUB2_PASSWORD} ]; then set superusers="root" export superusers password_pbkdf2 root ${GRUB2_PASSWORD} fi source ${config_directory}/custom.cfg source $prefix/custom.cfg;
11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
[root@localhost ~]# grep -E "^#[[:space:]]+[^[:space:]]+" /etc/rc.d/rc.local # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot.
12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[root@localhost ~]# netstat -tan|grep "LISTEN"' '*$ tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN
13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[root@localhost ~]# grep -E "^(\<[[: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:3008::/home/bash:/bin/bash nologin:x:3010:3011::/home/nologin:/sbin/nologin [root@localhost ~]# 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:3008::/home/bash:/bin/bash nologin:x:3010:3011::/home/nologin:/sbin/nologin
14、显示/proc/meminfo文件中以大写或小写S开头的行;用三种方式;
方法1 [root@localhost ~]# grep -i ^s /proc/meminfo SwapCached: 0 kB SwapTotal: 1048572 kB SwapFree: 1048572 kB Shmem: 4676 kB Slab: 51208 kB SReclaimable: 26556 kB SUnreclaim: 24652 kB 方法2 [root@localhost ~]# grep ^[s,S] /proc/meminfo SwapCached: 0 kB SwapTotal: 1048572 kB SwapFree: 1048572 kB Shmem: 4676 kB Slab: 51208 kB SReclaimable: 26556 kB SUnreclaim: 24652 kB 方法3 [root@localhost ~]# grep -E "^(s|S)" /proc/meminfo SwapCached: 0 kB SwapTotal: 1048572 kB SwapFree: 1048572 kB Shmem: 4676 kB Slab: 51208 kB SReclaimable: 26556 kB SUnreclaim: 24652 kB
15、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
[root@localhost ~]# grep -E "/sbin/nologin$" /etc/passwd |cut -d: -f1 bin daemon adm lp mail operator games ftp nobody avahi-autoipd systemd-bus-proxy systemd-network dbus polkitd tss postfix sshd mysql nologin
16、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
[root@localhost ~]# grep -E "/bin/bash$" /etc/passwd |cut -d: -f1 root mageia clouds openstack hadoop bash testbash basher
17、找出/etc/passwd文件中的一位数或两位数;
[root@localhost ~]# 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 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