Change linux password in one line

Changing Linux User Password in One Command Line

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.

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.

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.

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)

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

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

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:

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

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

Источник

Changing a Linux Users Password in One Line Command

Changing a Linux Users Password in One Line Command

I have a group of users that have the same account on a few of my Linux systems. They seem to forget their passwords at least once every two months and like to blame my password complexity rules. I am tasked every so often with changing a users password on multiple systems which really is a pain. Here we will examine a nice way to change a users password with a one line command. We will also talk about how we can do this remotely on multiple servers.

First, let’s look at how we can change a users password in one line using echo and the pipe.

echo -e "'NEWPASS'\n'NEWPASS'" | passwd USERNAME

NOTE: There are doube quotes ( » ) surrounding the passwords, but each password is wrapped in a single quote ( ‘ ) to allow for special characters.

Here we use echo with the “-e” switch. This tell echo to read the “\n” as a newline. Then is is piped into the passwd “USERNAME” command.

Читайте также:  Linux шлюз для сети

We can also use this to change the password on a remote machine with ssh.

ssh [email protected] 'useradd newuser; echo -e "'NEWPASS'\n'NEWPASS'" | passwd USERNAME'

This assumes your allowing ssh from root or a user with elevated privileges, which is not recommended for security reasons.

You can also run this command (or any command) on multiple servers using a loop. So for this example we will say we have several servers named server1 through server8. What I would do is make a text file containing the server names, one per line like so:

server1
server2
server3
server4
server5
server6
server7
server8

Now we can use a for loop to loop through the lines in the file and connect to each machine.

for i in `cat filename.txt`; do ssh $i 'echo -e "'NEWPASS'\n'NEWPASS'" | passwd USERNAME'; done

There are easier ways to accomplish this if your servers are actually named server1 through server8. But in the real world I doubt your servers and named so conveniently.

Источник

Can I change a user password in Linux from the command line with no interactivity?

I have a specific use case where I would really like to be able to change a user’s password with a single command with no interactivity. This is being done in a safe fashion (over SSH, and on a system with only one user able to be logged in), so it’s fine to expose the new password (and even the old one, if necessary) on the command line. FWIW, it’s a Ubuntu system. I just want to avoid having to add something Expect-like to this system for just this one task.

5 Answers 5

echo user:pass | /usr/sbin/chpasswd 

You can use usermod with the -p option to provide a password hash (not the actual password). You can generate the password hash using something like mkpasswd -m sha-256 or mkpasswd -m md5

  1. Hash the password on your local system.
  2. Connect to the remote machine (where you want to change the password)
  3. Feed the hashed password & the username to a creative sed script that updates your system’s password file ( /etc/shadow , /etc/master.passwd , whatever it happens to be).

The passwd utility has a —stdin option that states:

This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.

echo "newpass" | passwd --stdin user1 

Even though you mentioned you don’t care, you could put the password in a text file and then do cat pass.txt instead of the echo command, that way it doesn’t show up in the bash history.

Источник

How to Change User Password in Linux

How to Change User Passwords in Linux

In this tutorial we’ll explain how to change user passwords in Linux, from the command line, using the passwd command.

We’ll show you how to change your current user’s password, as well as changing, expiring, or locking passwords for other users.

These commands should work on any Linux distribution, such as Ubuntu, Debian, Linux Mint, CentOS, Rocky Linux, etc.

Introduction

Regular users can only change their own passwords. To change another user’s password you need to be root or be a user with sudo privileges.

In Linux and Unix-like operating systems, you can change passwords, and related changes, using the passwd utility. The hashed passwords, along with other related information is stored in the /etc/shadow file. To see the file contents you can go ahead and run:

It’s very important that you keep a habit of using strong passwords on your machines. This is especially important if your machine is a server that’s exposed to the internet, where bad actors may constantly attempt to break in.

Читайте также:  Виртуальная клавиатура альт линукс

