Linux中的权限修改指令及正则表达式

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

[root@centos6 ~]# cp -r /etc/skel/ /home/tuser1
[root@centos6 ~]# ls -la /home/tuser1
total 20
drwxr-xr-x. 2 root root 4096 Oct 23 18:20 .
drwxr-xr-x. 5 root root 4096 Oct 23 18:20 ..
-rw-r--r--. 1 root root   18 Oct 23 18:20 .bash_logout
-rw-r--r--. 1 root root  176 Oct 23 18:20 .bash_profile
-rw-r--r--. 1 root root  124 Oct 23 18:20 .bashrc
[root@centos6 ~]# chmod -R g-r,o-r /home/tuser1
[root@centos6 ~]# ls -la /home/tuser1/
total 20
d-wx--x--x. 2 root root 4096 Oct 23 18:20 .
drwxr-xr-x. 5 root root 4096 Oct 23 18:20 ..
-rw-------. 1 root root   18 Oct 23 18:20 .bash_logout
-rw-------. 1 root root  176 Oct 23 18:20 .bash_profile
-rw-------. 1 root root  124 Oct 23 18:20 .bashrc

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

[root@centos6 ~]# vim /etc/group
 root:x:0:
 bin:x:1: bin,daemon
 daemon:x:2:bin,daemon
 sys:x:3:bin,adm
 adm:x:4:adm,daemon
 tty:x:5:
 disk:x:6:
 lp:x:7:daemon
 mem:x:8:
...

编辑模式下输入G

...
oprofile:x:16:
slocate:x:21:
mariadb:x:498:
mageedu:x:5000:
fedora:x:501:

输入o,在光标所在行的下方新增一行,进入输入模式

...
fedora:x:501:

-- INSERT --

输入Hadoop:x:502:后,按esc退出至编辑模式,在编辑模式下输入:进入末行模式,键入wq,保存退出。

fedora:x:501:
hadoop:x:502:
:wq

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

[root@centos6 ~]# vim /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
...

在编辑模式下键入G,将光标移至最后行,键入o后在当前行下方新增一行,并进入输入模式

mariadb:x:498:498::/home/mariadb:/sbin/nologin
fedora:x:501:501::/users/fedora:/bin/bash

-- INSERT --

输入hadoop:x:502:502::/home/hadoop:/bin/bash

...
fedora:x:501:501::/users/fedora:/bin/bash
hadoop:x:502:502::/home/hadoop:/bin/bash

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

