Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

I'd like to use key-based authentication to login to one of my computers. It has sshd running on a port that is not the default of 22. I want to use ssh-copy-id to add my key to the list of authorised keys. I tried to specify the port using -p, as for the ssh command itself. However I get really strange error messages then.

For example

user@box:~$ ssh-copy-id -p 57777 -i ~/.ssh/ user@
Bad port 'umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys'

user@box:~$ ssh-copy-id '-p 57777' -i ~/.ssh/ user@ 
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: Could not resolve hostname umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys: Name or service not known

How can I make it work? (I know I can manually add the key to the authorized_keys file on the server. But I am lazy and definitely curious how to convince ssh-copy-id to do its job :) )

share|improve this question

Quoting was already a good idea but rather do it like this:

ssh-copy-id -i /home/user/.ssh/ '-p 57777 user@'

Put everything you want to tell the ssh command the script uses at the end and quote all of that. The script expects the last argument to be the hostname and hence copies it at the end of its generated ssh command.

(Apparently this is a known issue. See this bug report and this one)

share|improve this answer
Oh, great! That works! Thanks. Strange though, that it did not get fixed in 10 years. – seya Jul 9 '12 at 12:26
Probably it did not get a high priority as there is a workaround :S – zpea Jul 9 '12 at 12:29
@zpea nice! On an old Fedora here. You made my day easier – lliseil Sep 22 '15 at 19:33

For those that want a replacement script here is my

ssh $* 'mkdir -p ~/.ssh;echo '`cat ~/.ssh/`' >> ~/.ssh/authorized_keys;chmod 700 ~/.ssh;chmod 600 ~/.ssh/authorized_keys'

Now you can just -p123456

Obviously its not as fancy as the real one but at least it works for the logged in user.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .