Cisco: Authenticating with SSH public key on Cisco IOS devices

Using SSH public keys to authenticate on a Cisco IOS device

The preferred way to authenticate on a network device is using a SSH key. If you don't have a SSH key already, take a look at the steps to generate a new key here.
After the key is available, you have to connected to your Cisco device and install it.

But, first of all, you need to make sure that SSH is enabled on your device.

1st Step - Enable SSH

To enable SSH on your device, you have to define the domain name and hostname of the device. Also you need to generate the host keys and finally to enable ssh:

Router>ena
Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip domain-name johnyc20.blogspot.ro
Router(config)#hostname bucharest
bucharest(config)#crypto key generate rsa
The name for the keys will be: bucharest.johnyc20.blogspot.ro
Choose the size of the key modulus in the range of 360 to 4096 for your
  General Purpose Keys. Choosing a key modulus greater than 512 may take
  a few minutes.

How many bits in the modulus [512]: 2048
% Generating 2048 bit RSA keys, keys will be non-exportable...
[OK] (elapsed time was 23 seconds)

bucuresti(config)#ip ssh version 2
At this point, the SSH server should run on your device and you can test that by using the following command:
bucuresti#show ip ssh
SSH Enabled - version 2.0
Authentication timeout: 120 secs; Authentication retries: 3

 2st Step - create user(s)

The second step is to create user(s), if they doesn't exist already. You can do that by using the "username" command:
bucuresti#show running-config | include username
bucuresti#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
bucuresti(config)#username smocanu privilege 15 secret 0 Vicfief3
To generate a strong password use a password generator, like 'apg' .

3rd Step - install user public key

The installation of the key is quite simple. The only thing that you should be aware of is that the number of characters of a line is limited. I think a maximum of 256 charactes can be inserted into one line, so you have to split your key into a few parts and insert each part on a line:
bucharest(config)#ip ssh pubkey-chain
bucharest(conf-ssh-pubkey)#username smocanu
bucharest(conf-ssh-pubkey-user)#key-string
bucharest(conf-ssh-pubkey-data)#$+1psZudur5dMoF0nGpG71JMwzIGQ206TW9YAw2
bucharest(conf-ssh-pubkey-data)#$O59TcgxkiiGZ0SARKSRtDBRtuK+wYgrN6zGni9
bucharest(conf-ssh-pubkey-data)#$hoJNRsfIHZlLftJHqnUJRPm5yLRY5e3wBmtUkb
bucharest(conf-ssh-pubkey-data)#$/9oPkwf0BSIWCgowv/Dzz/6H5za3rhlFsFZf+s
bucharest(conf-ssh-pubkey-data)#$F3MQ0di58nQU= smocanu@johnyc20.blogspot.ro
bucharest(conf-ssh-pubkey-data)#exit
bucharest(conf-ssh-pubkey-user)#exit
bucharest(conf-ssh-pubkey)#exit
bucharest(config)#do show runn | begin ip ssh
ip ssh version 2
ip ssh pubkey-chain
  username smocanu
   key-hash ssh-rsa 5464E66EA16FD338E389253F868DAE25 smocanu@johnyc20.blogspot.ro

At this point you should be able to authenticate yourself using your private key but don't forget to make your changes permanent by saving the configuration of the device:
bucharest(config)#end
*Jun 13 07:28:31.207: %SYS-5-CONFIG_I: Configured from console by console
bucharest#write memory
Building configuration...
[OK]
bucharest#

Comments

Popular posts from this blog

JunOS - mount USB stick

SSH: Generating and using SSH keys

Linux: Versioning the /etc/ configurations using 'etckeeper'