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

相关推荐

  • 第六周作业

    一、Vim编辑器练习: 用Vim编辑器做下面的练习,我的Linux系统(CentOS 6.9)安装了精简版系统,没有Vim编辑器。 a、设置Yum源为网易开源镜像站。 b、选择对应“镜像名”,到对应的“CentOS使用说明”里面查看内容 c、看着CentOS说明操作就可以了 d、使用说明 首先备份/etc/yum.repos.d/CentOS-Base.re…

    Linux干货 2017-08-22
  • 实例;编译安装apache 2.2,并启动此服务

    基于c语言的源代码编译安装,通常有三个步骤     ./configure         (1)通过选项传递参数,指定启用特性,安装路径等:执行时会参考用户的指定以及makefile.in文件生成makefile.  &nbsp…

    Linux干货 2016-08-24
  • 马哥教育网络班20期+第二周课程练习

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。 3、请使用命令行展开功能来完成以下练习:    (1)、创建/tmp目录下的:a_c, a_d, b_c, b_d   …

    Linux干货 2016-06-23
  • 为什么新建文件时,所在文件系统还有大量剩余空间,却提示此文件系统空间已满??

    一:答疑解惑:    这是因为该文件所在分区的inode号被用尽,虽然分区的剩余容量还有很多。 二:基本原理:   inode为何物block又为何物?以及,inode都有哪些特征?   ①我们知道,在linux文件系统中,文件名只是计算机用来给人看,而计算机本身只识别文件所对应的数字或编号,而这个编号或数字…

    2017-07-22
  • CentOS7下编译安装Apache

    在Linux中通过编译源代码进行安装软件,需要GCC(GNU Compiler Collection,GNU编译器套件)的支持。通过yum安装GCC [root@localhost ~]# yum install gcc C程序源码编译安装三个步骤: 第一步:./configure  (1)通过选项传递参数,…

    Linux干货 2016-09-05
  • 集群-ipvsadm和NAT实验(3)

    纠正:报文进入内核空间后,当到达input链时发现是一个集群服务时,则直接发送到postrouting链,不经过forward链。 调度算法: 1、静态方法: rr:轮询,即依照次序从所有RS中进行挑选 wrr:加权轮询,按照权重在RS中进行轮询 sh:source hashing,源地址哈希,即对来自相同客户端的请求发送至同一RS,这样会破坏负载均衡效果。…

    Linux干货 2015-12-15

评论列表(1条)

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

    写的很棒! 加油!