2016全新Linux+Python高端运维班-Linux grep命令及基本正则表达式

简介:

本周作业内容:

1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
     第一种方式:
         [root@liu /] #chmod -R go=--- /home/tuser1
         [root@liu tuser1] # ls -l /home/tuser1/
         总用量 4
         -rw-------. 1 root root 47 8月  25 00:01 issue
     第二种方式:
         [root@liu /] # rm -rf /home/tuser1/
         [root@liu /] # cp -r /etc/skel/ /home/tuser1
         [root@liu /] # chmod -R g-rwx /home/tuser1/
         [root@liu /] # chmod -R o-rwx /home/tuser1/
         [root@liu /] # ls -l /home/tuser1/
         总用量 4
         -rw-------. 1 root root 47 8月  25 00:12 issue
         [root@liu /] # ls -ld /home/tuser1/
         drwx------. 4 root root 4096 8月  25 00:12  /home/tuser1/


2、编辑/etc/group文件,添加组hadoop。

1
2
3
4
5
6
7
     第一种方式:
         [root@liu /] # echo "hadoop:x:3008:" >> /etc/group
     第二种方式:
         vim  /etc/group
         在最下面添加一行:
         hadoop:x:3009:
         保存退出。


3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。 

1
2
3
4
5
6
vim  /etc/passwd 
     在最下面添加一行:
     hadoop:x:3008:3008:: /home/hadoop : /bin/bash
     保存退出。
     [root@liu /] # id hadoop
     uid=3008(hadoop) gid=3008(hadoop) 组=3008(hadoop)

    
4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

1
2
3
4
5
6
7
8
9
10
11
12
  [root@liu /] # cp -r /etc/skel/ /home/hadoop
     [root@liu /] # chmod -R go=--- /home/hadoop
     [root@liu /] # ls -la /home/hadoop
     总用量 32
     drwx------.  4 root root 4096 8月  25 00:33 .
     drwxr-xr-x. 19 root root 4096 8月  25 00:33 ..
     -rw-------.  1 root root   18 8月  25 00:33 .bash_logout
     -rw-------.  1 root root  176 8月  25 00:33 .bash_profile
     -rw-------.  1 root root  124 8月  25 00:33 .bashrc
     drwx------.  2 root root 4096 8月  25 00:33 .gnome2
     -rw-------.  1 root root   47 8月  25 00:33 issue
     drwx------.  4 root root 4096 8月  25 00:33 .mozilla


5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。
知识点
        修改文件的属主:chown
            chown [OPTION]... [OWNER][:[GROUP]] FILE...

                用法:
                    OWNER
                    OWNER:GROUP
                    :GROUP

                    Note: 命令中的冒号可用.替换;

                -R: 递归
            chown [OPTION]... --reference=RFILE FILE...

1
2
3
     [root@liu /] # chown -R hadoop:hadoop /home/hadoop/
     [root@liu /] # id hadoop
     uid=3008(hadoop) gid=3008(hadoop) 组=3008(hadoop)

    
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     [root@liu /] # grep '^[Ss]' /proc/meminfo
     SwapCached:            0 kB
     SwapTotal:       2097144 kB
     SwapFree:        2097144 kB
     Shmem:              3296 kB
     Slab:              46576 kB
     SReclaimable:      13148 kB
     SUnreclaim:        33428 kB
     [root@liu /] # grep -i "^s" /proc/meminfo
     SwapCached:            0 kB
     SwapTotal:       2097144 kB
     SwapFree:        2097144 kB
     Shmem:              3296 kB
     Slab:              46584 kB
     SReclaimable:      13160 kB
     SUnreclaim:        33424 kB


7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
知识点:
        grep [OPTIONS] PATTERN [FILE...]

            选项:
                --color=auto: 对匹配到的文本着色显示;
                -v: 显示不能够被pattern匹配到的行;
                -i: 忽略字符大小写;
                -o: 仅显示匹配到的字符串;
                -q: 静默模式,不输出任何信息;
                -A #:after, 后#行
                -B #: before, 前#行
                -C #:context, 前后各#行

                -E:使用ERE;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     [root@liu /] # grep -v "/sbin/nologin" /etc/passwd | cut -d: -f1
     root
     sync
     shutdown
     halt
     liu
     bash
     basher
     testbash
     centos
     useradd1
     mageia
     slackware
     openstacks
     user1
     user2
     user3
     hadoop


8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
     [root@liu /] # grep  "/bin/bash" /etc/passwd | cut -d: -f1
     root
     liu
     bash
     basher
     testbash
     centos
     useradd1
     mageia
     openstacks
     user1
     user2
     user3
     hadoop


9、找出/etc/passwd文件中的一位数或两位数;
知识点:
        基本正则表达式元字符:
            字符匹配:
                .:  匹配任意单个字符;
                []: 匹配指定范围内的任意单个字符
                [^]:匹配指定范围外的任意单个字符
                    [:digit:]、[:lower:]、[:upper:]、[:alpha:]、[:alnum:]、[:punct:]、[:space:]
            
            匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数;
                *:匹配前面的字符任意次;
                    例如: grep "x*y" 
                        abxy
                        xay
                        xxxxxxy

                    贪婪模式
                .*:任意长度的任意字符;
                \?:匹配其前面的字符0或1次;即前面的可有可无;
                \+:匹配其前面的字符至少1次;
                \{m\}:匹配前面的字符m次;
                \{m,n\}:匹配前面的字符至少m次,至多n次;
                    \{0,n\}:匹配前面的字符至多n次;
                    \{m,\}:匹配前面的字符至少m次;

            位置锚定:
                ^:行首锚定;用于模式的最左侧;
                $:行尾锚定;用于模式的最右侧;
                ^PATTERN$: 用于模式匹配整行;
                    ^$: 空行;
                    ^[[:space:]]*$

                \< 或 \b:词首锚定;用于单词模式的左侧;
                \> 或 \b:词尾锚定;用于单词模式的右侧;
                \<PATTERN\>:匹配整个单词;

            分组:
                \(\):将一个或多个字符捆绑在一起,当作一个整体进行处理;
                    \(xy\)*ab

