【Linux】问题解决:centos7系统不能创建纯数字用户名(useradd: invalid user name ‘123123‘))

文章讲述了在LinuxCentOS系统中遇到尝试创建纯数字用户名时的报错问题,原因是从7.6版本开始这种操作被弃用。通过查阅文档和RedHat的Bug报告,了解到可以通过设置环境变量SHADOW_ALLOW_ALL_NUMERIC_USER为1来临时或永久允许创建此类用户。文章提供了设置环境变量的方法,并警告在RHEL8中将完全移除对这种用户的支持。

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

👨‍🎓博主简介

  🏅云计算领域优质创作者
  🏅华为云开发者社区专家博主
  🏅阿里云开发者社区专家博主
💊交流社区:运维交流社区 欢迎大家的加入!
🐋 希望大家多多支持,我们一起进步!😄
🎉如果文章对你有帮助的话,欢迎 点赞 👍🏻 评论 💬 收藏 ⭐️ 加关注+💗


报错数据

[root@k8s-master ~]# useradd 123123
useradd: invalid user name '123123'

报错原因

是因为linux系统用户的uid就是纯数字,所以被限制不能创建纯数字用户名

解决思路

其实想了一下,有这样的限制也是有道理的,毕竟linux系统用户的uid就是纯数字的

比如像id 之类的命令,是可以直接指定uid或者用户名的,如果是纯数字了,到底应该视为什么呢?

不过测试过,冲突了还是只显示用户名为纯数字的用户信息,总之只会显示一个用户的信息

查询了redhat相关的文档,其中redhat 7.6 版本的Release_Notes已经说明了,如下:

All-numeric user and group names in shadow-utils are now deprecated
Creating user and group names consisting purely of numeric characters using the useradd and
groupadd commands is now deprecated and will be removed from the system with the next major
release. Such names can potentially confuse many tools that work with user and group names and user
and group ids (which are numbers).

也就是说,从7.6版本开始弃用创建纯数字的用户名;
但是7.6--7.9版本之间可以使用环境变量来创建的;
但是从8版本会直接删除那种使用命令创建纯数字用户名的方式,使用环境变量也不能来创建。

后来在Red Hat Bugzilla – Bug 1672958 文档中也发现了此解释,如下

The useradd and groupadd commands disallow user and group names
consisting purely of numeric characters since Red Hat Enterprise Linux 7.6.
The reason for not allowing such names is that this can confuse potentially
many tools that work with user and group names and user and group ids
(which are numbers). However due to some deployments depending on allowing all-numeric user names
this erratum makes useradd and groupadd commands to allow all-numeric user and group names
if environment variable SHADOW_ALLOW_ALL_NUMERIC_USER is set.
Please note that the all-numeric user and group names are
deprecated in Red Hat Enterprise Linux 7 and the support will be completely removed in Red Hat Enterprise Linux 8.

解决方法(适用于centos 7.6-7.9)

但是终究还是有方法改变这个限制的,可以使用export定义SHADOW_ALLOW_ALL_NUMERIC_USER变量的值为1,如下:

#设置临时环境变量
export SHADOW_ALLOW_ALL_NUMERIC_USER=1

#创建纯数字用户
useradd 123123
useradd -g test -s /sbin/nologin 123123

#查看最后3个用户
tail -n 3 /etc/passwd
  • 设置永久生效
echo "export SHADOW_ALLOW_ALL_NUMERIC_USER=1" >> /root/.bashrc

这样,每次连接该ip的时候都会自动生效了。

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

A-刘晨阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值