grep的用法

grep的用法

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

cp -R /etc/skel /home/thuser1 #复制文件/etc/skel 到/home/目录下并改名为thuser1
chmod -R 700 /home/thuser1 #更改/home/thuser1目录权限为属主全部权限,属组和其他用户则没有任何权限
[root@localhost ~]# cp -R /etc/skel /home/thuser1 
[root@localhost ~]# ls /home/thuser1 -ld
drwxr-xr-x. 3 root root 78 Jul 23 04:40 /home/thuser1
[root@localhost ~]# chmod -R 700 /home/thuser1 
[root@localhost ~]# ls /home/thuser1 -ld
drwx------. 3 root root 78 Jul 23 04:40 /home/thuser1
[root@localhost ~]#

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

echo hadoop:x:300 >> /etc/group #将显示的字符串叠加导入到/etc/passwd里
[root@localhost ~]# echo hadoop:x:300 >> /etc/group
[root@localhost ~]# 
[root@localhost ~]# tail -1 /etc/group
hadoop:x:300
[root@localhost ~]#

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

echo hadoop:x:300:300:/home/hadoop:bin/bash >> /etc/passwd
[root@localhost ~]# echo hadoop:x:300:300:/home/hadoop:bin/bash >> /etc/passwd
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# tail -1 /etc/passwd
hadoop:x:300:300:/home/hadoop:bin/bash
[root@localhost ~]#

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

cp -R /etc/skel /home/hadoop #复制文件/etc/skel 到/home/目录下并改名为hadoop
chmod 700 /home/hadoop/ #更改/home/hadoop目录权限为属主全部权限,属组和其他用户则没有任何权限
[root@localhost ~]# cp -R /etc/skel /home/hadoop
[root@localhost ~]# ls /home/hadoop/ -ld
drwxr-xr-x. 4 root root 90 Jul 23 05:05 /home/hadoop/
[root@localhost ~]#
[root@localhost ~]# chmod 700 /home/hadoop/
[root@localhost ~]# ls /home/hadoop/ -ld
drwx------. 4 hadoop hadoop 90 Jul 23 05:05 /home/hadoop/
[root@localhost ~]#

5、修改/home/hadoop目录及其它内部所有文件的属主问hadoop,属组为hadoop。

chown hadoop:hadoop /home/hadoop #修改/home/hadoop目录属主属组为hadoop
[root@localhost ~]# chown hadoop:hadoop /home/hadoop
[root@localhost ~]# ls /home/hadoop/ -ld
drwxr-xr-x. 4 hadoop hadoop 90 Jul 23 05:05 /home/hadoop/
[root@localhost ~]#

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

Method 1

cat /proc/meminfo |grep ‘^S’ #查看/proc/meminfo文件,并搜索以大写‘S’开头的行
[root@localhost ~]# cat /proc/meminfo | grep '^S'
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             10568 kB
Slab:             113956 kB
SReclaimable:      55908 kB
SUnreclaim:        58048 kB
[root@localhost ~]#

Method 2

grep -E ‘^(S)’ /proc/meminfo
[root@localhost ~]# grep -E '^(S)' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             10568 kB
Slab:             113956 kB
SReclaimable:      55908 kB
SUnreclaim:        58048 kB
[root@localhost ~]#

7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

grep -v ‘/sbin/nologin’ /etc/passwd |cut -d’:’ -f1 #显示非/sbin/nologin 的行,再搜索一‘:’为分隔符的第一个字段
[root@localhost ~]# grep -v '/sbin/nologin' /etc/passwd |cut -d':' -f1
root
sync
shutdown
halt
danry
hadoop
[root@localhost ~]#

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

[root@localhost ~]# grep '/bin/bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
danry:x:1000:1000:danry:/home/danry:/bin/bash
[root@localhost ~]#

9、找出/etc/passwd文件中的一位数或两位数;

