44

ssh-copy-id can be used to install your public key in a remote machine's authorized_keys. Could the same command be used to install other users' public keys, if you have sudo ability?

Update: both local and remote are using Ubuntu 12.04.

Update 2: describing the procedure of creating a new user account and adding public key

  1. (remote) Create a new user account, and set it to user public key access only.
  2. (local) Generate a public key for the new user account (ssh-keygen).
  3. Normally I do is to create the directory and file .ssh/authorized_keys on the remote server, then copy and paste the public key generated locally to the new user's account. What I am looking for is that if I can use ssh-copy-id to install this newly created user's public key directly into the ssh directory. Just to save a couple more commands.
2
  • 7 years. any updates on how to do this in one line?
    – skywinder
    Commented May 1, 2020 at 18:31
  • Save yourselves the hassle and use the webmin plugin to do it with a gui
    – Jonathan
    Commented Jun 12, 2021 at 6:46

3 Answers 3

27

Not the same command but if you can use sudo on the remote host, you can use ssh to remotely do the required steps. I use the following command to push my ssh key to my raspberry's root user:

cat ~/.ssh/id_rsa.pub | \
  ssh [email protected] \
  "sudo mkdir -p /root/.ssh; sudo tee -a /root/.ssh/authorized_keys"
  • cats my public key
  • pipes it to ssh
  • ssh connects to my raspberry as ssh user
  • on remote uses sudo to create /root/.ssh
  • then uses sudo with "tee -a" to append stdin (which holds the key from first cat) to /root/.ssh/authorized_keys

Just put this stuff together as a script, maybe add some chmod/chown on the remote side and you have what you need.

5
  • Beautiful and thank you for the command breakdown. Commented Oct 10, 2019 at 22:11
  • 2
    cat ~/.ssh/id_rsa.pub | ssh [email protected] "sudo cat >> /root/.ssh/authorized_keys" is for you if .ssh exists.
    – Timo
    Commented Nov 5, 2020 at 17:35
  • 2
    Don't you have to set permissions as well of the dir & key?
    – Jonathan
    Commented Jun 11, 2021 at 2:22
  • 2
    @Timo sudo cat >> is unlikely to work. It would run cat as root which reads from pipe and writes to pipe. the pipe is read by the shell spawned by ssh which runs as "pi" user and has no write permission to the target folder. That's why tee is used here and in most similar examples. Commented Sep 23, 2022 at 10:10
  • In a MINGW64 bash terminal on Windows 10, using this script, I get this error: [email protected]'s password: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper sudo: a password is required sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper sudo: a password is required It does prompt me for a password and I entered it
    – dlatikay
    Commented Jun 21 at 8:26
2

In my case:

  • the root user can already login via ssh (password or ssh-key)
  • the user svruser already exists on the server and has passwordless sudo rights
  • I want to allow the svruser to login via ssh-key: i.e. upload the public ssh key, that is stored on my local PC to the remote server and append it to the authorized_keys file

This command worked for me:

cat LOCAL/PATH/my-ssh.pub | ssh [email protected] \
"sudo -u svruser bash -c 'mkdir -p ~/.ssh; chmod 700 ~/.ssh; tee -a ~/.ssh/authorized_keys'"

Details

  • execute this on my local PC in a terminal where ssh is installed (e.g. Linux terminal, WSL, or git-bash on windows)
  • cat LOCAL/PATH/my-ssh.pub just prints the file contents of my public ssh key to the terminal
  • | pipes the text of the ssh-key to the next (ssh) command
  • ssh [email protected]: connect via ssh as user root to my host
    • sudo -u svruser: execute the following command as user svruser
      • bash -c '..' executes the text inside of the single quotes in a bash hell
        • mkdir -p ~/.ssh create the (hidden) .ssh dir in the home-directory of the svruser
        • chmod 700 ~/.ssh: set the right permissions to the new dir: see Permissions on private key in .ssh folder?
        • tee -a ~/.ssh/authorized_keys: prints the ssh-key text (that we piped to the ssh command) to the terminal and appends the text to the authorized_keys file
0

Provided that we have access to the server at ADDRESS with the root user and the user we want to set up keys for is called ubuntu, the following one-liner can be used.

cat ~/.ssh/id_rsa.pub | ssh root@ADDRESS "su - ubuntu -c 'mkdir -p ~/.ssh && tee -a ~/.ssh/authorized_keys'"

This will work regardless whether .ssh and authorized_keys exist or not.

2
  • Don't you have to set permissions as well of the dir & key?
    – Jonathan
    Commented Jun 11, 2021 at 2:23
  • I checked it. It works like this, because you are running mkdir and tee as the ubuntu user to whom the keys should be assigned. So ubuntu will be the owner of .ssh and authorized_keys.
    – Vic
    Commented Jun 11, 2021 at 8:19

You must log in to answer this question.

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