![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
Tutorials & Tips The place to find Ubuntu related Tips & Tricks. |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
A Carafe of Ubuntu
![]() |
HOWTO: SSH & Public Keys
Since SSH (Secure Shell) scans are so common anymore I wanted to add better protection to my server so I configured SSH to only allow logins with public & private keys instead of password authentication. This is how I set it up on Ubuntu however it should work on any version of Linux. Don't be afraid of the length of this tutorial it's really pretty simple and only a few commands. This HowTo ended up longer than I anticipated because I wanted to explain each step as best I could.
This HowTo assumes that you already have SSH installed properly. The first thing we need to do is generate the key pair. On your host computer go to "Applications">"System Tools">"Terminal" note this is your regular user terminal not a root terminal. Enter the following command at the terminal. Code:
username@ubuntu:~$ ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/home/username/.ssh/id_dsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/username/.ssh/id_dsa. Your public key has been saved in /home/username/.ssh/id_dsa.pub. The key fingerprint is: 5b:ab:73:32:9e:b8:8c:4b:29:dc:2a:2b:8c:2f:4e:45 username@ubuntu This will generate a DSA key pair. If you notice I say pair it generates a private key id_dsa and your public key id_dsa.pub which we will copy to the server. Next we need to copy the public key to the server. Code:
username@ubuntu:~$ cd .ssh/ Code:
username@ubuntu:~$ scp id_dsa.pub serverusername@192.168.1.40:./id_dsa.pub id_dsa.pub 100% |*****************************************************| 614 00:00 Now that we copied the public key to the server we have to install the key in the proper directory. To do this login to the server using ssh and your usual password. We still aren't using public key authentication yet but we are close. Once logged into the server issue the following command in the terminal. Note you don't need to be logged in as root just login with your normal user account. Code:
username@server:~$ cd .ssh serverusername@server:~$ touch authorized_keys2 serverusername@server:~$ chmod 600 authorized_keys2 serverusername@server:~$ cat ../id_dsa.pub >> authorized_keys2 serverusername@server:~$ rm ../id_dsa.pub Code:
username@ubuntu:~$ ssh -l serverusername 192.168.1.40 Enter passphrase for key '/home/serverusername/.ssh/id_dsa': Linux everest 2.6.10-5-386 #1 Tue Apr 5 12:12:40 UTC 2005 i686 GNU/Linux The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. You have new mail. Last login: Mon Apr 25 19:43:43 2005 from 192.168.1.15 serverusername@everest:~$ Code:
# PasswordAuthentication yes Change to: PasswordAuthentication no UsePAM no
__________________
"Beer is proof that God loves us and wants us to prosper." - Ben Franklin "Never argue with an idiot; they'll drag you down to their level and beat you with experience." Last edited by Beernut; May 4th, 2005 at 12:32 AM.. Reason: Fixed TYPO Added UsePAM no option |
|
|
|
|
|
#2 | |
|
First Cup of Ubuntu
![]() Join Date: Apr 2005
Location: New Zealand
Beans: 9
|
Re: HOWTO: SSH & Public Keys
Quote:
I'm glad you said that the user must enter a password. IMO, if you don't passphrase your key, you're asking for trouble You might also like to add something about ssh-add as it saves you from entering your passphrase everywhere. |
|
|
|
|
|
|
#3 |
|
A Carafe of Ubuntu
![]() Join Date: Mar 2005
Beans: 143
|
Re: HOWTO: SSH & Public Keys
Thanks for the howto! But how do I disable regular password authentication now that this is in place?
__________________
"Windows is something to overcome" Howto's by me: Tweak firefox! (URL now works..) Backup/Restore your system! Avoid having to reboot Compentux.org , the Linux Tip & Howto gathering initiative! |
|
|
|
|
|
#4 | |
|
A Carafe of Ubuntu
![]() |
Quote:
Code:
sudo vim /etc/ssh/ssh_config sudo gedit /etc/ssh/ssh_config (Enter this one only if you are on the server.) When you open the file all you have to do is change the following line to no. Code:
# PasswordAuthentication no
__________________
"Beer is proof that God loves us and wants us to prosper." - Ben Franklin "Never argue with an idiot; they'll drag you down to their level and beat you with experience." Last edited by Beernut; May 1st, 2005 at 09:57 AM.. |
|
|
|
|
|
|
#5 | |
|
A Carafe of Ubuntu
![]() |
Quote:
I hate blank passwords or passphrases. Why go through the trouble of securing you server and then leave that out? Thanks for the hint on ssh-add command I'll have to look into it. Does that just remember your password for the current session?
__________________
"Beer is proof that God loves us and wants us to prosper." - Ben Franklin "Never argue with an idiot; they'll drag you down to their level and beat you with experience." |
|
|
|
|
|
|
#6 | |
|
First Cup of Ubuntu
![]() Join Date: Apr 2005
Location: New Zealand
Beans: 9
|
Re: HOWTO: SSH & Public Keys
Quote:
When turning off password auth, I found that my debian testing version of sshd already had "PasswordAuthentication no". To really turn it off, you need to set "UsePAM no" (as sshd uses pam instead of doing the authentication itself). |
|
|
|
|
|
|
#7 |
|
A Carafe of Ubuntu
![]() Join Date: Mar 2005
Beans: 143
|
Re: HOWTO: SSH & Public Keys
Hey, i'm probably doing something wrong here,but i've set password authentication to 'no' on my (Gentoo) server in the /etc/ssh/ssh_config file, but I can still logg in with my regular password if I just hit enter when it asks me for the passphrase for the key. any idea what might be causing this?
__________________
"Windows is something to overcome" Howto's by me: Tweak firefox! (URL now works..) Backup/Restore your system! Avoid having to reboot Compentux.org , the Linux Tip & Howto gathering initiative! |
|
|
|
|
|
#8 | |
|
First Cup of Ubuntu
![]() Join Date: Mar 2005
Beans: 2
|
Re: HOWTO: SSH & Public Keys
Quote:
EDIT: wait a sec..whay are we setting up "password authentication no" in ssh_config? Isn't it sshd_config we should be changing? Last edited by heon2574; May 3rd, 2005 at 09:04 PM.. |
|
|
|
|
|
|
#9 |
|
A Carafe of Ubuntu
![]() |
Re: HOWTO: SSH & Public Keys
OOPS Missed the comment that's the bad thing with copy and paste. As far as changing it in the sshd_config file I don't have one on my system. At least not in /etc/ssh/ which is where it should be according to the documentaion at OpenSSH.
Also I don't see the anything about the "UsePAM no" option in the manual. Code:
man ssh_config
__________________
"Beer is proof that God loves us and wants us to prosper." - Ben Franklin "Never argue with an idiot; they'll drag you down to their level and beat you with experience." |
|
|
|
|
|
#10 |
|
Just Give Me the Beans!
![]() Join Date: May 2005
Beans: 46
|
Re: HOWTO: SSH & Public Keys
Great howto. Very easy to follow. I have only one question: You explained how to generate the keypair on two Ubuntu boxes, I'm curious. I run the SSHD at home on my ubuntu box but often connect from my workplace using Putty. How does one go about generating another keypair on windows with Putty?
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|