Ssh Copy Id As Another User in Bash

Here is a nice BASH one liner that will allow you as (presumably root) copy another users SSH keys up to a server.

This is ideal for setting up multiple users on a machine as the root user in bulk as part of a batch process.

Ideally you will have already added the server to your ssh_config file so that ports, users etc do not need to be specified and also you have run ssh-copy-id serverName as root so that you no longer have to set any passwords.

Then for each user, simply run this command, replacing the placeholders as you see fit.

cat /home/__USER__/.ssh/id_rsa.pub | ssh __SERVER__ "tee -a ~/.ssh/authorized_keys"

I tried a few techniques before settling on this one as by far the cleanest and easiest. Original credit goes to this answer on superuser.com


Tags: bashlinuxsshcopyidsukey