Skip to main content

如何使用不同的方式更改 Linux 用户密码 | Linux 中国

在 Linux 中创建用户账号时,设置用户密码是一件基本的事情。每个人都使用 passwd 命令跟上用户名,比如 passwd USERNAME 来为用户设置密码。
确保你一定要设置一个难以猜测的密码,这可以帮助你使系统更安全。我的意思是,密码应该是字母、符号和数字的组合。此外,出于安全原因,我建议你至少每月更改一次密码。
当你使用 passwd 命令时,它会要求你输入两次密码来设置。这是一种设置用户密码的原生方法。
如果你不想两次更新密码,并希望以不同的方式进行更新,怎么办呢?当然,这可以的,有可能做到。
如果你是 Linux 管理员,你可能已经多次问过下面的问题。你可能、也可能没有得到这些问题的答案。
无论如何,不要担心,我们会回答你所有的问题。
◈ 如何用一条命令更改用户密码?
◈ 如何在 Linux 中为多个用户更改为相同的密码?
◈ 如何在 Linux 中更改多个用户的密码?
◈ 如何在 Linux 中为多个用户更改为不同的密码?
◈ 如何在多个 Linux 服务器中更改用户的密码?
◈ 如何在多个 Linux 服务器中更改多个用户的密码?
方法-1:使用 passwd 命令
passwd 命令是在 Linux 中为用户设置、更改密码的标准方法。以下是标准方法。
  1. # passwd renu
  2. Changing password for user renu.
  3. New password:
  4. BAD PASSWORD: The password contains the user name in some form
  5. Retype new password:
  6. passwd: all authentication tokens updated successfully.
如果希望在一条命令中设置或更改密码,运行以下命令。它允许用户在一条命令中更新密码。
  1. # echo "new_password" | passwd --stdin thanu
  2. Changing password for user thanu.
  3. passwd: all authentication tokens updated successfully.
方法-2:使用 chpasswd 命令
chpasswd 是另一个命令,允许我们为 Linux 中的用户设置、更改密码。如果希望在一条命令中使用 chpasswd 命令更改用户密码,用以下格式。
  1. # echo "thanu:new_password" | chpasswd
方法-3:如何为多个用户设置不同的密码
如果你要为 Linux 中的多个用户设置、更改密码,并且使用不同的密码,使用以下脚本。
为此,首先我们需要使用以下命令获取用户列表。下面的命令将列出拥有 /home 目录的用户,并将输出重定向到 user-list.txt 文件。
  1. # cat /etc/passwd | grep "/home" | cut -d":" -f1 > user-list.txt
使用 cat 命令列出用户。如果你不想重置特定用户的密码,那么从列表中移除该用户。
  1. # cat user-list.txt
  2. centos
  3. magi
  4. daygeek
  5. thanu
  6. renu
创建以下 shell 小脚本来实现此目的。
  1. # vi password-update.sh

  2. #!/bin/sh
  3. for user in `more user-list.txt`
  4. do
  5. echo "[email protected]" | passwd --stdin "$user"
  6. chage -d 0 $user
  7. done
给 password-update.sh 文件设置可执行权限。
  1. # chmod +x password-update.sh
最后运行脚本来实现这一目标。
  1. # ./password-up.sh

  2. magi
  3. Changing password for user magi.
  4. passwd: all authentication tokens updated successfully.
  5. daygeek
  6. Changing password for user daygeek.
  7. passwd: all authentication tokens updated successfully.
  8. thanu
  9. Changing password for user thanu.
  10. passwd: all authentication tokens updated successfully.
  11. renu
  12. Changing password for user renu.
  13. passwd: all authentication tokens updated successfully.
方法-4:如何为多个用户设置相同的密码
如果要在 Linux 中为多个用户设置、更改相同的密码,使用以下脚本。
  1. # vi password-update.sh

  2. #!/bin/sh
  3. for user in `more user-list.txt`
  4. do
  5. echo "new_password" | passwd --stdin "$user"
  6. chage -d 0 $user
  7. done
方法-5:如何在多个服务器中更改用户密码
如果希望更改多个服务器中的用户密码,使用以下脚本。在本例中,我们将更改 renu 用户的密码,确保你必须提供你希望更新密码的用户名而不是我们的用户名。
确保你必须将服务器列表保存在 server-list.txt 文件中,每个服务器应该在单独一行中。
  1. # vi password-update.sh

  2. #!/bin/bash
  3. for server in `cat server-list.txt`
  4. do
  5. ssh [email protected]$server 'passwd --stdin renu <
  6. new_passwd
  7. new_passwd
  8. EOF';
  9. done
你将得到与我们类似的输出。
  1. # ./password-update.sh

  2. New password: BAD PASSWORD: it is based on a dictionary word
  3. BAD PASSWORD: is too simple
  4. Retype new password: Changing password for user renu.
  5. passwd: all authentication tokens updated successfully.
  6. New password: BAD PASSWORD: it is based on a dictionary word
  7. BAD PASSWORD: is too simple
  8. Retype new password: Changing password for user renu.
  9. passwd: all authentication tokens updated successfully.
方法-6:如何使用 pssh 命令更改多个服务器中的用户密码
pssh 是一个在多个主机上并行执行 ssh 连接的程序。它提供了一些特性,例如向所有进程发送输入,向 ssh 传递密码,将输出保存到文件以及超时处理。导航到以下链接以了解关于 PSSH 命令[1]的更多信息。
  1. # pssh -i -h /tmp/server-list.txt "printf '%s\n' new_pass new_pass | passwd --stdin root"
你将获得与我们类似的输出。
  1. [1] 07:58:07 [SUCCESS] CentOS.2daygeek.com
  2. Changing password for user root.
  3. passwd: all authentication tokens updated successfully.
  4. Stderr: New password: BAD PASSWORD: it is based on a dictionary word
  5. BAD PASSWORD: is too simple
  6. Retype new password:
  7. [2] 07:58:07 [SUCCESS] ArchLinux.2daygeek.com
  8. Changing password for user root.
  9. passwd: all authentication tokens updated successfully.
  10. Stderr: New password: BAD PASSWORD: it is based on a dictionary word
  11. BAD PASSWORD: is too simple
方法-7:如何使用 chpasswd 命令更改多个服务器中的用户密码
或者,我们可以使用 chpasswd 命令更新多个服务器中的用户密码。
  1. # ./password-update.sh

  2. #!/bin/bash
  3. for server in `cat server-list.txt`
  4. do
  5. ssh [email protected]$server 'echo "magi:new_password" | chpasswd'
  6. done
方法-8:如何使用 chpasswd 命令在 Linux 服务器中更改多个用户的密码
为此,首先创建一个文件,以下面的格式更新用户名和密码。在本例中,我创建了一个名为 user-list.txt 的文件。
参考下面的详细信息。
  1. # cat user-list.txt
  2. magi:new@123
  3. daygeek:new@123
  4. thanu:new@123
  5. renu:new@123
创建下面的 shell 小脚本来实现这一点。
  1. # vi password-update.sh

  2. #!/bin/bash
  3. for users in `cat user-list.txt`
  4. do
  5. echo $users | chpasswd
  6. done

via: https://www.2daygeek.com/linux-passwd-chpasswd-command-set-update-change-users-password-in-linux-using-shell-script/

Comments

Popular posts from this blog

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checking a shared sec

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh , but without the use of k

标 题: 关于Daniel Guo 律师

发信人: q123452017 (水天一色), 信区: I140 标  题: 关于Daniel Guo 律师 关键字: Daniel Guo 发信站: BBS 未名空间站 (Thu Apr 26 02:11:35 2018, 美东) 这些是lz根据亲身经历在 Immigration版上发的帖以及一些关于Daniel Guo 律师的回 帖,希望大家不要被一些马甲帖广告帖所骗,慎重考虑选择律师。 WG 和Guo两家律师对比 1. fully refund的合约上的区别 wegreened家是case不过只要第二次没有file就可以fully refund。郭家是要两次case 没过才给refund,而且只要第二次pl draft好律师就可以不退任何律师费。 2. 回信速度 wegreened家一般24小时内回信。郭律师是在可以快速回复的时候才回复很快,对于需 要时间回复或者是不愿意给出确切答复的时候就回复的比较慢。 比如:lz问过郭律师他们律所在nsc区域最近eb1a的通过率,大家也知道nsc现在杀手如 云,但是郭律师过了两天只回复说让秘书update最近的case然后去网页上查,但是上面 并没有写明tsc还是nsc。 lz还问过郭律师关于准备ps (他要求的文件)的一些问题,模版上有的东西不是很清 楚,但是他一般就是把模版上的东西再copy一遍发过来。 3. 材料区别 (推荐信) 因为我只收到郭律师写的推荐信,所以可以比下两家推荐信 wegreened家推荐信写的比较长,而且每封推荐信会用不同的语气和风格,会包含lz写 的research summary里面的某个方面 郭家四封推荐信都是一个格式,一种语气,连地址,信的称呼都是一样的,怎么看四封 推荐信都是同一个人写出来的。套路基本都是第一段目的,第二段介绍推荐人,第三段 某篇或几篇文章的abstract,最后结论 4. 前期材料准备 wegreened家要按照他们的模版准备一个十几页的research summary。 郭律师在签约之前说的是只需要准备五页左右的summary,但是在lz签完约收到推荐信 ,郭律师又发来一个很长的ps要lz自己填,而且和pl的格式基本差不多。 总结下来,申请自己上心最重要。但是如果选律师,lz更倾向于wegreened,