如何在不使用 useradd 或类似命令的情况下添加用户?

发布于 2024-12-11 05:54:19 字数 633 浏览 0 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临走之时 2024-12-18 05:54:19

您应该解释为什么要这样做。在我看来,这是一个坏主意。特别是,因为它不能很好地处理所有各种类型的系统(例如,某些Linux系统使用LDAP进行用户身份验证等)。

我相信你的行 echo "$user1: " >>> /etc/shadow 是错误的。查看(使用 sudo)/etc/shadow 文件的内容,您就会明白其中的条目不仅仅是用户名后跟冒号。

但实际上,您应该使用 useraddadduser 来执行此操作。你正冒着完全破坏你的系统的风险。

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.

蘑菇王子 2024-12-18 05:54:19

时应替换

echo "`passwd` $user1"

passwd $user1

输入第一个密码

。但除了这个问题之外,您还添加具有相同用户 ID 和组 ID 的所有新用户。因此,从技术上讲,不存在新用户,只有一个用户具有多个“别名”。您必须在编写 /etc/passwd/etc/group 时替换 500 才能解决此问题。

另一个大问题是,用户的新主目录和启动脚本不属于他,而是属于root。您可以在某处添加 chown -R $user1:$user1 /home/$homename 。

You should replace

echo "`passwd` $user1"

with

passwd $user1

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.

抠脚大汉 2024-12-18 05:54:19

你还应该有类似 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 the shadow file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文