Linux change password in one command

How to change a Linux password

To best manage your own user account or the accounts of other users, it is helpful to know the most important utilities of Linux. This includes passwd, which you can use to change your Linux password, among other things. You can learn the individual steps here.

The passwd command in Ubuntu

The passwd program package, which allows you to easily change your own password or the passwords of an entire group, as well as set other commands, is included in most Linux installations. Our guide refers to the Ubuntu installations — Ubuntu is one of the most versatile, free distributions of Linux with long-term support and biannual updates.

If you are not sure which Linux distribution or version you are using, see our article on how to display your Linux version.

Change your own Linux password

If you want to change your own Linux password, this is a relatively straightforward process.

  1. Open the Linux terminal. This is where you normally enter commands.
  2. Now type the command “passwd”. This is how you tell the system that you want to change your Linux password.
  3. Depending on the version, you may be prompted to enter your old password. In response, simply enter your new password and confirm it again.
  4. If you now try to log into the system, the new password will be valid.
$ passwd Enter a new UNIX password: Re-enter the new UNIX password: passwd: Password successfully changed $

Change your own Linux password

Changing passwords regularly was once one of the top recommendations for keeping safe online. However, this long-standing advice has been withdrawn, as changing passwords frequently can weaken security. Therefore, it is advisable to only change a password if the password is considered “compromised” or too weak.

Change a Linux password for one user

If you want to change the password of another user, then you need the root privileges. This means that you are the only user of the computer who has unlimited rights to all system files and resources. Keep your password ready for this reason.

  1. To change a user password, access the terminal and type “sudo passwd ”.
  2. Follow the steps in the previous example: enter new password and confirm by typing it again.
  3. If the password is set, the respective user must use the new password at the next login.
$ sudo passwd username Enter a new UNIX password: Re-enter the new UNIX password: passwd: Password successfully changed $

Force Linux password change at next log in

Sometimes a system administrator needs a user to change their password immediately. This can be for various reasons. There may be a user change for an account, or a password may have fallen into the wrong hands. In this case, enter the following syntax as an administrator: “-e passwd ”. Here, “-e” stands for “expire” and causes the respective password to expire immediately, so that the affected user must set up a new password the next time they log in.

Читайте также:  Linux sharing home directory

We have collected the most important Linux commands for you in our article.

All important Linux commands

All important Linux commands

Under Linux, all actions that you can carry out with the mouse and window system via the graphical user interface can also be performed using program calls in the terminal – provided you know the appropriate command and how to use it according to the correct syntax. To make working in the terminal easier, we provide you with an overview of basic Linux commands with detailed descriptions and…

How to get your Linux IP address in different ways

How to get your Linux IP address in different ways

If you want to check your IP address in Linux, you have a couple of options. In addition to the graphical route via the settings, you can also use commands to get the information you’re looking for. Here, we’ll introduce you to some of these commands, like “ifconfig”, the equivalent of the “ipconfig” Windows command in Linux.

The best Linux Server distributions in an overview

The best Linux Server distributions in an overview

There are several Linux distributions available for a variety of needs thanks to the open source feature and the dedicated communities. In our overview, we’ll highlight the best providers, explain their advantages and disadvantages, and outline which operating systems are best suited for servers, desktops, or other uses.

The best Linux distributions (operating systems)

The best Linux distributions (operating systems)

It can be difficult to keep track of all the Linux distributions available. Whether you are looking to switch your business to Linux or are simply curious about Linux and in the market for a new operating system, our article shows you the top distributions and discusses their different purposes. Read on to find out more.

Источник

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.

Читайте также:  Simply linux версия 10

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.

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.

Источник

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.

Читайте также:  Открыть двоичный файл linux

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'

Источник

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.

Источник

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