How to Create SSH Key for Linux User with ssh-keygen

linuxssh-keys

Being a Sudo user, is it possible to create a SSH key for an user in the same Linux server? This Sudo user doesn't have a Switch user privilege.

I have a server where I Login as sudo user say 'admin'(doesn't have switch user privilege) and I have another user say 'user1'. I have a script in my server that should run as 'user1' which should call another script running in a remote host(remote host has a similar user named 'user1') during this process, the 'user1' needs an authorized key for remote host authentication. Since the sudo user 'admin' doesn't have the privilege to switch as 'user1' and generate the ssh keys, I'll have to generate the ssh keys for 'user1' as 'admin'

Best Answer

You don't need any sudo for this. Assuming you have sshd running with passwordAuth on server2 for user1, do the following from server1:

mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/id_rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub user1@server2

At this point, ~/.ssh/authorized_keys is installed on server2, and assuming pubkeyAuth is enabled on server2, you can try a ssh without pwd:

ssh user1@server2 ls

now, if you want to revert things (allow user1 from server2 to login in server1 with pubKey), do the following from user1 on server2:

scp user1@server1:.ssh/id_rsa .ssh/id_rsa
chmod 600 .ssh/id_rsa

and on server1:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys