用户及文件权限常用命令(一)

1.用户身份

关于用户及用户口令的各类信息解释可以参考我的文章Linux下/etc/passwd与/etc/shadow配置文件详解

1.1 id

显示用户的详细信息。id 用户名

[root@hostname /]# id root
uid=0(root) gid=0(root) groups=0(root)

1.2 useradd

创建新的用户账户。useradd [参数] 用户名

[root@hostname /]# useradd -d /home/a_close -u 1002 -s /sbin/nologin a_close
[root@hostname /]# id a_close
uid=1002(a_close) gid=1002(a_close) groups=1002(a_close)

#创建用户a_close -d:指定目录  -u:指定UID -s:指定Shell解释器 /sbin/nologin禁止登录,默认bash解释器为/bin/bash

1.3 groupadd

创建新的用户组。groupadd [参数] 群组名

[root@hostname /]# groupadd a_close_group

1.4 usermod

修改用户属性。usermod [参数] 用户名

[root@hostname /]# id a_close
uid=1002(a_close) gid=1002(a_close) groups=1002(a_close)

#将a_close的扩展组改为root用户组,-G
[root@hostname /]# usermod -G root a_close
[root@hostname /]# id a_close
uid=1002(a_close) gid=1002(a_close) groups=1002(a_close),0(root)

#更改a_close的UID,-u
[root@hostname /]# usermod -u 1001 a_close
[root@hostname /]# id a_close
uid=1001(a_close) gid=1002(a_close) groups=1002(a_close),0(root)

#将a_close的解释器从/sbin/nologin更改为/bin/bash,-s
[root@hostname /]# su a_close
This account is currently not available.
[root@hostname /]# usermod -s /bin/bash a_close
[root@hostname /]# su a_close
[a_close@hostname /]$ 

1.5 passwd

修改用户密码、过期时间等信息。passwd [参数] 用户名

#修改自己
[root@hostname /]# passwd
Changing password for user root.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

#root改a_close
[root@hostname /]# passwd a_close


#root锁定用户a_close,-l
[root@hostname /]# passwd -l a_close
#root解锁用户a_close,-u
[root@hostname /]# passwd -u a_close

1.6 userdel

删除用户账户。userdel [参数] 用户名

#删除用户a_close
[root@hostname ~]# userdel a_close
[root@hostname ~]# id a_close
id: ‘a_close’: no such user

#删除a_close所在目录数据 /home/a_close
[root@hostname ~]# cd /home
[root@hostname home]# ls
a_close  hostname
[root@hostname home]# rm -rf a_close
[root@hostname home]# ls
hostname
[root@hostname home]# 

2.文件权限

可读、可写与可执行对应命令在文件和目录上的区别
可读、可写与可执行对应命令在文件和目录上的区别
可读、可写与可执行的字符与数字表示。731:rwx-wx–x
可读、可写与可执行的字符与数字表示

[linuxprobe@linuxprobe ~]$ ls -l a.txt
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Jun  8 16:07 a.txt

#文件类型:普通文件(-),还有目录文件(d)、链接文件(l)、管道文件(p)、块设备文件(b)、字符设备文件(c)
#所有者(linuxprobe)权限:rw-
#所属组(linuxprobe)权限:rw-
#其他用户权限:r--

3.文件的特殊权限

3.1 SUID

对二进制程序进行设置的特殊权限,让二进制程序的执行者临时拥有所有者权限。

[linuxprobe@hostname ~]$ ls -l /etc/shadow 
----------. 1 root root 1317 May 23 19:43 /etc/shadow
[linuxprobe@hostname ~]$ ls -l /bin/passwd
-rwsr-xr-x. 1 root root 34512 Aug 13  2018 /bin/passwd

#查看passwd命令属性发现所有者权限(rws),即该文件被赋予SUID特殊权限,即普通用户也可以将变更信息写入/etc/shadow中。

3.2 SGID

