马哥教育网络班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

相关推荐

  • shell编程及rpm包

    1. shell编程    case 实现条件判断, 无论是case后面还是值后面都是没有任何标点符号的。    case支持通配符, 如果想要表示其他的话, 可以使用*放到最后。 用*表示其他。   2. 用tar打包的文件或则目录,这些文件或则目录的的ACL(访问控制列表)权限会丢失。…

    Linux干货 2016-08-18
  • Linux文件权限之facl

    1、什么是facl         facl(file access control list)文件访问控制列表,主要目的是在原有的u,g,o之外,另一层让普通用户能控制赋权给另外的用户或组的机制;       &nbs…

    Linux干货 2016-09-19
  • 常用linux命令小计(1)

    关于linux那些事儿—常用命令小结(1) 系统相关信息 Uname –m显示处理器的架构 Uname –r显示正在使用的内核版本 cat /proc/cpuinfo显示cpu的具体信息 cat /proc/version显示内核的版本 cat /proc/swaps显示哪些分区被使用 data显示系统日期 cal –y显示当年的日历 date 021920…

    Linux干货 2017-02-19
  • free命令的介绍及参数的详细分析

           通过对free命令的深入学习,我觉得有必要整理一个文档,供自己和小伙伴们今后的复习使用。   free 命令的详细剖析      参数有:-b/k/m/g    分别以bytes/kilobytes/megabyte…

    2017-07-17
  • Lvm基本应用,扩展及缩减实现

    什么是LVM LVM是Logical Volume Manager的简写。其是Linux环境下对磁盘分区进行管理的一种机制。LVM由Heinz Mauelshagen在Linux 2.4内核上实现。其主要作用是在不损坏磁盘数据的情况下对磁盘空间进行增加,缩减。LVM的做法是将几块物理硬盘通过软件的方式组合成一块空间相对大的Volume Group,简称VG,…

    Linux干货 2016-09-08
  • 马哥教育网络班21期+第二周课程练习

    Linux上的文件管理类命令都有哪些?其常用的方法及其相关示例。     cp:将源文件或者目录复制到目标文件或者目录中。     mv:将文件或目录重新命名,或者将源文件从源目录移到目标目录中。     rm:删除指定的一个文件或者多个文件或者目录。       &n…

    Linux干货 2016-07-16

评论列表(1条)

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

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