1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
[root@www /]# cp -r /etc/skel /home/tuser1 && chmod 700 -R /home/tuser1
[root@www /]# echo $?
0
[root@www home]# ll -al /home/tuser1/
总用量 12
drwx------. 3 root root 74 11月 30 13:14 .
drwxr-xr-x. 4 root root 30 11月 30 13:14 ..
-rwx------. 1 root root 18 11月 30 13:14 .bash_logout
-rwx------. 1 root root 193 11月 30 13:14 .bash_profile
-rwx------. 1 root root 231 11月 30 13:14 .bashrc
drwx------. 4 root root 37 11月 30 13:14 .mozilla
cp SRC DEST
SRC是目录:
如果DEST不存在:则创建指定目录,复制SRC目录中所有文件至DEST中;
如果DEST存在:
如果DEST是文件:报错
如果DEST是目录:
2、编辑/etc/group文件,添加组hadoop。
[root@www /]#echo "hadoop:x:1001" >>/etc/group
[root@www /]# cat /etc/group |grep hadoop
hadoop:x:1001
[root@www /]#
输出重定向:>
特性:覆盖输出
输出重定向:>>
特性:追加输出
3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。
[root@www home]# echo "hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash" >> /etc/passwd && tail -n 2 /etc/passwd
user:x:1000:1000:user:/home/user:/bin/bash
hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash
4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。
[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# chmod 700 -R /home/hadoop
[root@localhost ~]# ll -al /home/hadoop
总用量 12
drwx------. 3 root root 78 7月 15 19:38 .
drwxr-xr-x. 5 root root 52 7月 15 19:38 ..
-rwx------. 1 root root 18 7月 15 19:38 .bash_logout
-rwx------. 1 root root 193 7月 15 19:38 .bash_profile
-rwx------. 1 root root 231 7月 15 19:38 .bashrc
drwx------. 4 root root 39 7月 15 19:38 .mozilla
[root@localhost ~]#
5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。
[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop/
[root@localhost ~]# ll -a /home/hadoop
总用量 12
d-wx------. 3 hadoop hadoop 78 7月 15 19:38 .
drwxr-xr-x. 5 root root 52 7月 15 19:38 ..
-rwx------. 1 hadoop hadoop 18 7月 15 19:38 .bash_logout
-rwx------. 1 hadoop hadoop 193 7月 15 19:38 .bash_profile
-rwx------. 1 hadoop hadoop 231 7月 15 19:38 .bashrc
drwx------. 4 hadoop hadoop 39 7月 15 19:38 .mozilla
grep option 选项
- --color=auto:对匹配到的文本着色后高亮显示;
- -i:ignorecase,忽略字符的大小写
- -o:仅显示匹配到的字符串本身;
- -v, --invert-match:显示不能被模式匹配到的行;
- -E:支持使用扩展的正则表达式元字符;
- -q, --quiet, --silent:静默模式,即不输出任何信息;
- -A #:after, 后#行
- -B #:before,前#行
- -C #:context,前后各
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;
[root@localhost ~]# grep ^[sS] /proc/meminfo
SwapCached: 0 kB
SwapTotal: 1048572 kB
SwapFree: 1048572 kB
Shmem: 19756 kB
Slab: 134512 kB
SReclaimable: 69476 kB
SUnreclaim: 65036 kB
[root@localhost ~]# grep -i ^s /proc/meminfo
SwapCached: 0 kB
SwapTotal: 1048572 kB
SwapFree: 1048572 kB
Shmem: 19756 kB
Slab: 134512 kB
SReclaimable: 69476 kB
SUnreclaim: 65036 kB
7、显示/etc/passwd文件中其默认shell为非/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
yedong1308:x:1000:1000:yedong1308:/home/yedong1308:/bin/bash
hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash
8、显示/etc/passwd文件中其默认shell为/bin/bash的用户
[root@localhost ~]# grep "bin/bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
yedong1308:x:1000:1000:yedong1308:/home/yedong1308:/bin/bash
hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash
使用cut
[root@localhost ~]# grep "bin/bash" /etc/passwd | cut -d: -f1
root
yedong1308
hadoop
9、找出/etc/passwd文件中的一位数或两位数;
[root@localhost ~]# grep -E "\<[0-9]{1,2}\>" /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
\<:词首锚定;
\>:词尾锚定;
\{m,n\}:匹配其前面的字符至少m次,至多n次;
扩展表达式 -E
{m,n}:至少m次,至多n次;
10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;
[root@centos6 ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-642.3.1.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS rd_LVM_LV=vg_centos/lv_swap rd_NO_MD LANG=zh_CN.UTF-8 rd_LVM_LV=vg_centos/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=auto
initrd /initramfs-2.6.32-642.3.1.el6.x86_64.img
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS rd_LVM_LV=vg_centos/lv_swap rd_NO_MD LANG=zh_CN.UTF-8 rd_LVM_LV=vg_centos/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文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
[root@localhost rc.d]# grep "^#[[: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 rc.d]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 10.10.10.1:53 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 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
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[root@localhost rc.d]# 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:1002:1002::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin
使用()需要用\(\)转义
\1:引用第一组左括号里面的内容
使用扩展正则表达式
[root@localhost rc.d]# 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:1002:1002::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin
[root@localhost rc.d]#