Chapter 4——Linux文件权限和正则表达式

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

[root@localhost ~]# cp -r /etc/skel /home/tuser1
[root@localhost ~]# chmod -R g=,o= /home/tuser1
[root@localhost ~]# ll -a /home/tuser1
total 12
drw-------. 2 root root  77 Aug 27 21:32 .
drwxr-xr-x. 3 root root  19 Aug 27 21:31 ..
-rw-------. 1 root root  18 Aug 27 21:32 .bash_logout
-rw-------. 1 root root 193 Aug 27 21:32 .bash_profile
-rw-------. 1 root root 231 Aug 27 21:32 .bashrc
-rw-------. 1 root root   0 Aug 27 21:32 example.txt

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

[root@localhost ~]# echo "hadoop:x:1200:" >> /etc/group
[root@localhost ~]# cat /etc/group | grep hadoop
hadoop:x:1200:

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

[root@localhost ~]# echo "hadoop:x:1200:1200:hadoop:/home/hadoop:/bin/bash" >> /etc/passwd
[root@localhost ~]# id hadoop
uid=1200(hadoop) gid=1200(hadoop) groups=1200(hadoop)

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

[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# chmod -R g=,o= /home/hadoop
[root@localhost ~]# ll -a /home/hadoop
total 12
drwx------. 2 root root  59 Aug 27 21:49 .
drwxr-xr-x. 5 root root  45 Aug 27 21:49 ..
-rw-------. 1 root root  18 Aug 27 21:49 .bash_logout
-rw-------. 1 root root 193 Aug 27 21:49 .bash_profile
-rw-------. 1 root root 231 Aug 27 21:49 .bashrc

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

[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
[root@localhost ~]# ll -a /home/hadoop
total 12
drwx------. 2 hadoop hadoop  59 Aug 27 21:49 .
drwxr-xr-x. 5 root   root    45 Aug 27 21:49 ..
-rw-------. 1 hadoop hadoop  18 Aug 27 21:49 .bash_logout
-rw-------. 1 hadoop hadoop 193 Aug 27 21:49 .bash_profile
-rw-------. 1 hadoop hadoop 231 Aug 27 21:49 .bashrc

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

方式一:

[root@localhost ~]# cat /proc/meminfo | grep '^[sS]'
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              8872 kB
Slab:             115136 kB
SReclaimable:      47348 kB
SUnreclaim:        67788 kB

方式二:

[root@localhost ~]# grep -e '^s' -e '^S' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              8872 kB
Slab:             115136 kB
SReclaimable:      47348 kB
SUnreclaim:        67788 kB

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

[root@localhost ~]# grep -v '/sbin/nologin$' /etc/passwd 
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
zhaoxl:x:1000:1000::/home/zhaoxl:/bin/bash
hadoop:x:1200:1200:hadoop:/home/hadoop:/bin/bash

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

[root@localhost ~]# grep '/bin/bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
zhaoxl:x:1000:1000::/home/zhaoxl:/bin/bash
hadoop:x:1200:1200:hadoop:/home/hadoop:/bin/bash

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

[root@localhost ~]# egrep '\<[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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

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

因CentOS7上/boot/grub/grub.conf已升级为/boot/grub2/grub.cfg,故以此做演示:

[root@localhost ~]# egrep '^[[:space:]]+' /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
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  d9a55bc5-6b72-4ddd-bb69-e00fc1eb909d
    else
      search --no-floppy --fs-uuid --set=root d9a55bc5-6b72-4ddd-bb69-e00fc1eb909d
    fi
    linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-3.10.0-327.el7.x86_64.img
    load_video
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  d9a55bc5-6b72-4ddd-bb69-e00fc1eb909d
    else
      search --no-floppy --fs-uuid --set=root d9a55bc5-6b72-4ddd-bb69-e00fc1eb909d
    fi
    linux16 /vmlinuz-0-rescue-69699ac5960f4959b4a987a2d1013faf root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet
    initrd16 /initramfs-0-rescue-69699ac5960f4959b4a987a2d1013faf.img
  source ${config_directory}/custom.cfg
  source $prefix/custom.cfg;

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

[root@localhost ~]# egrep '^#[[:space:]]+[^[:space:]]' /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.

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

[root@localhost ~]# netstat -tan | egrep 'LISTEN[[:space:]]*$'
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN 

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

[root@localhost ~]# egrep '(^[a-z]*):.*\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:1201:1201::/home/bash:/bin/bash
nologin:x:1204:1204::/home/nologin:/sbin/nologin
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容