Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 14 章 权限管理

用户、组、权限管理

目录

14.1. User 用户管理
14.1.1. 添加用户
14.1.2. 删除用户
14.1.3. 修改用户组
14.1.4. 修改 shell
14.1.5. 修改自己的登陆shell
14.1.6. 账号加锁与解锁
14.2. Group
14.2.1. Add a new group
14.2.2. Add a user to the group
14.2.3. /etc/group
14.2.4. gpasswd - administer /etc/group and /etc/gshadow
14.3. 访问权限
14.3.1. umask
14.3.2. chown - change file owner and group
14.3.3. chgrp - change group ownership
14.3.4. chmod - change file access permissions
14.4. chattr - change file attributes on a Linux second extended file system
14.5. su - run a shell with substitute user and group IDs
14.6. runuser - run a command with substitute user and group ID
14.7. sudo, sudoedit - execute a command as another user
14.7.1. /etc/sudoers
14.7.2. /etc/sudoers
14.7.3. 设置示例
14.7.4. NOPASSWD
14.7.5. 允许或禁止命令
14.7.6. Cmnd_Alias 用法
14.7.7. wheel 组
14.7.8. 注意事项
14.8. ACL - Access Control List
14.8.1. getfacl - get file access control lists
14.8.2. setfacl - set file access control lists

14.1. User 用户管理

14.1.1. 添加用户

		
$ adduser neo			
			
			
		
			
## 添加一个账号和root有一样的权限
useradd -o -u 0 -g 0  admin

## 指定家目录和shell
useradd -o -u 0 -g 0  -d /root -s /bin/bash admin

## 添加 root 用户并且设置密码
useradd -o -u 0 -g 0 admin
echo redhat | passwd admin --stdin
// 上面两条同下面一条
useradd -o -u 0 -g 0 -p $(openssl passwd -1 redhat@@neo) admin

## 添加普通用户并且设置密码
useradd -p $(openssl passwd -1 redhat@@admin) admin

##  添加普通用户指定其第二用户组
useradd -G root  -p $(openssl passwd -1 redhat@@admin) admin			
			
			
			

			
groupadd -g 80 www
adduser -o --uid 80 --gid 80 -G wheel -c "Web Application" www

PASSWORD=$(cat /dev/urandom | tr -dc [:alnum:] | head -c 32)
echo "www password: ${PASSWORD}"
echo www:${PASSWORD} | chpasswd
			
[root@localhost ~]# id www
uid=80(www) gid=80(www) groups=80(www),10(wheel)			
			
			

创建用户,使用已存在的组

			
[root@localhost ~]# grep docker /etc/group
docker:x:992:gitlab-runner
[root@localhost ~]# adduser -g 992 -c "Docker" docker
[root@localhost ~]# id docker
uid=1000(docker) gid=992(docker) groups=992(docker)
			
			

14.1.1.1. 创建系统账号

所谓系统账号就是没有 HOME 目录的账号

				
[root@localhost ~]# adduser -r netkiller
[root@localhost ~]# id netkiller
uid=990(netkiller) gid=988(netkiller) groups=988(netkiller)

[root@localhost ~]# grep netkiller /etc/group
netkiller:x:988:				

[root@localhost ~]# grep netkiller /etc/passwd
netkiller:x:990:988::/home/netkiller:/bin/bash
				
				

虽然 /etc/passwd 中有 /home/netkiller 我们去查看 /home 目录并没有 netkiller 文件夹

				
[root@localhost ~]# ls /home/
docker  gitlab-runner  www				
				
				

14.1.1.2. 修改用户名

		
usermod -l new_username old_username
groupmod -n newname	oldname
usermod -d /home/susan -m susan
		
				

14.1.2. 删除用户

remove an existed user, but keeping directory /home/neo

		
$ userdel neo			
			
			

delete user's directory under /home when removing an existed user

		
$ userdel -r neo
			
			

14.1.3. 修改用户组

usermod - modify a user account

		
usermod -G group -a user

[root@scientific ~]# groupadd vm
[root@scientific ~]# adduser xen
[root@scientific ~]# usermod -G vm -a xen
[root@scientific ~]# usermod -G vm -a kvm
[root@scientific ~]# id xen
uid=501(xen) gid=502(xen) groups=502(xen),501(vm)
			
			

将 www 加入 wheel 组,www 用户可以使用 sudo 命令

		
			
[root@localhost ~]# usermod -aG wheel www

[www@localhost ~]$ id www
uid=80(www) gid=80(www) groups=80(www),10(wheel)			
			
			
			

14.1.4. 修改 shell

修改 Shell 使用 usermod --shell /usr/bin/fish adm

			
[root@development ~]# grep adm /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin

[root@netkiller ~]# usermod --shell /usr/bin/fish adm

[root@netkiller ~]# grep adm /etc/passwd
adm:x:3:4:adm:/var/adm:/usr/bin/fish

[root@netkiller ~]# su - adm
error: can not save history
warning-path: Unable to locate data directory derived from $HOME: '/var/adm/.local/share/fish'.
warning-path: The error was 'Permission denied'.
warning-path: Please set $HOME to a directory where you have write access.

error: can not save universal variables or functions
warning-path: Unable to locate config directory derived from $HOME: '/var/adm/.config/fish'.
warning-path: The error was 'Permission denied'.
warning-path: Please set $HOME to a directory where you have write access.

mkdir: cannot create directory ‘/completions’: Permission denied
mkdir: cannot create directory ‘/conf.d’: Permission denied
mkdir: cannot create directory ‘/functions’: Permission denied
warning: An error occurred while redirecting file '/config.fish'
open: Permission denied
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish			
			
			

14.1.5. 修改自己的登陆shell

chsh - change your login shell

查看系统安装了na xx

			
[root@netkiller ~]# chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/usr/bin/fish
/bin/fish			
			
			
			
[root@netkiller ~]# chsh -s /bin/fish
			
			
			
[docker@netkiller root]$ chsh
Changing shell for docker.
New shell [/bin/bash]: /usr/bin/fish
Password: 
Shell changed.

[docker@netkiller root]$ exit
logout

[root@netkiller ~]# su - docker
Last login: Thu Sep 25 18:02:36 CST 2025 on pts/2
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish

docker@netkiller ~> ls -la
total 0
drwxr-xr-x  4 docker docker 35 Sep 25 18:04 ./
drwxr-xr-x. 3 root   root   20 Sep 25 18:04 ../
drwx------  3 docker docker 18 Sep 25 18:04 .config/
drwx------  3 docker docker 19 Sep 25 18:04 .local/
docker@netkiller ~> exit			
			
			

14.1.6. 账号加锁与解锁

lock / unlock

		
passwd -l neo
			
			
		
passwd -u neo
			
			

14.1.6.1. /etc/passwd

		
[root@netkiller ~]# cat /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
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:4294967294:4294967294:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
avahi-autoipd:x:100:102:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
neo:x:500:500::/home/neo:/bin/bash
mysql:x:501:501::/home/mysql:/bin/bash