[root@localhost ~]# grep -E "\<[0-9]{1,2}\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost ~]# ^C

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

[root@localhost ~]# grep '^ ' /boot/grub2/grub.cfg
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
  set timeout_style=menu
  set timeout=5
  set timeout=5
  source ${prefix}/user.cfg
  if [ -n "${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root ${GRUB2_PASSWORD}
  fi
  source ${config_directory}/custom.cfg
  source $prefix/custom.cfg;
[root@localhost ~]#

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

[root@localhost ~]# grep -E '^#[[:space:]]+[^spance:]+' /etc/rc.d/rc.local 
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
[root@localhost ~]#

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

[root@localhost ~]# netstat -tan |grep "LISTEN"' '*$
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
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 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
[root@localhost ~]#

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

[root@localhost ~]# grep -E "^(\<[[:alnum:]]+\>).*\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:3007:3008::/home/bash:/bin/bash
nologin:x:3010:3011::/home/nologin:/sbin/nologin
[root@localhost ~]# grep "^\([[:alnum:]]\+\>\).*\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:3007:3008::/home/bash:/bin/bash
nologin:x:3010:3011::/home/nologin:/sbin/nologin

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

(1)
N27_DanryN27_Danry
上一篇 2017-07-23
下一篇 2017-07-23

相关推荐

  • 建立dns服务

    一 、环境 两台机器,一台LINUX,机器名为LINUX,域名为tongqiang.cnIP地址为192.168.0.5,另一台机器为WIN98,机器名为tongqiang,IP地址从LINUX上DHCP服务器上获得。 二 、建立DNS服务器(假设你的DNS软件包已经安装好了,而且是在安装LINUX是选择安装DNS服务器的) 建立DNS服务器只需要修改以下几…

    2017-10-09
  • Linux第四周总结

    1、复制/etc/skel目录为/home/tuser1, 要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop, 其基本组ID为hadoop组的id号;其家目录为/home/hadoop。 4、复制/etc/…

    2017-07-24
  • HSRP vs VRRP

    HSRP:(Hot Standby Router Protocol)-热备份路由协议 是cisco平台一种特有的技术,是cisco的私有协议。 VRRP:(Virtual Router Redundancy Protocol)-虚拟路由冗余协议 是国际标准,由IETF提出的解决局域网中配置静态网关出现单点失效现象的路由协议。 ----------------…

    Linux干货 2016-10-19
  • Linux包管理:rpm/yum/编译安装

    rpm:安装,查询,升级,校验,卸载   Topic: 程序包 rpm管理 yum管理   程序包:   什么是程序包管理器: 将编译好的应用程序的各个组成文件打包成一个或几个程序包文件,从而更方便地实现程序包的安装、升级、卸载和查询等管理操作   程序包的组成清单: 文件清单 安装或卸载时运行的脚本 数据库(公共) …

    2017-09-14
  • Linux 下openssl搭建一个CA

    CA : 证书颁发机构PKI : 公钥基础设施,公钥基础构架证书: 里面存放了用户的各种信息,最核心的部分就是公钥 谁来给CA颁发公钥,解决方法是,CA自己给自己颁发公钥 openssl 是一个强大的工具可以帮助我们来创建颁发证书,下面我们就来看下在linux下 opensssl 搭建一个CA,并实现管理 1、首先我们需要给CA生成一个私钥  cd…

    2017-07-16
  • N22-第六周作业

    1、复制/etc/rc.d/init.d/functions文件至/tmp目录,将/tmp/functions文件中 的以至少一个空白字符开头的行的行首加# ~]# cp /etc/rc.d/init.d/functions /tmp~]# vim /tmp/functions %s@^[[:space:]]\+@#@g 2、复制/boot/grub/gru…

    Linux干货 2016-10-17

评论列表(1条)

  • 马哥教育
    马哥教育 2017-07-27 00:11

    注意下第五题、第十一题,答题还需要再仔细,继续加油~