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

相关推荐

  • Linux基础知识之WMware Tools的安装

    该博文以CentOS6.8_x86_64系统为基础,tty终端登录CentOS6.8系统,以root身份登录系统。 为什么要安装WMware Tools?                    VMware Tools是VMware虚拟机中自带的一种增强工具,…

    Linux干货 2016-07-29
  • 源码包编译安装mariaDB

    前言     MySQL是一个关系型数据库管理系统,是最流行的关系型数据库管理系统,由于其体积小、速度快、总体拥有成本低,并且之前是完全开源,所以大受欢迎。但由于后面MySQL卖给了SUN,随后SUN被Oracle收购,虽然也有开源免费版本,但是很多功能都需要另外购买商业版本,导致现在MySQL使用份额逐渐减少。所以M…

    Linux干货 2016-02-28
  • Linux 磁盘、文件系统管理

    Linux 磁盘、文件系统管理                                               &nb…

    Linux干货 2016-09-01
  • Linux下chomd修改文件或目录权限

    文件或目录权限分为三个字段 以/tmp为例ll后可看到权限为:     drwxrwxrwt 第一d为文件类型 Linux下的文件类型     -:普通文件     d:目录文件     b:块设备  &nbs…

    Linux干货 2016-02-28
  • linux磁盘管理及文件系统介绍

        硬盘是计算机必备组成部分,现在的硬盘分固态硬盘和传统机械硬盘,这里要介绍的是传统机械硬盘。     一.硬盘最基本结构         1.硬盘是由坚硬金属材料制成的涂以磁性介质的盘片,不同容量硬盘的盘片数不等。…

    Linux干货 2016-06-22
  • 数据库的历史及常见基本功能

    数据库的基本知识,MariaDB的基本知识,SQL语句

    2018-01-29

评论列表(1条)

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

    写的很棒! 加油!