linux系统创建用户、调整权限及用户和用户组id(centos 9 stream)

本文详细介绍了在CentOS9系统中创建用户、设置密码、调整用户ID和用户组ID,以及如何赋予管理员权限的过程,包括使用adduser命令和SELinux用户映射的注意事项。

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

本测试环境为centos9 stream,一般条件下linux系统创建及id调整与本文实例一致。

创建及设置用户是需要使用root账户或者具有sudo权限的用户。

useadd(adduser)命令参数参考

[root@n2 munge]# adduser
Usage: adduser [options] LOGIN
       adduser -D
       adduser -D [options]

Options:
      --badname                 do not check for bad names //不检查用户名是否合规
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account //新建用户的基础home目录,就是用户登陆时自动加载的目录位置
      --btrfs-subvolume-home    use BTRFS subvolume for home directory
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account //用户个人目录,登陆时进入的指定目录,一般会 ~表示,使用pwd可查看详细路径
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account //创建用户时指定用户所属用户组,在多用户分组时很好,不创建新的用户组
  -G, --groups GROUPS           list of supplementary groups of the new
                                account //添加用户时创建新用户组,并且将此用户加入到指定用户组
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID  //允许创建用户时用户名不唯一,这个一般不这样弄了。
  -p, --password PASSWORD       encrypted password of the new account //创建用户时设置密码
  -r, --system                  create a system account //创建系统用户
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account //指定用户id
  -U, --user-group              create a group with the same name as the user //指定用户组名称
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping //设置用户为selinux安全组用户。



[root@n2 munge]# adduser
Usage: adduser [options] LOGIN
       adduser -D
       adduser -D [options]

Options:
      --badname                 do not check for bad names
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
      --btrfs-subvolume-home    use BTRFS subvolume for home directory
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

 系统的help信息已经比较全了,简单翻译一下,不懂英文的就网络翻译一下吧。

1、创建用户

###创建用户名为zhangshan的用户,默认这样创建会自动顺序配置uid,gid,home目录等,因此有可能出现uid与gid不相等情况。
useradd zhangsan
#默认情况下会自动创建zhangshan用户对应的用户组
id zhangshan

[root@n2 munge]# id zhangshan
uid=1000(zhangshan) gid=1000(zhangshan) groups=1000(zhangshan)

###在特殊情况下我们只需要创建系统用户,不需要登陆,不需要建立用户home目录
useradd -s /sbin/nologin slurm

###指定uid编号
useradd -u 1000 zhangshan

###指定用户目录,默认在/home/zhangshan
useradd -d /home/zhangshan zhangshan

###指定用户组,-g使用现有用户组,-G创建zhangshan用户组并将zhangshan用户加入用户组
useradd -g root zhangshan
useradd -G root zhangshan


###比较完整的创建,一般不需要这样指定。
useradd -s /bin/bash -m -d /home/zhangshan -g zhangshan zhangshan


###批量创建用户
newusers usersfilename.txt

##usersfilename.txt内容
#loginname:password:uid:gid:comment:home_dir:shell

zhangshan:zhangshanmima2234:1020:1001:Zhangshan Yonghu:/home/zhangshan:/bin/bash
lisi:lisidemima:1021:1001:Lisi Yonghu:/home/lisi:/bin/bash
wangwu:wangwu2234:1022:1001:Wangwu Yonghu:/home/wangwu:/bin/bash

2、设置密码

password zhangshan


Changing password for user zhangshan.
New UNIX password:             //在这里输入新密码
Retype new UNIX password:       //再次输入新密码
passwd: all authentication tokens updated successfully.

直接输入两次相同密码即可。

3、用户权限调整

让普通用户具有管理员所有权限

###方法1
vim /etc/sudoers
root        ALL=(ALL)       ALL
zhangshan   ALL=(ALL)       ALL

###方法2,将用户组设置为root,并将/etc/sudoers中的%wheel 这行去掉注释
usermod -g root zhangshan

vim /etc/sudoers
%wheel ALL=(ALL) ALL

#此方法会改变用户用户组,个人不建议

###修改/etc/passwd文件,将指定用户id设置为0
#个人不推荐。

4、调整用户ID和用户组ID

在linux集群中为保证不同集群中各节点系统用户权限保持一致,将所有节点相同服务的用户名uid及gid设置为一致非常重要,尤其是在不同节点共享数据目录时,各节点服务uid和gid的不同会导致部分节点服务不可用。

###普通用户调整uid及gid
usermod -u 2898 zhangshan

groupmod -g 2898 zhangshan
usermod -g 2898 zhangshan

chown -R zhangshan:zhangshan /home/zhangshan

###调整系统用户mungeid为1899
#如为系统用户,要先停止用户相关服务进程
systemctl stop munge
#修改munge用户id为1899
usermod -u 1899 munge
#修改munge用户用户组id为1899
groupmod -g 1899 munge
usermod -g 1899 munge
#查看munge用户id
id munge

uid=1899(munge) gid=1899(munge) groups=1899(munge)

###默认情况下linux按id授权目录权限,调整id后需要重新设置相关目录的使用者,否则会造成访问权限问题。
chown -R munge:munge /var/log/munge
chown -R munge:munge /etc/munge
chown -R munge:munge /var/run/munge
chown -R munge:munge /var/lib/munge


###同样使用相同命令调整slurm用户id及用户组id,并重新设置目录的用户所有者。
usermod -u 1898 slurm
groupmod -g 1898 slurm
usermod -g 1898 slurm
id slurm
chown -R slurm:slurm /var/log/slurm
chown -R slurm:slurm /etc/slurm
chown -R slurm:slurm /var/run/slurm
chown -R slurm:slurm /var/spool/slurm

5、删除用户

不详细解释了,userdel 后面接指定用户名即可删除

[root@n2 munge]# userdel
Usage: userdel [options] LOGIN

Options:
  -f, --force                   force some actions that would fail otherwise
                                e.g. removal of user still logged in
                                or files, even if not owned by the user   //强制删除,不管用户当前是否登陆或在使用文件
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool //一并删除用户的个人目录
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -Z, --selinux-user            remove any SELinux user mapping for the user

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小果运维

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值