Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Not sure if the question I'm asking is correct but basically I wanted to automate this process

  scp ~/.ssh/id_rsa.pub username@example.com:~/
  ssh username@example.com
  mkdir .ssh
  cat id_rsa.pub >> .ssh/authorized_keys
  rm id_rsa.pub
  chmod go-w ~
  chmod 700 ~/.ssh
  chmod 600 ~/.ssh/authorized_keys

So I thought I could create a shell script and put it in my .bash_profile like this:function

setup_ssh () {
  scp ~/.ssh/id_rsa.pub $1:~/
  ssh $1
  #the following is happens when connected to the server using ssh
  mkdir .ssh
  cat id_rsa.pub >> .ssh/authorized_keys
  rm id_rsa.pub
  chmod go-w ~
  chmod 700 ~/.ssh
  chmod 600 ~/.ssh/authorized_keys

But of course this doesn't work because it doesn't continue the commands once connected to the server. Is there any way to continue the commands once it's connected to the server via ssh?

share|improve this question
Patrick's answer explains how to make your method work, and it's a valuable lesson in how to use ssh. But for the specific task of copying keys, use ssh-copy-id as shown by forcefsck, it's exactly suited for this task. – Gilles Jan 22 '12 at 23:05
See also this question for this specific use case. I left this open because this is a more general question about forwarding commands over SSH, but then the accepted answer ended up not addressing it, which is unfortunate – Michael Mrozek Jan 23 '12 at 16:06
If it'll make you feel better I'll change it to accepting the other one. But for the purpose of my question forcefsck gave me a better option. They're both good answers though. – Marc Jan 27 '12 at 12:27
up vote 5 down vote accepted

Adds your identity to the remote server, also creates ~/.ssh tree with appropriate permissions if needed.

ssh-copy-id -i ~/.ssh/id_rsa.pub user@server
share|improve this answer
Wow... I had no idea this existed. This is great. – Marc Jan 23 '12 at 10:05

Here's a slightly different way that works:

setup_ssh() {
  cat ~/.ssh/id_rsa.pub | ssh $1 '
    mkdir .ssh;
    cat >> .ssh/authorized_keys;
    chmod go-w .;
    chmod 700 .ssh;
    chmod 600 .ssh/authorized_keys

You could also get rid of the cat and have the shell pipe in your id_rsa.pub file, but I think this is enough to get you going.

The way it works
You're basically starting a new shell on the remote host to execute the commands you passed to ssh (in the quotes). When it gets to the cat >> .ssh/authorized_keys part, nothing has read from the STDIN yet, so the data from the cat ~/.ssh/id_rsa.pub is still sitting there. cat reads data from STDIN and then we redirect that to the new authorized_keys file. Then we execute the remaining commands.

share|improve this answer
The key is that all the commands to be run on the remote machine are the parameters to ssh (using the single quotes). – Aaron D. Marasco Jan 22 '12 at 1:02
Thanks so much this worked great. For some reason it would stick at cat >> .ssh/authorized_keys, so I just set it to the lines that I had and it worked great. Thanks so much! – Marc Jan 23 '12 at 10:00
Actually I'm going to have to go with the answer below. didn't know this was already implemented. – Marc Jan 23 '12 at 10:06
@Marc its bad etiquette to radically change someones answer like that. The solution I proposed removed the need for typing the password multiple times, removed the need for having a temporary file, and removed superfluous commands. – Patrick Feb 25 '12 at 22:37
Sorry Patrick. I really should have appended it to the end of the post. I completely understand though. – Marc Feb 28 '12 at 1:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.