Set password with password linux

Four ways to non-interactively set passwords in Linux

Manually setting or resetting passwords for a large chunk of users is a painfully boring task & is definitely not the best use of our time. In this article, I’ll share four methods to automate this process. Non-Interactively Set Passwords in Linux.

Method1: Use chpasswd Non-Interactively Set Passwords

Using chpasswd we can set or reset login passwords for many users non-interactively.
The chpasswd command reads a list of username and password pairs from standard input and uses this information to update a group of existing users.

Each line is in the format:

We just need to add the username & password in a text file in the form of a key-value pair in the syntax shown above & serve this text file as input to chpasswd command & that is all we need to do.

I’ve created a few users and have added the usernames and their corresponding passwords in a text file.
Given below is the content of the file.

[root@sahil-centos ~]# cat pass.txt testuser1:TestpA$$123 testuser2:TestpA$$124 testuser3:Testpa$$123 testuser4:TestpA$$152 [root@sahil-centos ~]#

Now we just need to feed it to chpasswd to set the passwords for these users.

[root@sahil-centos ~]# chpasswd 

In case we do not want to use a text file to feed input to the chpasswd command due to security concerns then you can execute chpasswd directly, type the username and password separated by colons and press ctrl+d to stop feeding input to chpasswd.

By default, the passwords must be supplied in clear-text, and are encrypted by chpasswd.
chpasswd command works by first updating all the passwords in memory and then commits all the changes to disk if no errors occurred for any user.
This command is intended to be used in a large system environment where many accounts are created at a single time.

Method2: Use stdin

This is another simple method wherein we echo out the password to passwd command via –stdin.
The –stdin option lets the passwd command accept a password from STDIN instead of asking for the new password twice which is the commands’ default behavior.

[root@sahil-centos ~]# echo "TeStP_w0rD" | passwd testuser1 --stdin Changing password for user testuser1. passwd: all authentication tokensupdated successfully.

Method3: Use echo

We know that the passwd command asks the user to supply the password twice (separated by enter key press) when setting or changing it.
Using echo with -e option and a pipe we can emulate entering the password twice separated by an enter key press.
Here is an example:

[root@sahil-centos ~]# echo -e "TeStP_wOr1\nTeStP_wOr1" | passwd testuser2 Changing password for user testuser2. New password: Retype new password: passwd: all authenticationtokens updated successfully.

When we use the -e option with echo it allows us to use escape sequences like \n. The \n character is an escape sequence denoting the new line character and emulates an enter key press.

The pipe redirects the output of echo command which is basically the password we’d like to set written twice separated by the newline character to the input of the passwd command which accepts this input and changes the users’ password.

You may use this method on older versions of the bash shell where –stdin might not be working.

Method 4: Use expect

Expect is an entire programming language based on TCL and is intended to automate tasks which would require interactive input.
For the purposes of this article, we’ll barely scratch the surface and I’ll share how we may use it to reset user passwords non-interactively.

Given below is the expect script that we’ll be using to reset passwords non-interactively.

#!/usr/bin/expect set timeout 10 set user [lindex $argv 0] set password [lindex $argv 1] spawn passwd $user expect "password:" send "$password\r" expect "password:" send "$password\r" expect eof

Let’s reset a user’s password using this quick script.

[root@sahil-centos ~]# ./reset.expect testuser4 TeStP_w0r7 spawn passwd testuser4 Changing password for user testuser4. New password: Retype new password: passwd: all authentication tokens updatedsuccessfully.
  • Without getting into too much detail regarding expect I’ll explain the working of the script.
  • It accepts two arguments the first being the username and the second being the password.
  • The username and password typed as arguments are saved in variables user and password respectively.
  • After accepting the arguments the script executes the passwd command for the user.

Next, we have a combination of expect and send statements to non-interactively change the users’ password.

This may not seem very useful at first but we can put the usernames and password in a file and then iterate the script over them in a for loop.

That’s it about Non-Interactively Set Passwords in Linux.

Thanks for your wonderful Support and Encouragement

More than 40000 Techies in our community do you want part of it Join Now

I am a system administrator who loves to learn and share my knowledge with the community. I've been working in the IT industry since 2011.

Источник

Easily Create User With Password with one line command in Linux

Linux requires knowledge about terminal and commands and unlike Windows 10 you can do everything with a bunch of keystrokes. Adding users in Linux is rather simple but command based. In this article, we will see how to create user with password on Linux and one line commands to add a user with password’.

When we talk about Linux, the first thing to consider is Ubuntu, which is a fairly popular operating system. Apart from this, many operating systems are based on Ubuntu since there are segments of people who don’t like the way Ubuntu works and looks. Some operating systems are such that are made for very specific users such as Elementary OS.