1.对二进制程序进行设置的特殊权限,让二进制程序的执行者临时拥有所属组权限。

[linuxprobe@hostname ~]$ ls -l /bin/ps
-rwxr-xr-x. 1 root root 141240 Aug 13  2018 /bin/ps
[root@hostname home]# chmod g+s /bin/ps
[root@hostname home]# ls -l /bin/ps
-rwxr-sr-x. 1 root root 141240 Aug 13  2018 /bin/ps

#​给ps命令增加SGID权限(r-s)

2.在某个目录中创建的文件自动继承该目录的用户组(仅可对目录设置)。以部门共享目录为例,给该目录设置SGID特殊权限后,部门内任何人在里面创建的文件都会属于该目录的所属组,而不是自己的基本用户组。

#在/tmp文件夹下新增文件夹testdir,设置好目录的777及SGID权限
[root@hostname /]# cd /tmp
[root@hostname tmp]# mkdir testdir
[root@hostname tmp]# ls -ald testdir
drwxr-xr-x. 2 root root 6 Jun 19 09:36 testdir
[root@hostname tmp]# chmod -R 777 testdir
[root@hostname tmp]# chmod -R g+s testdir
[root@hostname tmp]# ls -ald testdir
drwxrwsrwx. 2 root root 6 Jun 19 09:36 testdir


#切换普通用户linuxprobe,在目录testdir中新创建文件test,新创建的文件test即会继承testdir所在所属组名称root
[root@hostname tmp]# su linuxprobe
[linuxprobe@hostname tmp]$ cd /tmp/testdir
[linuxprobe@hostname testdir]$ echo "hello" >test
[linuxprobe@hostname testdir]$ ls -ald test
-rw-rw-r--. 1 linuxprobe root 6 Jun 19 09:43 test

3.2.1 chomd

用于设置文件的一般权限及特殊权限(-R 对文件目录递归操作)。chmod [参数] 文件名

[root@hostname ~]# ls -l anaconda-ks.cfg 
-rw-------. 1 root root 1385 Apr 12 00:42 anaconda-ks.cfg
[root@hostname ~]# chmod 760 anaconda-ks.cfg 
[root@hostname ~]# ls -l anaconda-ks.cfg 
-rwxrw----. 1 root root 1385 Apr 12 00:42 anaconda-ks.cfg

3.2.2 chown

用于设置文件的所有者和所有组(-R 对文件目录递归操作)。

[root@linuxprobe ~]# chown linuxprobe:linuxprobe anaconda-ks.cfg 
[root@linuxprobe ~]# ls -l anaconda-ks.cfg 
-rwxrw----. 1 linuxprobe linuxprobe 1385 Apr 12 00:42 anaconda-ks.cfg

3.3 SBIT

对某个目录设置SBIT权限,该目录中的文件仅能被其所有者执行删除。

#/tmp文件夹默认为SBIT权限,切换到普通用户linuxprobe,且普通用户对testdir文件夹rwx均开,但不可删除/tmp下的testdir
[linuxprobe@linuxprobe tmp]$ ls -ald /tmp
drwxrwxrwt. 17 root root 4096 Jun 19 10:19 /tmp
[linuxprobe@linuxprobe tmp]$ ls -ald testdir
drwxrwsrwx. 2 root root 6 Jun 19 10:23 testdir
[linuxprobe@linuxprobe tmp]$ rm -rf testdir
rm: cannot remove 'testdir': Operation not permitted

3.4 特殊权限设置

参数 作用
u+s 设置SUID权限
u-s 取消SUID权限
g+s 设置SGID权限
g-s 取消SGID权限
o+t 设置SBIT权限
o-t 取消SBIT权限

操作上可以利用数字一键设置权限。SUID、SGID、SBIT对应权限4、2、1。
权限的字符表示转数字表示

参考文献

《Linux就该这么学》

猜你喜欢

转载自blog.csdn.net/weixin_47505548/article/details/131283103