Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify user's password interactively and non-interactively under Linux

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to change user password interactively and non-interactively under Linux". In daily operation, I believe that many people have doubts about how to modify user password interactively and non-interactively under Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize interactive and non-interactive user password modification under Linux". Next, please follow the editor to study!

1. Configure local users' passwords interactively: passwd command

The code is as follows:

[root@host_221-81] # passwd qa

Changing password for user qa.

New password:

BAD PASSWORD: it is too short

BAD PASSWORD: is too simple

Retype new password:

Passwd: all authentication tokens updated successfully.

two。 Non-interactive modification of local users' passwords: chpasswd

The code is as follows:

# the chpasswd command is simple to use

[root@host_221-81 ~] # echo "qa:1234" | chpasswd

# it is also possible to modify passwords non-interactively by using passwd command

[root@host_221-81 ~] # echo "1234" | passwd-- stdin "qa"

Changing password for user qa.

Passwd: all authentication tokens updated successfully.

3. Use expect to handle interactive input to achieve non-interactive password changes.

The code is as follows:

#! / bin/sh

#\

Exec expect-f "$0"$@"

If {$argc! = 2} {

Puts "Usage: $argv0"

Exit 1

}

Set password [lindex $argv 1]

Spawn passwd [lindex $argv 0]

Sleep 1

Expect "assword:"

Send "$password\ r"

Expect "assword:"

Send "$password\ r"

Expect eof

Note: the second line of the script, which may be unfamiliar, is the syntax in the TCL language, The backslash is recognized as part of a comment to sh, but in Tcl the backslash continues the comment into the next line which keeps the exec command from executing again.

The execution result of the script is:

The code is as follows:

[root@smilejay ~] #. / change-pwd-expect.sh qa 1234

Spawn passwd qa

Changing password for user qa.

New password:

BAD PASSWORD: it is too short

BAD PASSWORD: is too simple

Retype new password:

Passwd: all authentication tokens updated successfully.

At this point, the study on "how to change users' passwords interactively and non-interactively under Linux" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report