1
     [root@liu /] # grep -E -o "\<[0-9]{1,2}\>" /etc/passwd


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

1
2
3
4
5
6
7
8
     [root@liu /] # grep "^[[:space:]]\+" /boot/grub/grub.conf
             root (hd0,0)
   
           kernel  /vmlinuz-2 .6.32-431.el6.i686 ro 
root=UUID=3fcabd0f-e9fb-4618-918d-c9e004fe6a59 rd_NO_LUKS  
KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 
rd_NO_LVM rd_NO_DM rhgb quiet
             initrd  /initramfs-2 .6.32-431.el6.i686.img


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

1
2
3
4
5
6
7
8
9
10
11
12
13
     [root@liu /] # 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.
     ...

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

1
2
3
4
5
6
7
8
9
10
11
12
13
     [root@liu /] # netstat -tan | grep "LISTEN[[:space:]]\+$"
     tcp        0      0 0.0.0.0:111                 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
     tcp        0      0 0.0.0.0:33661               0.0.0.0:*                   LISTEN
     tcp        0      0 :::36996                    :::*                        LISTEN
     tcp        0      0 :::111                      :::*                        LISTEN
     tcp        0      0 :::22                       :::*                        LISTEN
     tcp        0      0 ::1:631                     :::*                        LISTEN
     tcp        0      0 ::1:25                      :::*                        LISTEN
     或者使用 egrep 命令
     netstat  -tan | egrep  "LISTEN[[:space:]]+$"

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

1
2
3
4
5
6
7
8
9
10
     [root@liu /] # useradd bash
     [root@liu /] # useradd testbash
     [root@liu /] # useradd basher
     [root@liu /] # useradd nologin -s /sbin/nologin
     [root@liu /] # grep "^\(\<[[:alpha:]]\+\>\).*\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:501:501:: /home/bash : /bin/bash
     nologin:x:504:504:: /home/nologin : /sbin/nologin


本文转自 ljohnmail 51CTO博客,原文链接:http://blog.51cto.com/ljohn/1843661,如需转载请自行联系原作者
相关文章
|
3月前
|
人工智能 运维 Kubernetes
别再手动敲命令了!运维自动化才是打工人的“自救之道”
别再手动敲命令了!运维自动化才是打工人的“自救之道”
92 8
|
6月前
|
数据采集 监控 数据安全/隐私保护
Python正则表达式:用"模式密码"解锁复杂字符串
正则表达式是处理字符串的强大工具,本文以Python的`re`模块为核心,详细解析其原理与应用。从基础语法如字符类、量词到进阶技巧如贪婪匹配与预定义字符集,结合日志分析、数据清洗及网络爬虫等实战场景,展示正则表达式的强大功能。同时探讨性能优化策略(如预编译)和常见错误解决方案,帮助开发者高效掌握这一“瑞士军刀”。最后提醒,合理使用正则表达式,避免过度复杂化,追求简洁优雅的代码风格。
155 0
|
12月前
|
安全 网络安全 文件存储
思科设备巡检命令Python脚本大集合
【10月更文挑战第18天】
389 1
思科设备巡检命令Python脚本大集合
|
7月前
|
弹性计算 人工智能 运维
摆脱繁琐命令-让运维更加流畅-阿里云ECS操作系统控制台运维篇
阿里云操作系统控制台提供了便捷的服务器监控与管理功能,简化了运维工作。通过将多台服务器纳入统一监控平台,用户可以快速查看CPU、内存、磁盘和网络等关键资源的使用情况,避免了逐一远程连接查询的繁琐操作。此外,该工具支持自动化数据汇总,极大地方便了日报、周报和月报的编写。测试过程中,系统展示了良好的稳定性和响应速度,尤其在网络抖动和大文件健康状态测试中表现出色。整体体验流畅,显著提升了运维效率。 操作系统控制台地址:[点击访问](https://alinux.console.aliyun.com/)
196 26
摆脱繁琐命令-让运维更加流畅-阿里云ECS操作系统控制台运维篇
|
9月前
|
运维 Shell 数据库
Python执行Shell命令并获取结果:深入解析与实战
通过以上内容,开发者可以在实际项目中灵活应用Python执行Shell命令,实现各种自动化任务,提高开发和运维效率。
249 20
|
10月前
|
分布式计算 MaxCompute 对象存储
|
9月前
|
安全 Shell 数据处理
使用Python执行Shell命令并获取结果
在实际应用中,可以根据需要选择适当的参数和方法来执行Shell命令,并处理可能出现的各种情况。无论是系统管理、自动化任务还是数据处理,掌握这些技巧都将极大地提高工作效率。
302 12
|
9月前
|
运维
阿里云服务器批量执行命令(系统运维管理oos)
阿里云【系统运维管理oos】批量执行详情
185 5
|
11月前
|
缓存 监控 Linux
Python 实时获取Linux服务器信息
Python 实时获取Linux服务器信息
|
12月前
|
运维 监控 网络协议

热门文章

最新文章