1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;grep -E "^[[:space:]]+" /boot/grub2/grub.cfg
2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;grep -E "^#[[:space:]]+[^[:space:]]+" /etc/rc.d/rc.sysinit
3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;netstat -tan | grep -E "\<LISTEN\>[[:space:]]*"
[root@server1 mail]# netstat -tan | grep -E "\<LISTEN\>[[:space:]]*" 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 :::22 :::* LISTEN tcp6 0 0 ::1:631 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN [root@server1 mail]#
4、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;grep -E "^\<([^:]+)\>.+\1$" /etc/passwd
[root@server1 ~]# useradd bash [root@server1 ~]# useradd testbash [root@server1 ~]# useradd basher [root@server1 ~]# useradd -s /sbin/nologin nologin [root@server1 ~]# grep -E "^\<([^:]+)\>.+\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@server1 ~]#
5、显示当前系统上root、fedora或user1用户的默认shell;grep -E "(root|fedora|user1)" /etc/passwd | cut -d':' -f7
[root@server1 ~]# grep -E "(root|fedora|user1)" /etc/passwd | cut -d':' -f7 /bin/bash /sbin/nologin /bin/bash
6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();grep -E "[^[:space:]]+\(\)[[:space:]]+" /etc/rc.d/init.d/functions
[root@server1 ~]# grep -E "[^[:space:]]+\(\)[[:space:]]+" /etc/rc.d/init.d/functions checkpid() { __pids_var_run() { __pids_pidof() { daemon() { killproc() { pidfileofproc() { pidofproc() { status() { echo_success() { echo_failure() { echo_passed() { echo_warning() { update_boot_stage() { success() { failure() { passed() { warning() { action() { strstr() { is_ignored_file() { is_true() { is_false() { apply_sysctl() {
7、使用echo命令输出一个绝对路径,使用grep取出其基名;
[root@server1 ~]# echo /etc/sysconfig/network-scripts/ifcfg-eno16777736 | grep -E -o "[^/]*$"
ifcfg-eno16777736
扩展:取出其路径名echo /etc/sysconfig/network-scripts/ifcfg-eno16777736 | sed -r 's@[^/]+/?$@@'
[root@server1 ~]# echo /etc/sysconfig/network-scripts/ifcfg-eno16777736 | sed -r 's@[^/]+/?$@@' /etc/sysconfig/network-scripts/ [root@server1 ~]# [root@server1 ~]# echo /etc/sysconfig/network-scripts/ | sed -r 's@[^/]+/?$@@' /etc/sysconfig/ [root@server1 ~]#
8、找出ifconfig命令结果中的1-255之间数字;ifconfig | grep -E "\<[1-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-5]\>"
9、挑战题:写一个模式,能匹配合理的IP地址;ifconfig | grep -E -o "((2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\.){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)"
[root@server1 ~]# ifconfig | grep -E -o "((2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\.){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)" 192.168.2.106 255.255.255.0 192.168.2.255 127.0.0.1 255.0.0.0 192.168.122.1 255.255.255.0 192.168.122.255
10、挑战题:写一个模式,能匹配出所有的邮件地址;grep -E ".+@.+\.com$" mail.txt
11、查找/var目录下属主为root,且属组为mail的所有文件或目录;find /var -user root -a -group mail -ls
[root@server1 ~]# find /var -user root -a -group mail -ls 67151162 4 drwxrwxr-x 2 root mail 4096 Dec 24 23:14 /var/spool/mail 72124628 4 -rw------- 1 root mail 3583 Dec 24 23:13 /var/spool/mail/root
12、查找当前系统上没有属主或属组的文件;find / -nouser -o -nogroup
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录; find / \( -nouser -o -nogroup \) -atime -3 -ls
[root@server1 ~]# find / \( -nouser -o -nogroup \) -atime -3 -ls find: ‘/proc/3902/task/3902/fd/6’: No such file or directory find: ‘/proc/3902/task/3902/fdinfo/6’: No such file or directory find: ‘/proc/3902/fd/6’: No such file or directory find: ‘/proc/3902/fdinfo/6’: No such file or directory 1683202 0 drwx------ 3 4001 4001 74 Dec 5 08:43 /var/tmp/gentoo 36228709 0 drwxr-xr-x 4 4001 4001 37 Nov 14 06:44 /var/tmp/gentoo/.mozilla 71581776 0 drwxr-xr-x 2 4001 4001 6 Jun 9 2014 /var/tmp/gentoo/.mozilla/extensions 104016109 0 drwxr-xr-x 2 4001 4001 6 Jun 9 2014 /var/tmp/gentoo/.mozilla/plugins 1683222 0 drwx------ 3 4002 4002 74 Dec 5 08:48 /home/fedora 35731203 0 drwxr-xr-x 4 4002 4002 37 Nov 14 06:44 /home/fedora/.mozilla 71592655 0 drwxr-xr-x 2 4002 4002 6 Jun 9 2014 /home/fedora/.mozilla/extensions 102930554 0 drwxr-xr-x 2 4002 4002 6 Jun 9 2014 /home/fedora/.mozilla/plugins [root@server1 ~]#
13、查找/etc目录下所有用户都有写权限的文件;find /etc -perm -002
14、查找/etc目录下大于1M,且类型为普通文件的所有文件;find /etc -size +1M -a -type f -ls
[root@server1 ~]# find /etc -size +1M -a -type f -ls 35386472 5920 -r--r--r-- 1 root root 6060612 Oct 31 07:06 /etc/udev/hwdb.bin 2020639 3512 -rw-r--r-- 1 root root 3592412 Apr 7 2014 /etc/selinux/targeted/policy/policy.29 34862799 1336 -rw-r--r-- 1 root root 1367395 Jan 29 2014 /etc/brltty/zh-tw.ctb [root@server1 ~]#
15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;find /etc/init.d/ -perm -113 -ls
16、查找/usr目录下不属于root、bin或hadoop的文件;find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
[root@server1 ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls 34388291 4 drwx------ 2 polkitd root 4096 Oct 31 07:06 /usr/share/polkit-1/rules.d 100815145 12 -rwsr-sr-x 1 abrt abrt 11240 Mar 3 2014 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache [root@server1 ~]#
17、查找/etc/目录下至少有一类用户没有写权限的文件;find /etc/ -not -perm -222 -ls
18、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;"find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls
原创文章,作者:xiaopang304,如若转载,请注明出处:http://www.178linux.com/66087
评论列表(1条)
写的很好,提一个问题,255.255.255.255是一个合理的ip地址吗?