root@centos6 ~]# cp -r /etc/skel/ /home/hadoop/
[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop  502 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root 4096 Oct 23 18:20 ..
-rw-r--r--. 1 hadoop  502   18 Jul 18  2013 .bash_logout
-rw-r--r--. 1 hadoop  502  176 Jul 18  2013 .bash_profile
-rw-r--r--. 1 hadoop  502  124 Jul 18  2013 .bashrc
drwxr-xr-x. 2 root   root 4096 Oct 23 23:43 skel
[root@centos6 ~]# chmod -R g-r,o-r /home/hadoop/
[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop  502 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root 4096 Oct 23 18:20 ..
-rw-------. 1 hadoop  502   18 Jul 18  2013 .bash_logout
-rw-------. 1 hadoop  502  176 Jul 18  2013 .bash_profile
-rw-------. 1 hadoop  502  124 Jul 18  2013 .bashrc
drwx--x--x. 2 root   root 4096 Oct 23 23:43 skel

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

[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop  502 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root 4096 Oct 23 18:20 ..
-rw-------. 1 hadoop  502   18 Jul 18  2013 .bash_logout
-rw-------. 1 hadoop  502  176 Jul 18  2013 .bash_profile
-rw-------. 1 hadoop  502  124 Jul 18  2013 .bashrc
drwx--x--x. 2 root   root 4096 Oct 23 23:43 skel
[root@centos6 ~]# chown -R hadoop:hadoop /home/hadoop/
[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop hadoop 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root   4096 Oct 23 18:20 ..
-rw-------. 1 hadoop hadoop   18 Jul 18  2013 .bash_logout
-rw-------. 1 hadoop hadoop  176 Jul 18  2013 .bash_profile
-rw-------. 1 hadoop hadoop  124 Jul 18  2013 .bashrc
drwx--x--x. 2 hadoop hadoop 4096 Oct 23 23:43 skel

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

[root@centos6 ~]# cat /proc/meminfo | grep "^[S,s]"
SwapCached:            0 kB
SwapTotal:       2031608 kB
SwapFree:        2031608 kB
Shmem:               232 kB
Slab:              80628 kB
SReclaimable:      26364 kB
SUnreclaim:        54264 kB
[root@centos6 ~]# cat /proc/meminfo | grep -e "^[s|S]"
SwapCached:            0 kB
SwapTotal:       2031608 kB
SwapFree:        2031608 kB
Shmem:               232 kB
Slab:              80636 kB
SReclaimable:      26364 kB
SUnreclaim:        54272 kB
[root@centos6 ~]# cat /proc/meminfo | grep -i "^S"
SwapCached:            0 kB
SwapTotal:       2031608 kB
SwapFree:        2031608 kB
Shmem:               232 kB
Slab:              80624 kB
SReclaimable:      26364 kB
SUnreclaim:        54260 kB

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

[root@centos6 ~]# cat /etc/passwd | grep -v "/sbin/nologin$" |cut -d: -f1
root
sync
shutdown
halt
fedora
hadoop

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

[root@centos6 ~]# cat /etc/passwd | grep "/bin/bash$"|cut -d: -f1
root
fedora
hadoop

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

[root@centos6 ~]# cat /etc/passwd | grep -o "\<[[:digit:]][[:digit:]]\?\>"
0
0
1
1
2
2
...

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

[root@centos6 ~]# cat /boot/grub/grub.conf | grep "^[[:space:]]\+"
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_centos6-lv_root rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos6/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos6/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.el6.x86_64.img

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

[root@centos6 ~]# cat /etc/rc.d/rc.sysinit | grep "^#[[:space:]]\+[^[:space:]]\+"
# /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
...

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

[root@centos6 ~]# 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:56339               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 :::111                      :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 :::38177                    :::*                        LISTEN

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

[root@centos6 ~]# cat /etc/passwd | grep "^\(\<[[:alnum:]]\+\>\).*\1$"
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:503:503::/home/bash:/bin/bash
nologin:x:506:506::/home/nologin:/sbin/nologin

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

(0)
lucklymelucklyme
上一篇 2016-10-24 09:09
下一篇 2016-10-24 09:09

相关推荐

  • 从shell眼中看世界–展开和引用

    这篇博客的目的在于加深对于展开和引用的理解,阐释展开和引用之间联系。 ‘ ‘ :强引用,其中的变量引用不会被替换为变量值,而保持原字符串       ” “:弱引用,其中的变量引用会被替换为变量值    如果之前对于’ …

    Linux干货 2017-04-16
  • 2016/10/14作业

    操作类: 1、设置自己的终端提示符,要求终端登陆时:     a> 需要带颜色      b> 需要显示当前执行到了第几条命令      c> 显示当前登录终端,主机名和当前时间 首先我们要了解一个环境…

    Linux干货 2016-10-19
  • 开源跳板机Jumpserver v2.0.0 Beta版发布

    demo页面: http://demo.jumpserver.org admin admin 一. 更新Log — 登录脚本 — 1.1 使用paramiko原生ssh协议登录后端主机(原来版本使用pexpect模拟登录) 1.2 新增使用别名或备注登录 1.3 新增主机分组查看,使用更方便 1.4 多线…

    Linux干货 2015-04-20
  • shell脚本编程基础之二(if、case、for、while、until、continue、break语句使用)

    在shell脚本编程中,我们可以根据命令的状态结果,判断要不要执行下一步,但是有时候要判断的问题不止一个,甚至对问题本身都要做判断;同时问题的结果有时也不止一个,这时要借助简单的逻辑与和逻辑或,就显得很无力;要完成复杂的任务,需要借助一定的流程控制:顺序执行、选择执行、循环执行、同时在脚本执行过程中,有用户交互输入的需; if语句 case语句 for语句 …

    Linux干货 2016-08-21
  • Linux系统命令使用格式已经如何查看帮助

    在Linux 系统中有非常多的命令一般的命令格式:“COMMAND OPTIONS ARGUMENTS” 下面有一些基础命令可以体验下命令的基本格式用法 ifconfig命令    作用: 配置网络接口,查看网络接口信息    基本语法:     ifconfig  […

    Linux干货 2016-08-15
  • N21沉舟17周作业

    1、结合图形描述LVS的工作原理; NAT模型 NAT模型其实就是通过网络地址转换来实现负载均衡的,它的工作方式几乎跟iptables 中的DNAT一模一样的,NAT模型的工作方式: 1.用户请求VIP(也就是是CIP请求VIP) 2,Director Server 收到用户的请求后,发现源地址为CIP请求的目标地址为VIP,那么Dorector Serve…

    Linux干货 2016-11-14

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-27 13:02

    写的很棒! 加油!