程式扎記: [Linux 常見問題] How to setup password of account without interactive steps

標籤

2016年5月3日 星期二

[Linux 常見問題] How to setup password of account without interactive steps

Question 
From Linux, we change password by command passwd. For example: 
john@ubuntu:~$ passwd
Changing password for john.
(current) UNIX password: <Current Password>
Enter new UNIX password: <Enter new password>
Retype new UNIX password: <Enter new password again>
passwd: password updated successfully

If you are root, you can change password without enter current password. For example: 
# passwd john
輸入新的 UNIX 密碼: <Enter new password>
再次輸入新的 UNIX 密碼: <Enter new password again>
passwd: password updated successfully

Is it possible to change the password of account in one line? 

How-To 
First of all, we know the Linux will store the account information in /etc/shadow and /etc/passwd. For example: 
# cat /etc/shadow | grep john
john:$6$abv9TkJp$3anOR4UYfTtUCibWkcoLsw1UDebf97WhZaA2dABePQedKzuNNZye.CzmHpKgy.yHIekqqP7h.uKkeE5VypV.N/:16924:0:99999:7:::

The password in /etc/shadow is encrypted. First step, we have to encrypt our password by openssl
# echo 'abcd1234' | openssl passwd -crypt -stdin
ws.3IuJZJjZEE

Then we will use command usermod to setup the password of account: 
# usermod --password ws.3IuJZJjZEE john // Use the encrypted password 'ws.3IuJZJjZEE' (abcd1234) for account john
# ssh john@localhost // Now you can use password=abcd1234 to login host with account john

Now I want to change the password of account john to 'hellojohn': 
# usermod --password `echo 'hellojohn' | openssl passwd -crypt -stdin` john

Supplement 
How to set user passwords using passwd without a prompt? 
How to create encrypted password via command line

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!