Skip to content
  • About
  • Friends
  • About
  • Friends
The Blog of Philippmy personal Site of Things
  • About
  • Friends
Written by Philipp on 2011-01-23

setup your public SSH key to another UNIX Host

Security . Snippets

Normally you would prefer to use your public ssh key for login into a remote linux machine.
I created a script to perform the basic steps for inserting your public key into the hosts authorized_keys files.

The script looks like this:

#!/bin/bash

HOST=$1;
echo ">> setup your ssh keys for $HOST"
echo ""
echo ">> creating ssh keys on $HOST if necessary"
echo "(you need to enter your password)"
echo ""
ssh $HOST 'if [ ! -d ~/.ssh  ] ; then ssh-keygen -t rsa; fi'
echo ""
PUBKEY=`cat ~/.ssh/id_dsa.pub`
echo "=========================================================="
echo "your id_dsa.pub:"
echo "$PUBKEY"
echo "=========================================================="
echo ""
echo ">> transfering your public ssh key"
scp ~/.ssh/authorized_keys $HOST:~/.ssh/authorized_keys
ssh $HOST 'chmod 600 ~/.ssh/authorized_keys'
echo ""
echo ">> login with your public key"
echo "(should work without a password)"
ssh $HOST

A typical run might look like this:

imotep:~ philipp$ setupssh philipp@192.168.178.55
>> setup your ssh keys for philipp@192.168.178.55

>> creating ssh keys on philipp@192.168.178.55 if necessary
(you need to enter your password)

The authenticity of host '192.168.178.55 (192.168.178.55)' can't be established.
RSA key fingerprint is ...
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.178.55' (RSA) to the list of known hosts.
philipp@192.168.178.55's password:
Enter file in which to save the key (/home/philipp/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Generating public/private rsa key pair.
Created directory '/home/philipp/.ssh'.
Your identification has been saved in /home/philipp/.ssh/id_rsa.
Your public key has been saved in /home/philipp/.ssh/id_rsa.pub.
The key fingerprint is:
... philipp@debian
The key's randomart image is:
+--[ RSA 2048]----+
|...              |
+-----------------+

==========================================================
your id_dsa.pub:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx......xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
==========================================================

>> transfering your public ssh key
philipp@192.168.178.55's password:
authorized_keys                                                                         100%  610     0.6KB/s   00:00    

>> login with your public key
(should work without a password)
Linux debian 2.6.26-2-amd64 #1 SMP Thu Nov 25 04:30:55 UTC 2010 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jan 23 17:31:16 2011 from imotep.fritz.box
philipp@debian:~$

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook

Like this:

Like Loading...

Related

2 comments

  • wisenheimer has written: 2011-01-25 at 21:16

    Is there any difference to ssh-copy-id ?

    If so what ?

  • philipp has written: 2011-01-28 at 14:54

    i did not now ssh-copy-id yet. I guess a difference would be that above script works also with other unices besides linux :-).

Leave a ReplyCancel reply

Archives

  • August 2025
  • November 2023
  • February 2023
  • January 2023
  • April 2020
  • January 2018
  • December 2017
  • May 2017
  • February 2016
  • September 2015
  • December 2014
  • August 2014
  • June 2014
  • March 2014
  • February 2014
  • September 2013
  • August 2013
  • July 2013
  • November 2012
  • October 2012
  • September 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • January 2011
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • January 2010
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • September 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006

Calendar

January 2011
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  
« Aug   May »

Categories

  • Bash
  • Bochum
  • Build
  • CCC
  • CLI
  • Coderwall
  • Coventry
  • DB
  • Edu
  • Freenas
  • Gitlab
  • Graphics
  • Hacking
  • iOS
  • Java
  • Javascript
  • Mac
  • NAS
  • Network
  • nexenta
  • Perl
  • Personal
  • PHP
  • Play! Framework
  • Proxmox
  • ruby
  • Ruby on Rails
  • Security
  • SmartOS
  • Snippets
  • Sound
  • Tech
  • Testing
  • Tooling
  • Twitter
  • UI
  • Uncategorized
  • Video
  • Virtualisierung
  • ZFS

Copyright The Blog of Philipp 2026 | Theme by ThemeinProgress | Proudly powered by WordPress

%d