Migrate SSH users from one server to another

In this post, we’ll see how we can migrate SSH users from one server to another server. We’ll cover following scenarios:

ec2-user: exists on both the servers
new_user: ssh key for the user not available on server1
new_user2: /home directory exists but /home/.ssh directory doesn’t exist(on server2)
new_user3: /home and /home/.ssh directory exist but /home/.ssh/authorized_keys file is not there(n server2)
new_user4: user doesn’t exist on server1

Pre-requisite: 2 servers with password authentication enabled. On server 2 we need a user that can ssh into server1. We also need sshpass package installed on server2.

Step 1: Let’s have a look at what files and directories we have on server 1.

Step 2: On server2, create a file named user.txt with the list of user names we need to create on this server.

cat user.txtec2-user

Step 3: On server2, create a file named password.txt with the password we need in order to ssh into server1

cat password.txtNewtestPassword2020#

Step 4: Copy bash script given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/script/ssh.sh on server2. Provide server1 and server2 IP address.

Step 5: Execute following commands on server2

chmod +x ssh.sh

That’s all. Now you’ve all 5 users available on server2. If you want to ssh into server2 with new_user and new_user4 usernames, you need to specify the password(default password is user_password@2020#) while other users can be accessed via their ssh keys.