马哥教育网络班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 15:24
下一篇 2016-07-29 15:25

相关推荐

  • DNS和BIND配置(第二部分)

    接第一部分课后练习 三、课后练习: 2、配置反向解析: 步骤一:写主配置文件:改成与上个实验相同,也可以自己写一个简单的配置文件,如下: [root@localhost tmp]# cat named.conf.new options { directory  "/var/named&qu…

    Linux干货 2016-10-17
  • mysql配置详解-备份-主从-MHA

    目录: 1.备份和恢复 2.主从复制 3.主主复制 4.半同步复制 5.proxysql_读写分离 6.MHA 1.备份和恢复 ·mysqldump 备份: mysqldump -E -R –triggers –master-data=2 –flush-logs –single-transaction –dat…

    Linux干货 2017-08-08
  • ​第五周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@localhost ~]# grep  -E "^[[:space:]]+" /boot/grub/grub.conf  root (hd0,0) kernel&nbs…

    Linux干货 2017-02-05
  • 编程真难啊

    上周,在Sun的Java论坛上出现了一个这样的帖子,这个贴子的链接如下:http://forums.sun.com/thread.jspa?threadID=5404590&start=0&tstart=0 LZ的贴子翻译如下: 大家好,我是一个Java的新手,我有一个简单的问题:请问我怎么才能反转一个整数的符号啊。比如把-12转成+12。是…

    Linux干货 2015-04-03
  • 简单的文本处理工具和正则表达式

    一、文本工具     在linux中,有很多优秀且功能强大的文本处理工具,对文件内容进行查找、替换、删除、排序等操作,是linux进行文本处理变得特别方便。日常工作中,经常会用文本处理进行日志分析,文本抽取等,所以掌握文本处理,将会对我们的工作起到极大的作用。 cat:查看文件    &n…

    Linux干货 2016-08-10
  • 十三.Linux博客-2016年8月18日while、for特殊用法、selet循环与菜单、函数

    格式说明: 操作 概念 命令 说明及举例 十三.while、for特殊用法、selet循环与菜单、函数 while特殊用法 while循环的特殊用法(遍历文件的每一行): while read line; do 循环体 done < /PATH/FROM/SOMEFILE 依次读取/PATH/FROM/…

    Linux干货 2016-08-24

评论列表(1条)

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

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