Whatever new user account you create, a new folder creates under /Home/username.

Add a user to Linux the usual way.

For this, you will have to open the terminal. You can open the terminal by going to the Task Bar — you will find the terminal app in applications list. There are some shortcuts that you can use to open the terminal window.

When the terminal window opens, you have to type the comment given below following by a username.

useradd [username] replace username with something more human.

The new user account has to be secured, so it’ll be locked which require you to set a new password.

passwd [password] This command will set given password for the user you created.

Example, I would like to have a new user account with the name “Devendra” and “qf007” password.

# useradd Devendra
# passwd qf007

  • -c comment: Add a comment for user.
  • -e yyyy-mm-dd: Date for the account to be disabled
  • -f days: Set expiration date 0 to disable account with password expiration. -1 for not disable the account.
  • -M: Do not create the home directory

Load more options you can choose by typing the command.

Linux add user with password one line

Linux users are demanding, many would ask for one line command to add username with password and fortunately, there is a way to do this.

In Linux, useradd is used to configure everything including username and password. For security reasons, the password should in encrypted, and you can use openssl for making md5 passwords, this helps to specify the password if it’s in plain text.

useradd -u ABCDE -g users -d /home/username -s /bin/bash -p $(echo mypasswd | openssl passwd -1 -stdin) username

-u userid
-d groupname
-d user home directory
-s default shell
-p password
Openssl passwd will generate hash of mypasswd to be used as secure password.

To exclude this from history, unite a space before the command to prevent is from history. If you have to do the corresponding on lots of machines, generate the password once and type in the command.

useradd -u 12345 -g users -d /home/username -s /bin/bash -p '$1$NNfXfoym$Eos.OG6sFMGE8U6ImwBqT1' username
adduser --uid 3434 --password my_password my_login

Pretty much everything was given in this article to create a user with password in Linux, even with a single line code-that’s something. If you like Linux, hop on and subscribe to our channel. Do you have more Linux tips, why not comment down them, below.

Since you are here…

I’ve got a small favor to ask. This is an independent site, and producing content takes a lot of hard work and time. Although many people are reading Quickfever, many use adblocker.. And unlike many other sites, there is no paywall blocking. So you can see why your help is needed. If everyone who finds this website useful and helps to support it, the future would be much more secure. Thank you.

If you use adblocker, please disable it for this site.

$0 raised so far by 0 people.

Источник

How to set password to a user account in Linux?

The Linux Juggernaut

In this post we will see how to set a password for an user. Once the user is created we have to set a password for the account, if we do not set it user will not be able to login. Below steps shows the steps in setting a password for an user.

Step 1 : Check if user password is set or not.

grep username /etc/shadow

root@linuxnix.com:/home/surendra# grep kumar /etc/shadow
kumar: ! :15615:0:99999:7.
root@linuxnix.com:/home/surendra#

if you see !(negation or exclamation) in second field of ‘kumar’ username in /etc/shadow file, it indicates user password is not set.

Step 2 : Create password for an user

passwd username

root@linuxnix.com:/home/surendra# passwd krishan
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

This command will ask for the conformation for the password and will not show what you type. Just retype the password. Once the password is matched it gives information that password updated successfully.

Now try to login with the username and password and enjoy Linux login access.

Advanced way of creating password in Linux

We can even create password by using md5pass command to create a password checksum and keep this encrypted password in shadow file.

Step1: Create encrypted password using md5pass as shown below.

md5pass redhat

root@linuxnix.com:/home/surendra# md5pass krishan
$1$At.eFgfG$BxrbG/v8qZTZoEb7SRJpw.
root@linuxnix.com:/home/surendra#

Step2: Now copy the encrypted password ‘$1$At.eFgfG$BxrbG/v8qZTZoEb7SRJpw.’ without quotes to the second field of user ‘kumar’ entry in /etc/shadow file.

vi /etc/shadow

enter the password in second field of username:kumar and save the file.

Step 3 : Check if the password is updated or not

grep kumar /etc/shadow

root@linuxnix.com:/home/surendra# grep kumar /etc/shadow
kumar:$1$At.eFgfG$BxrbG/v8qZTZoEb7SRJpw.:15615:0:99999:7.
root@linuxnix.com:/home/surendra#

Now try to login with the username and password what you created now.

Note: This is just for knowledge purpose and do not try on production machines.

Surendra Anne

Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.

Latest posts by Surendra Anne (see all)

  • Docker: How to copy files to/from docker container - June 30, 2020
  • Anisble: ERROR! unexpected parameter type in action: Fix - June 29, 2020
  • FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
  • Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
  • How to use ohai/chef-shell to get node attributes - July 19, 2018

Источник

Читайте также:  Linux file string in files
Оцените статью
Adblock
detector