linux 命令行修改密码_如何在一个命令行中更改Linux用户的密码

linux 命令行修改密码

In Linux, we use passwd to change password, but passwd requires input from stdin to get the new password. It is common case that we may want to change the password non-interactively, such as creating new user accounts and change or set password for these accounts on a number of Linux boxes when the new user creating can be done by one command line. With the help of pipe and a little tricky, we can change user’s password in one command line. This will save much time especially when creating a batch of user accounts.

Linux中 ,我们使用passwd更改密码,但是passwd需要stdin的输入才能获得新密码。 通常情况下,当可以通过一个命令行创建新用户时,我们可能希望非交互地更改密码,例如创建新用户帐户,并在多个Linux机器上更改或设置这些帐户的密码。 借助管道和一些技巧,我们可以在一个命令行中更改用户密码。 这将节省大量时间,尤其是在创建一批用户帐户时。

We use one example to introduce how to change Linux user’s password in one command line. Suppose we login as root and want to change user linuxuser‘s password to linuxpassword.

我们使用一个示例来介绍如何在一个命令行中更改Linux用户的密码。 假设我们以root用户身份登录,并且想要将用户linuxuser的密码更改为linuxpassword。

The passwd command asks for the new password twice. And these two inputs (the same password) is separated by one "Enter". We can emulate this by the echo command with the -e option set. When -e is in effect, escaped characters will be interpreted. Hence, n in echo’s input is echoed as "new line". In addition, on modern Linux with sufficiently new passwd, you can use the --stdin option to let passwd accept a password from the STDIN instead of asking for the new password twice.

passwd命令要求输入两次新密码。 并且这两个输入(相同的密码)之间用一个“ Enter”分隔。 我们可以通过带有-e选项集的echo命令来模拟这一点。 当-e有效时,将解释转义的字符。 因此,echo输入中的n被作为“换行”回显。 另外,在具有足够新passwd现代Linux上,可以使用--stdin选项让passwd接受来自STDIN的密码,而不是两次询问新密码。

So to change the password in our example, we just execute this one command:

因此,要在我们的示例中更改密码,我们只需执行以下命令:

# echo "linuxpassword" | passwd --stdin linuxuser

on modern Linux. (Thanks to DAVID for this tip)

在现代Linux上。 (感谢DAVID的提示)

or

要么

# echo -e "linuxpassword\nlinuxpassword" | passwd linuxuser

This can also be put into one bash script or executed on remote node by the ssh command.

也可以将其放入一个bash脚本中,或通过ssh命令在远程节点上执行。

For example, we can change the password of linuxuser on a batch of servers (100 servers: 10.1.0.1 to 10.1.0.100) by:

例如,我们可以通过以下方法在一批服务器(100台服务器:10.1.0.1到10.1.0.100)上更改linuxuser的密码:

# for ((i=1;i<=100;i++)); do \
ssh 10.1.0.$i 'echo -e "linuxpassword\nlinuxpassword" | passwd linuxuser'; \
done;

Even further, we can create one user and set its initial password remotely by:

更进一步,我们可以创建一个用户并通过以下方式远程设置其初始密码:

# ssh remoteserver \
'useradd newuser; echo -e "passwdofuser\npasswdofuser" | passwd newuser'

If you want to update your own password as a normal user, you may use

如果您想以普通用户身份更新自己的密码,则可以使用

$ echo -e "your_current_pass\nlinuxpassword\nlinuxpassword" | passwd

Security notes
You must be aware that the full command line can be viewed by all users in the Linux system and the password in the command line can be potentially leased. Only for cases where this is okay, you may consider using the method here.

安全须知
您必须注意,Linux系统中的所有用户都可以查看完整的命令行,并且命令行中的密码有可能被租用。 仅在可以的情况下,您可以考虑使用此处的方法。

Alternative method using chpasswd
chpasswd is a nice tool to change a batch of accounts’ passwords in one Linux box. It can be used to change a user’s password in one command line too. Check its manual for how to use it.

使用chpasswd替代方法
chpasswd是一个不错的工具,可以在一个Linux框中更改一批帐户的密码。 它也可以用于在一个命令行中更改用户密码。 检查其手册以了解如何使用。

翻译自: https://www.systutorials.com/changing-linux-users-password-in-one-command-line/

linux 命令行修改密码

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值