如何在不使用 useradd 或类似命令的情况下添加用户?
如何在 Linux bash 脚本上添加用户而不使用 useradd
或类似命令。
还要复制位于 /etc/skel/
中的启动脚本,并更改已添加的用户的密码。
user1=$1
read -p "Enter your home name" home_name
read -p "Enter your login shell" loginshell
echo "$user1:x:500:500:$user1:/home/$home_name:$loginshell" >> /etc/passwd
echo "$user1:x:500:" >> /etc/group
mkdir /home/$home_name
chmod 744 /home/$home_name
cp -pr /etc/skel/.bashrc /home/$home_name
echo "$user1: " >> /etc/shadow
echo "`passwd` $user1"
执行此脚本后出现错误
passwd: Authentication token manipulation error
请您建议我是否有任何错误?
How to add user on linux bash script with out using useradd
or similar command.
Also copy the startup script which located in /etc/skel/
, and change password for the user which you have been added.
user1=$1
read -p "Enter your home name" home_name
read -p "Enter your login shell" loginshell
echo "$user1:x:500:500:$user1:/home/$home_name:$loginshell" >> /etc/passwd
echo "$user1:x:500:" >> /etc/group
mkdir /home/$home_name
chmod 744 /home/$home_name
cp -pr /etc/skel/.bashrc /home/$home_name
echo "$user1: " >> /etc/shadow
echo "`passwd` $user1"
The error i have got it after execute this script
passwd: Authentication token manipulation error
Please could you advice me if there any mistakes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该解释为什么要这样做。在我看来,这是一个坏主意。特别是,因为它不能很好地处理所有各种类型的系统(例如,某些Linux系统使用LDAP进行用户身份验证等)。
我相信你的行
echo "$user1: " >>> /etc/shadow
是错误的。查看(使用 sudo)/etc/shadow 文件的内容,您就会明白其中的条目不仅仅是用户名后跟冒号。但实际上,您应该使用 useradd 或 adduser 来执行此操作。你正冒着完全破坏你的系统的风险。
You should explain why you want to do that. In my opnion, it is a bad idea. In particular, because it does not handle well all the various kind of systems (for instance, some Linux system use LDAP for user authentification, etc).
And I believe that your line
echo "$user1: " >> /etc/shadow
is wrong. Look (with sudo) at the content of the /etc/shadow file, and you'll understand that entries inside are more than just a username followed by a colon.But really, you should use useradd or adduser to do that. You are risking to break your system entirely.
时应替换
为
输入第一个密码
。但除了这个问题之外,您还添加具有相同用户 ID 和组 ID 的所有新用户。因此,从技术上讲,不存在新用户,只有一个用户具有多个“别名”。您必须在编写
/etc/passwd
和/etc/group
时替换500
才能解决此问题。另一个大问题是,用户的新主目录和启动脚本不属于他,而是属于root。您可以在某处添加 chown -R $user1:$user1 /home/$homename 。
You should replace
with
for entering the first password.
But besides this problem you add all new users with the same user-id and group-id. So there are technically no new users but one user with several "aliases". You have to replace the
500
when writing/etc/passwd
and/etc/group
to fix that.Another big problem is, that the user's new home directory and the startup script do not belong to him but to root. You may add a
chown -R $user1:$user1 /home/$homename
somewhere.你还应该有类似
echo "$user1: " >> /etc/gshadow
用于您正在创建的组。与您对用户和shadow
文件所做的操作相同。you should also have something like
echo "$user1: " >> /etc/gshadow
for the group that you are creating. Same as what you have done for the user and theshadow
file.