bash sudo 和 chpasswd 在一条线上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10192223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:03:05  来源:igfitidea点击:

sudo and chpasswd on one line

bashsshpasswordssudo

提问by user1338373

I need connect to server via sshand change users' passwords without typing sshor sudo password.

我需要通过连接到服务器ssh并更改用户密码而无需键入sshsudo password

I found some example but I cannot complete one command.

我找到了一些示例,但我无法完成一个命令。

I found this command for ssh connection without typing password and running sudocommand for delete user:

我发现此命令用于 ssh 连接,而无需输入密码并运行sudo删除用户的命令:

sshpass -p $admin_password ssh -t $admin@$server "echo $admin_password | sudo -S /usr/sbin/userdel -r $usr"

then I found command for change users password:

然后我找到了更改用户密码的命令:

echo "$usr:$password" | sudo -S /usr/sbin/chpasswd;

Finaly, I want something like this:

最后,我想要这样的东西:

sshpass -p $admin_password ssh -t $admin@$server "echo $admin_password | sudo -S echo "$usr:$password" | sudo -S /usr/sbin/chpasswd;"

Do you have any ideas?

你有什么想法?

回答by linuxnut

Your question is actually going to require 2 things. The command to change a password remotely for a user:

你的问题实际上需要两件事。为用户远程更改密码的命令:

ssh remoteserver 'echo -e "passwdofuser\npasswdofuser" | passwd username'

In order to do that you probably need to be root, or in the sudo config, have "passwd" NOT require a password for your user to run.

为了做到这一点,您可能需要成为 root,或者在 sudo 配置中,让“passwd”不需要密码才能让您的用户运行。

visudo

Edit the file:

编辑文件:

Cmnd_Alias      MYPASSWD = /usr/bin/passwd
yourusername ALL = NOPASSWD: MYPASSWD

That should allow you to ssh in and not have to use a password to run the passwd command.

这应该允许您 ssh 进入,而不必使用密码来运行 passwd 命令。