Copy user's SSH ID to another user

I have login with root on a client server and use "su - u user1" to stimulate the user. Afterwards, I use "ssh-keygen -t rsa".

Afterwards, I use "ssh-copy-id -i id_rsa.pub server" to copy the user's key to another server. It ask for the user password.

However, I don't have the user's password and hence, I can't copy the keys to the server. What can I do ?
(actually, I have use root to copy the keys to the user 's .ssh directory but it doesn't work).

Any advise ? Tks
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
Did you adjust the rights and owner on the ssh keys after copying them to the other user's directory?

Also when you copy over the key to another server, is it the user password or the root password of the other server it's asking?
AXISHKAuthor Commented:
it ask for user password but I do not have it
Zephyr ICTCloud ArchitectCommented:
Can't you try copying it to the root account on the other server with the root user?
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

serialbandCommented:
I'll clarify a bit more on what spravtek mentions.

It shouldn't ask if you're logged in to root or connected to root when you do the copying.  If you're not using root, then you must have the user account's password first, or you'll need to have the user do it for you.

When you copy the ssh key to the other account, you also need to set the owner of the key file to that new user.
  chown -R user2 ~user2/.ssh/
AXISHKAuthor Commented:
policy do not allow root to ssh to another user
serialbandCommented:
Then how did you copy the file as root, as you've mentioned in the original question?
AXISHKAuthor Commented:
so use root account to copy files under the user's .ssh folder to the corresponding. user folder on remote server.Grant the permission then, correct
Zephyr ICTCloud ArchitectCommented:
That's a possibility yes ... You can test that.
serialbandCommented:
Yes.  If you don't have the account password, and you have root, then that's the only way.
AXISHKAuthor Commented:
Still doesn't work...

There are 3 keys - id_rsa, id_rsa.pub and known_hosts have been copied to the remote server. And already changed the group (chown) and permission (chmod 644) to these files. Try again to ssh to the remote server with the user but it still asked for password. Any idea ?

Tks
serialbandCommented:
You only need to copy the public key to the remote server and place it in the authorized keys file.

cat id_rsa.pub >> ~user2/.ssh/authorized_keys

If this is the first time you've created the flie, you also need to change the owner.
chown user2 ~user2/.ssh/authorized_keys

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AXISHKAuthor Commented:
Done. when ssh user1@remoteserver, it seem to connect but it cut off automatically... Any idea ?/
Zephyr ICTCloud ArchitectCommented:
Set logging on when doing the ssh, that might give us a clue, like this:

ssh user1@remoteserver -vvvv

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.