Public key authentication over SSH
I’m using Xubuntu 12.04.03 32bit
If you want to use public key authentication over SSH in your server you can just change localhost with your servers IP address.
First you need to install openssh-server.
$ sudo apt-get update && sudo apt-get install -y openssh-server
Make user and test your ssh works.
$ sudo adduser soivite01
$ ssh soivite01@localhost whoami;
soivite01
Make public and private keys.
$ ssh-keygen
You can leave these questions empty if you don’t want to change the directory where keys are saved and if you dont’ want to use passphrase when you take ssh connection.
Generating public/private rsa key pair.
Enter file in which to save the key (/home/feelix/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Copy public key to user and test it works
$ ssh-copy-id soivite01@localhost
$ ssh soivite01@localhost whoami;
soivite01
Make three new users
$ sudo adduser soivite02
$ sudo adduser soivite03
$ sudo adduser soivite04
Let’s make public key copying to new users in loop.
$ for H in 02 03 04; do ssh-copy-id soivite$H@localhost; done
soivite02@localhost's password:
Now try logging into the machine, with "ssh 'soivite02@localhost'", and check in:
~/.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
.....
Let’s make shell script that connects to all users and makes whoami so we can confirm that users and ssh connection is working.
$ nano sshPublicTest.sh
#!/bin/bash/
for H in 01 02 03 04
do
ssh soivite$H@localhost whoami
done
Run shell script
$ sh sshPublicTest.sh
soivite01
soivite02
soivite03
soivite04
Now you are successfully using public key authentication over SSH with four different users.
Or without do - done
$ nano sshPublicTest.sh
#!/bin/bash/
for H in 01 02 03 04;
{
ssh soivite$H@localhost whoami;
}
Run script
$ bash sshPublicTest.sh
soivite01
soivite02
soivite03
soivite04