Linux: How to Change Password Over SSH

This post will guide you how to change the root password on Linux server over ssh. How to use a bash shell script to change user password on remote Linux server.

Change Password on Linux Server


If you have logged on Linux server, and you want to change password for your current logging user, you can use passwd command to achieve the result. Type the following command:

# passwd

Outputs:

devops@devops-osetc:~$ passwd
Changing password for devops.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:

Change Password Over SSh on Remote Linux server


You can also change the root password over ssh on a remote linux server, you can use the ssh command to connect your linux server, then pass passwd command to it. type the following command to change password for root user using ssh:

# ssh root@192.168.1.21 passwd

You can also write an expect script to achieve the same result of changing password for a specific user using ssh:

#!/usr/bin/expect -f
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd

set username [lindex $argv 0]
set password [lindex $argv 1]
set serverid [lindex $argv 2]
set newpassword [lindex $argv 3]

spawn ssh $serverid passwd
expect "assword:"
send "$password\r"
expect "UNIX password:"
send "$password\r"
expect "password:"
send "$newpassword\r"
expect "password:"
send "$newpassword\r"
expect eof

You need to install expect package in your linux system before running this script.

You might also like:

Sidebar



back to top