马哥教育网络班21期+第5周课程练习

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

[root@centos ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf 
root (hd0,0)
kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-504.el6.x86_64.img

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

[root@centos ~]# 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).
# Update quotas if necessary
# Check to see if a full relabel is needed
# 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

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

[root@centos ~]# 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 0.0.0.0:56665               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 :::51608                    :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN

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

[root@centos ~]# grep "^\([[:alnum:]]\+\b\).*\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

5、显示当前系统上root、fedora或user1用户的默认shell;

[root@centos ~]# for i in root fedora user1; do grep "^$i" /etc/passwd | cut -d: -f1,7; done
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash

6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();

[root@centos ~]# grep "\b[[:alpha:]]\+\b()" /etc/rc.d/init.d/functions 
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

7、使用echo命令输出一个绝对路径,使用grep取出其基名;

[root@centos ~]# echo "/etc/sysconfig/network-scripts/ifcfg-eth0" | grep -E -o "[^/]+/?$" | cut -d"/" -f1
ifcfg-eth0

    扩展:取出其路径名

[root@centos ~]# echo "/etc/rc.d/rc.sysinit" | egrep -o  "^(/.*/)"
/etc/rc.d/

8、找出ifconfig命令结果中的1-255之间数字;

[root@centos ~]# ifconfig | egrep -o "\b`for i in {1..256};do echo $i; done;`" 
29
7
51
192
168
40
128
192
168

9、挑战题:写一个模式,能匹配合理的IP地址;

[root@centos ~]# ifconfig | egrep -o "[1-2][0-9]?[0-9]?.[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9]"
192.168.40.128
192.168.40.255
255.255.255.0
127.0.0.1
255.0.0.0

10、挑战题:写一个模式,能匹配出所有的邮件地址;

[root@centos ~]# cat mailaddress
#yuan@baidu.com
yuan_xin@baidu.com.cn
smith@top.com__
3982294699@qq.com
xinixn@sina.com
[root@centos ~]# cat mailaddress | egrep  "^[[:alnum:]].*@[[:alnum:]]+[.].*[[:alnum:]]$"
yuan_xin@baidu.com.cn
3982294699@qq.com
xinixn@sina.com

11、查找/var目录下属主为root,且属组为mail的所有文件或目录;

[root@centos ~]# find /var/ -user root -a -group mail 
/var/spool/mail

12、查找当前系统上没有属主或属组的文件;

[root@centos ~]# find / -nouser -o -nogroup -type f
find: `/proc/3658/task/3658/fd/5': No such file or directory
find: `/proc/3658/task/3658/fd/5': No such file or directory
find: `/proc/3658/task/3658/fdinfo/5': No such file or directory
find: `/proc/3658/task/3658/fdinfo/5': No such file or directory
find: `/proc/3658/fd/5': No such file or directory
find: `/proc/3658/fd/5': No such file or directory
find: `/proc/3658/fdinfo/5': No such file or directory
find: `/proc/3658/fdinfo/5': No such file or directory

     进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;

[root@centos ~]# find / -nouser -o -nogroup  -a -atime -3
find: `/proc/2535/task/2535/fd/5': No such file or directory
find: `/proc/2535/task/2535/fd/5': No such file or directory
find: `/proc/2535/task/2535/fdinfo/5': No such file or directory
find: `/proc/2535/task/2535/fdinfo/5': No such file or directory
find: `/proc/2535/fd/5': No such file or directory
find: `/proc/2535/fd/5': No such file or directory
find: `/proc/2535/fdinfo/5': No such file or directory
find: `/proc/2535/fdinfo/5': No such file or directory

13、查找/etc目录下所有用户都有写权限的文件;

[root@centos ~]# find /etc -perm -222 -exec ls -l {} \;
lrwxrwxrwx. 1 root root 10 Jun  2 18:13 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 14 Jun  2 18:13 /etc/system-release -> centos-release
lrwxrwxrwx. 1 root root 19 Jun  2 18:25 /etc/pam.d/fingerprint-auth -> fingerprint-auth-ac
lrwxrwxrwx. 1 root root 25 Jun  2 18:14 /etc/pam.d/smtp -> /etc/alternatives/mta-pam
lrwxrwxrwx. 1 root root 14 Jun  2 18:25 /etc/pam.d/system-auth -> system-auth-ac
lrwxrwxrwx. 1 root root 16 Jun  2 18:25 /etc/pam.d/password-auth -> password-auth-ac
lrwxrwxrwx. 1 root root 17 Jun  2 18:25 /etc/pam.d/smartcard-auth -> smartcard-auth-ac
lrwxrwxrwx. 1 root root 10 Jun  2 18:13 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Jun  2 18:13 /etc/rc6.d -> rc.d/rc6.d

14、查找/etc目录下大于1M,且类型为普通文件的所有文件;

[root@centos ~]# find /etc/ -type f -size +1M 
/etc/selinux/targeted/modules/active/policy.kern
/etc/selinux/targeted/policy/policy.24
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@centos ~]# find /etc/ -type f -size +1M -exec ls -lh {} \;
-rw-r--r--. 1 root root 7.8M Jun  2 18:17 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 7.8M Jun  2 18:17 /etc/selinux/targeted/policy/policy.24
-rw-r--r--. 1 root root 2.2M Jun  2 18:21 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml

15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;

[root@centos ~]# find /etc/init.d/ -perm -113 -exec ls -lh {} \;

16、查找/usr目录下不属于root、bin或hadoop的文件;

[root@centos ~]# find /usr/ -not \( -user root -o -user bin -o -user hadoop \)

17、查找/etc/目录下至少有一类用户没有写权限的文件;

[root@centos ~]# find /etc/  -not -perm -222 -ls | less
260098   12 drwxr-xr-x 118 root     root        12288 Jul 23 10:27 /etc/
260242    4 -rw-r--r--   1 root     root          801 Jul 19  2011 /etc/gssapi_mech.conf
260103    4 drwxr-xr-x   2 root     root         4096 Jun  2 18:19 /etc/modprobe.d
260106    4 -rw-r--r--   1 root     root           52 Jun  2 18:06 /etc/modprobe.d/anaconda.conf
260348    4 -rw-r--r--   1 root     root          884 Oct 16  2014 /etc/modprobe.d/blacklist.conf
260345    4 -rw-r--r--   1 root     root          382 Oct 15  2014 /etc/modprobe.d/dist-alsa.conf
260347    8 -rw-r--r--   1 root     root         5596 Oct 15  2014 /etc/modprobe.d/dist.conf
273795    4 -rw-r--r--   1 root     root           30 Oct 10  2009 /etc/modprobe.d/openfwwf.conf
260346    4 -rw-r--r--   1 root     root          473 Oct 15  2014 /etc/modprobe.d/dist-oss.conf
264283    4 -rw-r--r--   1 root     root          524 Oct 16  2014 /etc/auto.misc
260162    4 -rw-r--r--   1 root     root          272 Nov 18  2009 /etc/mailcap

18、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;

[root@centos ~]# find /etc/  -mtime -7 -a  -not \( -user root -o -user hadoop\)

原创文章,作者:Snoo,如若转载,请注明出处:http://www.178linux.com/25230

(0)
SnooSnoo
上一篇 2016-07-29
下一篇 2016-07-29

相关推荐

  • 深入Php底层,用c为php编写拓展

    1.前言              随着lamp/lnmp架构的流行,Php语言越来越得到广泛的使用。php语言在表现层有着非常优异的表现,部署方便,开发迅速。但Php语言也有着天生短板以及局限性—-对多线程以及多进程的支持不甚如意,以及…

    Linux干货 2016-10-29
  • N22-妙手-第九周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;: #!/bin/bash # declare -i numberOfLoginUser=0 declare -i numberOfUnloginUs…

    Linux干货 2016-10-19
  • linux挂载的基本使用

    挂载   挂载是指将一个设备(通常是存储设备)挂接到一个已存在的目录上。 我们要访问存储设备中的文件,必须将文件所在的分区(已有文件系统)挂载到一个已存在的目录上, 然后通过访问这个目录来访问存储设备。 挂载条件 1、挂载点必须是一个目录。 2、一个分区挂载在一个已存在的目录上,这个目录可以不为空,但挂载后这个目录下以前的内容将隐藏不可用。对于其他…

    Linux干货 2016-09-07
  • gawk 语法介绍及其实例

    gawk 语法介绍及其实例  §·awk介绍 Linux文本处理三剑客:grep  sed 和 awk 。其中grep是一种文本过滤工具,sed是文本行编辑器,而awk是一种报表生成器,就是对文件进行格式化处理的,这里的格式化不是文件系统的格式化,而是对文件的内容进行的各种排版,进而格式化显示。 在linux之上我们使用awk是GUN a…

    Linux干货 2016-09-22
  • 马哥教育网络班第21期+第三周课程作业

    1. 列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 who | awk  '{print $1}'|uniq 2.列出最后登录到当前系统的用户的相关信息 last | head -1   3. 读取当前系统上被用户当做其默认shell最多的那个shell cat /etc/pa…

    Linux干货 2016-07-26
  • 认识shell

    一、认识shell 什么是shell?shell为单词外壳的意思。那么这是谁的外壳?我们知道一个系统中实际工作的是那些硬件,cpu、内存、磁盘等。我们如何调用这些硬件为我们工作?实际上,硬件是由内核kernel控制的。我们可以通过kernel控制硬件,但我们不能直接和内核kernel交流。我们需要一个外壳,这个外壳就是shell来沟通kernel。何为she…

    Linux干货 2015-09-22

评论列表(1条)

  • 马哥教育
    马哥教育 2016-07-29 15:48

    写的很好,排版也很棒,第8题不是我们要的结果,在想象,用grep做。加油