管理用户和组(三)-修改用户,组和密码寿命

本文介绍了如何使用`usermod`命令管理Linux系统中的用户和组,包括锁定和解锁用户、更改组名和用户组、设置密码寿命以及调整账户过期日期。通过这些操作,可以有效地控制系统的安全性和访问权限。

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

这一节将用到 usermod 这个命令 

usermod --help

Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -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
  -m, --move-home               move contents of the home directory to the
                                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  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

 比如我们要锁住一个用户ali,用 -L 选项,之后我们再登录ali,就登录不上了

sudo usermod -L ali

解锁用户用 -U

sudo usermod -U ali
sudo groupadd marketing
sudo groupadd -r apps
cat /etc/group
sudo vi /etc/login.defs

上面的命令中,apps group 被使用-r (创建一个系统组)添加,使它的gid被限制在一个默认的范围里,这个范围可以通过查看login.defs里的SYS_GID_MIN 和 SYS_GID_MAX。所以在/etc/group里能看到这两个新建组的gid不是递增的.

groupmod -help

Usage: groupmod [options] GROUP

Options:
  -g, --gid GID                 change the group ID to GID
  -h, --help                    display this help message and exit
  -n, --new-name NEW_GROUP      change the name to NEW_GROUP
  -o, --non-unique              allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD       change the password to this (encrypted)
                                PASSWORD
  -R, --root CHROOT_DIR         directory to chroot into
sudo groupmod -n sales marketing

现在我们将创建后的marketing 改名为sales, 用-n选项。也可以用-g更改gid

sudo groupmod -g 5000 sales

现在将 用户ali 的 组 修改为 sales

sudo usermod -g sales ali
id ali

 现在将ali加进一个补充组,用 -G选项实现,ali将同时属于ITdep和sales。 

sudo groupadd ITdep
sudo usermod -G ITdep ali
id ali

 -G 和 -aG的区别是,-G 会 remove ITdep 然后 把ali加进 apps group,-aG则会保留原有的 supplementary group, 然后将 apps 加进 supplementary group list。用 id 命令就能清楚的看到区别。

sudo usermod -aG apps ali
id ali
sudo tail -n 1 /etc/shadow

上面命令可以看到一行被冒号分为9个区域的信息,表示了用户名:加密的密码:上一次密码修改在1970/1/1以来的天数:密码修改后最小的能再次修改的天数:密码修改后最多多少天可以再次修改密码:密码过期前几天通知用户:密码不可用期限:密码过期日:保留位。更详细的解释可以看这里

要查看这些信息:

sudo chage -l ali

要立刻更改ali的密码,使用-d 选项, -d指的是上一次更改密码的时间

sudo chage -d 0 ali

要求每90天更改一次密码:

sudo chage -M 90 ali

改完密码3天后才允许再次修改密码:

sudo chage -m 3 ali

 我们想让ali的账户在120天后过期,先看一下120天后是几号

date -d "+120 days"

然后chage -E

sudo chage -E 2019-11-06 ali

在这个过程你始终可以用chage -l 查看这些修改的信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值