How to use ssh-copy-id on a non-standard port
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 [email protected]
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 [email protected]
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 :) )
Solution 1:
Quoting was already a good idea but rather do it like this:
ssh-copy-id -i /home/user/.ssh/id_rsa.pub '-p 57777 [email protected]'
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)
Solution 2:
For those that want a replacement script here is my ssh-copy-id.sh
#!/bin/bash
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 [email protected] -p123456
Obviously its not as fancy as the real one but at least it works for the logged in user.