We recommend that you set strong passwords for your systems. This means passwords of a minimum length of 12 characters, containing at least one uppercase letter (A-Z), one lowercase letter (a-z), one number (0-9), and one special characeter (!@#$..).

It’s also recommended that each user on your system have an unique password, and to update all your user passwords on a regular basis.

Table of Contents

How to Change Your User Password in Linux

To change the password for the current user you can use the passwd command, which is easy and straightforward.

To change our user password just run the passwd command:

You’ll be prompted to enter your current password to make sure it’s really you.

Changing password for edxd. Current password:

Input your current password and press Enter.

Passwords won’t be displayed on the screen when you input them, for security reasons.

If the password you entered is correct, you’ll next you’ll be prompted to type the new password you want to set.

Usually, the minimum requirements for your new password are a minimum length of 6 characters, for the password not to be too simple, and for it not to be too similar to the current password.

We recommend you use a complex password of at least 12 characters, as we mentioned in the intro.

Next you’ll have to enter the new password again:

If the password fits the minimum requirements then your password should now be successfully changed, and the next time the system asks it will expect your new password.

passwd: password updated successfully

How to Change Another User’s Password in Linux

We can also change passwords for other user accounts using the passwd utility.

To change the user password of a different user’s account you need root access or sudo privileges.

In the following examples we’re assuming you’re logged in as the root user or a non-root sudo user.

To change another user’s password just run the passwd command, followed by the username of the user whose password you want to change:

You will be asked to enter the new password twice, and if it meets the minimum requirements it will be successfully changed and next time the user is prompted for their password they will need to use the new one.

New password: Retype new password: passwd: password updated successfully

If you try to change the password for another user, without havcing root access or sudo privileges you will get an error:

passwd: You may not view or modify password information for bytexd_user.

Change a User’s Password Without a Prompt (Non-Interactive)

You can change another user’s password in a single line, without being prompted for confirmation, using the usermod command.

In order to do this we’ll use the usermod command, which expects us to provide it with the password in a hashed format.

To hash our password right before providing it to usermod we will use the openssl passwd command, with the -6 option, which hashes our password using the SHA512 hashing algorithm.

This command will change the password to 8n&E8MgZd^Rc in one line, without a prompt, for bytexd_user :

sudo usermod --password $(openssl passwd -6 '8n&E8MgZd^Rc') bytexd_user

It’s recommended that you put the password in single quotes – ‘8n&E8MgZd^Rc’ instead of 8n&E8MgZd^Rc , in order to avoid accidental character substitution if you have characters such as $1 in your password.

Читайте также:  Установка x11vnc astra linux

Change Passwords for Multiple Users

Another way to change user passwords is using the chpasswd command that allows you to batch set passwords for multiple users.

Such as before, you will need root access or a user with sudo privileges for this.

Next you won’t see any output and you will have to enter the username and password separated by : – one user:password per line.

edxd:somenewpassword bytexd_user:someothernewpassword stelixd:yetanotherpassword

When you are done press Ctrl+D for the changes to take effect and return to the regular prompt.

There won’t be any confirmation message in the output.

How to Force a User to Change Password at Next Login in Linux

To force a user to change their password at next login means that we’re expiring the user’s password.

To do this we’ll need root or sudo privileges. As such, the following example will assume you’re acting as the root user or a non-root sudo user.

By default, user passwords are set to never expire, however we can can change that using the passwd comand and the -e (or —expire ) option, followed by the user’s username.

The following command will expire the password for user bytexd_user :

sudo passwd -e bytexd_user
passwd: password expiry information changed.

When bytexd_user logs in, after entering their current password, they’ll be forced to change it to a new one.

The user will need to enter their current password once again, after which they’ll be required to set a new one.

You are required to change your password immediately (administrator enforced) Changing password for bytexd_user. Current password: New password: Retype new password: passwd: password updated successfully Connection to 172.141.197.192 closed.

After successfully updating the password, if you’re logging in via SSH, then the connection will close.

If you are simply switching users while already logged in, then the user will successfully switch.

Locking & Unlocking User Passwords in Linux

We can also lock or unlock user passwords. Locking a user password means that the user won’t be able to use or change their password.

The user can still log into the server through a different authentication method, such as with SSH keys, if they have them set up.

We can do this using passwd -l .

The following command locks the password for user bytexd_user :

sudo passwd -l bytexd_user
passwd: password expiry information changed.

Now, when the user tries to SSH into the server, they will still enter the password, but will get Permission denied :

[email protected]'s password: Permission denied, please try again.

To unlock the password for a user we can use the -u option.

The following command wil unlock the password for bytexd_user :

sudo passwd -u bytexd_user
passwd: password expiry information changed.

Conclusion

In this tutorial you learned how to change user passwords in Linux, how to expire passwords and how to lock them.

You can view all options for the passwd command from the manual by running man passwd in your command line, or by reading the man page online.

If you have any questions or have enountered issues, feel free to leave a comment and we’ll get back to you as soon as we can.

Источник

Оцените статью
Adblock
detector