2

I want to setup my home server to use ssh keys but when I follow the tutorial on https://help.ubuntu.com/10.04/serverguide/C/openssh-server.html for setting up ssh keys I get this:

Bad port 'umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys'

I get past the part where you create the key using:

ssh-keygen -t dsa

but the part that asks me to do this:

ssh-copy-id user@servername

is when I get the error above.

Thanks for the help.

3
  • Can you currently ssh into the machine in question using a password? The ssh-copy-id script depends on that capability to work.
    – teeks99
    Jan 22, 2011 at 23:24
  • I am having a similar issue (and error) and it turns out that, in my case, the target machine does not have a ~/.ssh folder and ssh-copy-id does not auto-create it either - thus the error. When I manually ssh in with the password and create the folder, the command works fine. Jan 6, 2017 at 0:16
  • Additionally, by passing extra options to the ssh-copy-id command (-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PubkeyAuthentication=no) it tripped one of the other options in the script which caused it to skip the important "mkdir ~/.ssh" bit. I removed those extra options and the command worked fine. Jan 6, 2017 at 0:23

2 Answers 2

4

I'm not sure why that script isn't working for you, but it's just a simple helper script. Try doing it manually (and if there is a problem other than just with the script, it may help you diagnose the issue).

The steps to do it the long way are:

  1. copy your key to the remote server with scp ~/.ssh/id_dsa.pub user@servername:~/
  2. ssh to the remote server with ssh user@servername
  3. make sure the .ssh directory exists and has the correct permissions by invoking ssh with something like ssh localhost
  4. put your key in the correct place with cat ~/id_dsa.pub >> ~/.ssh/authorized_keys

(you can then delete the id_dsa.pub file in your home directory on the target machine)

This assumes that you have a dsa key -- if you have an rsa key, use id_rsa.pub in all cases.

There are some complex ways to do this in a single line, but if you have an issue, they won't be any more useful than the script in helping you figure it out.

3

I got the same error message when I tried to log in with a non-standard port.

To debug, I ran the script, prefixing bash -x. Here's where I ran into issues:

+ ssh -p8882 'umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys'

So it took the command line option '-p8882' and tried to use that as the machine to try to reach. Needless to say it failed. I'd have to see your output to isolate your issue further though.

As teeks99 said, make certain you can log in with your password first though. This should isolate "can't log in with ssh" issues from "ssh-copy-id is broken somehow" issues.

Edited to add:

you can get around some of the issues of not being able to pass any additional arguments to ssh by creating or adding to a file called ~/.ssh/config:

Host shortname
    HostName ec2-0-0-0-0.compute-1.amazonaws.com
    Port  8882
    IdentityFile ~/.ssh/nonstandard.name
    User ubuntu

man 5 ssh_config for details

With roughly this setup, I got the script to work by just issuing ssh-copy-id shortname

You must log in to answer this question.