Results 1 to 1 of 1
-
4th Apr 2014, 01:49 PM #1
HowTo: Setup SSH Keys on a Linux / Unix System
Tutorial details Difficulty Easy (rss) Root privileges No Requirements OpenSSH client and server Estimated completion time 10m
I am assuming that you are using Linux or Unix-like server and client with the following software:
- OpenSSH SSHD server
- OpenSSH ssh client and friends on Linux (Ubuntu, Debian, {Free,Open,Net}BSD, RHEL, CentOS, OSX and co).
What is a public key authentication?
OpenSSH server supports various authentication schema. The two most popular are as follows:
- Passwords based authentication
- Public key based authentication. It is an alternative security method to using passwords. This method is recommended on a VPS, cloud, dedicated or even home based server.
How do I set up public key authentication?
You must generate both a public and a private key pair. For example:
///////////
//Internet//
////////////
|
+---------------+ | +-------------+
| Unix/Linux | | | Linux/Unix |
| Server with +------+-------+ OSX/*BSD |
| OpenSSH SSHD | | Client |
+---------------+ +-------------+
server1.cyberciti.biz client1.cyberciti.biz
75.126.153.206 192.168.1.42
- server1.cyberciti.biz - You store your public key on the remote hosts and you have an accounts on this Linux/Unix based server.
- client1.cyberciti.biz - Your private key stays on the desktop/laptop/ computer (or local server) you use to connect to server1.cyberciti.biz server. Do not share or give your private file to anyone.
In public key based method you can log into remote hosts and server, and transfer files to them, without using your account passwords. Feel free to replace server1.cyberciti.biz and client1.cyberciti.biz names with your actual setup. Enough talk, let's set up public key authentication. Open the Terminal and type following commands if .ssh directory does not exists:
mkdir -p $HOME/.ssh
chmod 0700 $HOME/.ssh
On the computer (such as client1.cyberciti.biz), generate a key pair for the protocol.
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/vivek/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/vivek/.ssh/id_rsa.
Your public key has been saved in /Users/vivek/.ssh/id_rsa.pub.
The key fingerprint is:
80:5f:25:7c:f4:90:aa:e1:f4:a0:01:43:4e:e8:bc:f5 vivek@desktop01
The key's randomart image is:
+--[ RSA 2048]----+
| oo ...+. |
|.oo . .ooo |
|o .o. . .o . |
| o ...+o. |
| o .=.=S |
| . .Eo . |
| |
| |
| |
+-----------------+
- $HOME/.ssh/id_rsa - contains your private key.
- $HOME/.ssh/id_rsa.pub - contain your public key.
#2: Install the public key in remote server
Use scp or ssh-copy-id command to copy your public key file (e.g., $HOME/.ssh/id_rsa.pub) to your account on the remote server/host (e.g., nixcraft@server1.cyberciti.biz). To do so, enter the following command on your client1.cyberciti.biz:
ssh-copy-id -i $HOME/.ssh/id_rsa.pub user@server1.cyberciti.biz
scp $HOME/.ssh/id_rsa.pub user@server1.cyberciti.biz:~/.ssh/authorized_keys
On some system ssh-copy-id command may not be installed, so use the following commands (when prompted provide the password for remote user account called vivek) to install and append the public key:
## First create .ssh directory on server ##
ssh vivek@server1.cyberciti.biz umask 077; test -d .ssh || mkdir .ssh
## cat local id.rsa.pub file and pipe over ssh to append the public key in remote server ##
cat $HOME/.ssh/id_rsa.pub ssh vivek@server1.cyberciti.biz cat >> .ssh/authorized_keys
The syntax is:
Or copy a text file called foo.txt:
scp foo.txt user@server1.cyberciti.biz:/tmp/
What are ssh-agent and ssh-add, and how do I use them?
To get rid of a passphrase for the current session, add a passphrase to ssh-agent and you will not be prompted for it when using ssh or scp/sftp/rsync to connect to hosts with your public key. The syntax is as follows:
eval $(ssh-agent)
ssh-add
#4: Disable the password based login on a server
Login to your server, type:
$ sudo vim /etc/ssh/sshd_config
$ sudo vim +/PermitRootLogin /etc/ssh/sshd_config
PermitRootLogin no
## CentOS/RHEL/Fedora Linux server reload sshd ##
sudo service sshd reload
## Debian/Ubuntu Linux server reload sshd ##
/etc/init.d/ssh reload
## Generic Unix method to reload sshd ##
kill -HUP `cat /var/run/sshd.pid`
To to change your passphrase type the following command:
ssh-keygen -p
Just copy files to your backup server or external USB pen/hard drive:
## Copy files to home based nas server ##
rsync -avr $HOME/.ssh user@home.nas-server:/path/to/encrpted/nas/partition/
## Copy files to usb pen drive mounted at /mnt/usb ##
cp -avr $HOME/.ssh/ /mnt/usb/backups/GarryField Reviewed by GarryField on . HowTo: Setup SSH Keys on a Linux / Unix System http://www.besthostingforums.com/images/cdn/besthostingforums.com/2014/04/openbsd_logo_sm-1.png Tutorial details Rating: 5Follow Board Rules & Thread Rules.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Linux/Unix: Find Command Ignore Case Insensitive Search
By GarryField in forum Server ManagementReplies: 0Last Post: 4th Apr 2014, 01:43 PM -
Howto setup the perfect server - debian 6 web,mail,openvpn and much more
By syn4pse in forum Tutorials and GuidesReplies: 0Last Post: 3rd Feb 2012, 06:44 PM -
[Howto] Reset SSH passowrd (Linux)
By WRZHost in forum Technical and Security TutorialsReplies: 2Last Post: 14th Nov 2010, 05:50 PM -
[off-topic] In need of a VALID Linux/UNIX VNC License for Ubuntu.
By DXS in forum General DiscussionReplies: 2Last Post: 11th Jan 2010, 11:15 PM -
HOWTO Install cURL in Linux
By BlaZe in forum Technical and Security TutorialsReplies: 1Last Post: 15th Nov 2009, 04:03 PM
themaManager - edit and manage...
Version 4.04 released. Open older version (or...