Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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/id_rsa.pub user@
Bad port 'umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys'

user@box:~$ ssh-copy-id '-p 57777' -i ~/.ssh/id_rsa.pub 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

2 Answers 2

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

ssh-copy-id -i /home/user/.ssh/id_rsa.pub '-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

For those that want a replacement script here is my ssh-copy-id.sh

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

Now you can just ssh-copy-id.sh blah@stuff.com -p123456

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

share|improve this answer

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.