3.2 linux用户管理 : 用户CRUD 【就是对/etc/passwd数据库的修改】

本文详细介绍了在Linux系统中如何进行用户管理,包括用户账号的创建、删除、修改等操作,以及如何设置用户权限、主目录和过期时间。通过具体的命令示例,如useradd、usermod、userdel和passwd,帮助读者掌握Linux用户管理的基本技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3.2 linux用户管理 : 用户CRUD 【就是对/etc/passwd数据库的修改】

思维导图

1.增加用户useradd adduser

$ useradd / adduser <userName> ##增加用户
$ passwd <userName>##设置密码
$ userdel <userName> ##删除用户 只有debian 的sudo 和RedHat 的wheel 的用户组才能执行
$ su <userName> ##切换用户
$ id <userName> ##查看指定用户的uid,gid,groupid

2.查看指定用户的uid,gid,groupid

$ id
uid=1000(sgx) gid=1000(sgx) groups=1000(sgx),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)

3.usermod 修改用户-修改home目录(-d)

-d 指定用户的home 目录
$ usermod user2 -d /home/user2
$ mkdir /home/user2
$ su user2 ##就会切入user2的目录下

3.usermod 修改用户-指定账户的过期时间(-e)

$ sudo usermod user2 -e 2017-10-10 ##指定过期时间
[sudo] password for sgx:
$ su user2
Password: 
Your account has expired(过期); please contact your system administrator
su: Authentication failure

3.usermod 修改用户-强行使用GROUP作为新的主group(-g)

$ groupadd testGroup ##新建group
$ sudo usermod user3 -g testGroup ##把user3指定为testGroup分组
$ su user3 ##切换
$ groups   ##查看分组
testGroup

3.usermod 修改用户-增加user一个的groups的list(-G)

$ usermod user3 -G sudo ## 给user3用户添加了sudo组
$ su user3
$ sudo reboot           ##可以执行sudo命令

3.usermod修改用户-指定登陆的名字(修改用户名)(-l)

user3@ubuntu:~$ sudo usermod user4 -l chao313 ##指定user4更改为chao313
user3@ubuntu:~$ su user4
No passwd entry for user 'user4'
user3@ubuntu:~$ su chao313                     ##只能是chao313登陆
Password:

4.usermod 命令详解

  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account   ##为用户新建一个home directorry
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE ##指定账户的过期时间,格式为 YYYY-MM-DD
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group       ##强行使用GROUP作为新的主group
  -G, --groups GROUPS           new list of supplementary(增加的) GROUPS   ##增加user一个的groups的list    
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name                ##指定登陆的名字(修改用户名)
  -L, --lock                    lock the user account                      ##lock account
  -m, --move-home               move contents of the home directory to the ##移动home文件夹到新的位置和-d一起使用
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password ##使用加密的密码作为新的密码
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remvoe range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remvoe range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

图形界面注意:
对于Linux图形界面,增加用户的时候必须要同时创建/home/<user_name> 并更换用户所属权
—> 否则没有权限登陆

#useradd <userName>
#passwd <userName>
#mkdir /home/<userName>
#chown <userName>:<userName> /home/<userName>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值