SSH Authorized Keys Example Usage


SSH (Secure Shell) is a protocol used to remotely and safely (encrypted) get entry to techniques. The SSH server runs at the faraway gadget, and the SSH shopper for your running gadget. Communication between shopper and server is standard by way of the command line. Now, there are a number of techniques of authenticating the relationship – password authentication, public/personal key-based authentication (the usage of the authorized_key record), and host-based authentication (the usage of the known_host record).

  1. In the password-based authentication approach, a password is needed to login. Passwords will also be lengthy and tedious to bear in mind; then again, worse even, they may be able to be brute-forced (hacked)! Simple python scripts can bruteforce even the worst of passwords, and as such, they pose a safety chance.
  2. In the general public/personal key-based authentication, no password is needed to login (a password-less login). In truth, key-based authentication is more secure than password authentication as a result of there is not any want to sort a password. In such instances, the server merely verifies that you’ve got a non-public key! This personal secret is a record and thus will also be copied (safety chance); then again, it’s a lot more potent and longer than an 8-character password. Further, the authorized_keys record is used to authenticate customers through the server.
  3. In the recognized host-based authentication approach, the recognized host record incorporates the hosts which are allowed to glue. The known_hosts record is used to authenticate servers through the customers.

In this educational, we can be taking a look at methods to arrange the general public/personal key-based authentication and check out the authorized_keys record and its makes use of.


When putting in sophisticated techniques akin to those, we want to make sure that the configuration recordsdata are correctly configured! If they aren’t, all of the procedure is not going to paintings! Now, there are two techniques right here – the buyer and the server. The rec/ssh/sshd_config at the server at the server Do uncomment and configure them as follows:


PasswordAuthentication sure

ChallengeResponseAuthentication no

Next, we want to style private and non-private keys. To generate the keys, run (at the shopper gadget):

When you run ssh-keygen, you’ll be brought on with a couple of questions. The first query would be the location the place you need to save lots of the keys. If you permit this clean, it is going to reserve it within the default folder. In my case,that’s the /house/shopper/.ssh/id_rsa, the place id_rsa is the real personal key, and the .ssh is the folder. Next, you’ll be brought on to go into a passphrase. You don’t have to go into a passphrase, however this provides every other layer of safety. The passphrase is used to encrypt the personal key.

This will create a public key and a non-public key.

~/.ssh/id_rsa (personal key)

~/.ssh/ (public key)

The dot ssh signifies that it’s a hidden folder through default. Further, the general public secret is used for encryption, whilst the personal secret is used for decryption. And although the general public key will also be bandied about all over and any place, the personal key will have to be saved protected! Your personal key will have to stay inside your community all the time! If you lose your personal key, chances are you’ll as neatly suppose that your gadget has been compromised. It’s worse than dropping your password as it’s a password-less login).

Next, we want to reproduction the general public key to the server, and for that, we use the next code (which is administered at the shopper gadget):

For instance, in my case, I’d write:

Ex: ssh-copy-id server@

Ssh-copy-id   is such that servername is the title of the server, and ip is its ip deal with. In this situation, “serve” is the title of my server, and is its ip deal with. When the former code is inputted into the buyer gadget, the buyer will instructed for the server’s password, do enter it. It will reproduction the general public key to the server at ~/.ssh/authorized_keys and due to this fact show”Number of keys added: “ for your shopper gadget.

The shopper gadget can even ask you to try a login the usage of:

ssh server@ip>

(ex: ssh server@

The 2d the general public secret is copied onto the server, a record referred to as authorized_keys will probably be created with the general public key inside it. As you’ll see within the following photos, right here’s a hidden folder referred to as /.ssh received my server; when the authorized_keys record is opened, you’ll see the general public key that we generated inside it.

Though this procedure appears to be somewhat easy, you’ll and most certainly will come throughout numerous mistakes whilst putting in the key-based authentication procedure. One, particularly, is the next:

Error“Agent admitted failure to sign using the key. Permission denied. (publickey”

You would possibly get this mistake after copying the general public key to the authorized_keys record. Use the next code at the shopper gadget to mend it:

Once the entirety has been arrange, you currently want to disable the Password Authentication for your server gadget. This is finished through going into the /and so on/ssh/sshd_config record for your server and surroundings the PasswordAuthentication to no:

PasswordAuthentication no

Once you place the password authentication to no, should you attempt to login by way of ssh, you will have to be robotically logged in. (Please notice that I didn’t set a passphrase.)

Regardless of the kind of key you utilize (ex: rsa, ecdsa, and so on.), to make use of key-based authentication, the general public key generated will have to be copied onto the server’s authorized_keys record. Typically, if this record doesn’t exist, the server will try password authentication. Please additionally keep in mind that each and every public secret is saved in one line within the authorized_keys record. Do additionally take note to offer the /.ssh folder, the personal/public keys, and the authorized_keys record the correct permissions – you and also you by myself will have to be capable to mess with it. Note that you’ll reproduction the general public key manually into the /.ssh folder as neatly, and if achieved manually, the correct permissions are crucial a part of the method.

In the development that you simply upload a 2d public key manually into the authorized_keys record, finish the road with a “newlin” or a go back. If you don’t, it is going to suppose that the 2 distinct keys are a unmarried key, and neither will paintings.

The /.ssh listing will have to have the next permission:

The authorized_keys record will have to have the next permission:

chmod 600 ~/.ssh/authorized_keys

The public key will have to have the next permission:

chmod 644 ~/.ssh/

The personal key will have to have the next permission:

You too can grant different customers get entry to on your server. For this, you merely get their public key and position it into the authorized_keys record (in a brand new line). The latter will grant them get entry to on your server.

Typically, when key-based authentication is about up, the consumer can get entry to the faraway gadget with absolutely useful instructions. However, you’ll prohibit get entry to to a unmarried command that you need through the usage of the authorized_keys record. This is known as “pressured command“.

This is the layout of the authorized_keys record if you wish to drive a command:

command> ssh public key> remark>


Command=”date” ssh-rsa AASASA[]

In my instance, I positioned the command “date” in entrance of the general public key within the authorized_keys record (see within the image under). The results of this added command to the authorized_keys record is that I handiest get the date on my shopper gadget. The command you specified, and handiest that command will then be achieved or allowed.

The problem to the pressured command within the authorized_keys record is that you’ll normally handiest put one command consistent with licensed public key. To bypass this, you’ll desire a bash script. If you’re coping with a bash script, you are going to use the next notation:

command=location of bash script> ssh public key> remark>

Suppose that I write a script referred to as (that is simply an instance script):


PS3=‘Choose your choice: ‘

possible choices=(“get the date” “make a directory” “make a file” “exit”)

choose choose in ${choices[@]}; do

    case $choose in

        “get the date”)

            CURRENTDATE=`date +“%Y-%m-%d %T”`

            echo ${CURRENTDATE}


        “make a directory”)

            echo “what is the name of the directory?”

            learn nameDir

            mkdir $nameDir


        “make a file”)

            echo “Enter the text you want to place in file”

            learn textual content

            echo “Name of the file please”

            learn recordName

            echo $textual content >> $recordName




            echo “Good bye! See you again soon!”

            go out


        *) echo “invalid option $REPLY;;



The subsequent step is to make this record executable through typing the next:

Please notice that if you don’t make this record executable, the method will throw an error! Here, you’d position the record you simply created in ~/.ssh as ~/.ssh/, and write the next within the authorized_key record:


Command=”/house/server/.ssh/” ssh-rsa AASASA[]

The result’s as follows:

When the (executable) record is positioned within the ~/.ssh folder (~/.ssh/, and that the authorized_keys record is changed, you will have to see the result of the bash script at the shopper gadget (as within the symbol above). And that’s it! Easy, breezy, gorgeous code!

Key-based authentication is a straightforward, fast, and protected technique to login on your faraway gadget the usage of ssh. In specific, the authorized_keys record is of significant use in authenticating the consumer and specifying which instructions are accepted through the consumer.

Happy Coding!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More