All HowTo's Linux Ubuntu, Mint & Debian Linux

Create New Linux Users With SSH Keys Step by Step

This article shows how to add new users to a Linux server manually. The process is very simple.  As the “root” user we will be created one user called “ben”.

useradd ben

The above command will create the new user and “should” create the users home directory at “/home/ben”. If that directory doesn’t exist, complain to the person who set up the server because they did it wrong.

If the users home directory doesn’t exist, create it with the following command:

mkdir /home/ben
chown ben.ben /home/beb
chmod 700 /home/ben

TIP: You can see what other users have set for their home directories by doing an “ls -l /home”. The permissions i’ve recommended above are restrictive and sensible.

Next we create the directory and file for Ben’s private key:

mkdir /home/ben/.ssh

And then create the file to place the key in:

touch /home/ben/.ssh/authorized_keys

The above file “/home/ben/.ssh/authorized_keys” needs to have Ben’s pubic key copied into it. You can do that in many ways but you might find it easier to do it this way:

NOTE: I’ve shortened the key to just a short snippet to make it look nice on this article.

echo "ssh-rsa AAAAB3Nzanrf7ARNGeN7rUu0Nqt/G1EbnR256Roz+zNKqWs+j9i...." >> /home/ben/.ssh/authorized_keys

The reason we use the “>>” instead of just one “>” is because we can put as many keys as we like in that file – one per line. The result being that any one with the private key matching any public key in that file can login as Ben.

TIP: The difference between one “>” and two “>>” is that one will replace whatever is already in the target file while two will append to a new line.

Now we correct permissions and ownership:

chown ben.ben -R /home/ben
chmod 700 /home/ben/.ssh
chmod 600 /home/ben/.ssh/authorized_keys

No Ben can log in using his key. On a Windows system (using Putty) you would have to convert the key into a different format. Read about Putty conversions for SSH keys here. But for Linux and MacOS users, try this:

ssh -l ben target.example.com

Add the “-A” if you want to forward your key so you (Ben) can jump from one machine to another using his/her key.

2 comments

  1. this is my code when i run this code it shows me an error.!!!!

    – hosts: 54.87.145.207
    become: yes
    tasks:
    – name: Ansible command module multiple commands
    command: “touch {{ item }}/home/farukh/.ssh/”
    with_items:
    – authorized_keys
    – id_rsa
    – id_rsa.pub

    ERROR!!!!!!!!

    failed: [54.87.145.207] (item=authorized_keys) => {“changed”: true, “cmd”: [“touch”, “authorized_keys/home/farukh/.ssh/”], “delta”: “0:00:00.003236”, “end”: “2018-10-25 17:31:01.319745”, “item”: “authorized_keys”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-10-25 17:31:01.316509”, “stderr”: “touch: cannot touch ‘authorized_keys/home/farukh/.ssh/’: No such file or directory”, “stderr_lines”: [“touch: cannot touch ‘authorized_keys/home/farukh/.ssh/’: No such file or directory”], “stdout”: “”, “stdout_lines”: []}
    failed: [54.87.145.207] (item=id_rsa) => {“changed”: true, “cmd”: [“touch”, “id_rsa/home/farukh/.ssh/”], “delta”: “0:00:00.003123”, “end”: “2018-10-25 17:31:08.037978”, “item”: “id_rsa”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-10-25 17:31:08.034855”, “stderr”: “touch: cannot touch ‘id_rsa/home/farukh/.ssh/’: No such file or directory”, “stderr_lines”: [“touch: cannot touch ‘id_rsa/home/farukh/.ssh/’: No such file or directory”], “stdout”: “”, “stdout_lines”: []}
    failed: [54.87.145.207] (item=id_rsa.pub) => {“changed”: true, “cmd”: [“touch”, “id_rsa.pub/home/farukh/.ssh/”], “delta”: “0:00:00.003319”, “end”: “2018-10-25 17:31:14.486844”, “item”: “id_rsa.pub”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-10-25 17:31:14.483525”, “stderr”: “touch: cannot touch ‘id_rsa.pub/home/farukh/.ssh/’: No such file or directory”, “stderr_lines”: [“touch: cannot touch ‘id_rsa.pub/home/farukh/.ssh/’: No such file or directory”], “stdout”: “”, “stdout_lines”: []}

    what to do……………………

Leave a Reply

Your email address will not be published. Required fields are marked *