Skip to content

Trevweb

Computing, education and assorted other bits.

  • Home
  • About Me

Tag: login

SSH Authentication With Keys

SSH Authentication With Keys

I recently posted about disabling the root user on Ubuntu. A seemingly obvious next step might be to avoid having to use passwords altogether. I use SSH keys to monitor my servers. Non-Exhaustive benefits (assuming that you keep your keys safe):

  • SSH keys are a lot less likely to be brute forced than a password.
  • Password authentication can be disabled if appropriate.
  • Easier to setup scripts that connect to other servers E.g. rsync backup without supplying authentication credentials.
  • Allow multiple people to share access to a system user without sharing the password.
  • Remove access for an individual without altering the system configuration.

Generating a Key

Note: If you already have a key pair that you wish to use, skip to Configure a Remote User. You might have created a key already that you wish to retain, perhaps for a code repository or other service using SSH.

The key I refer to in the title is actually a key pair; public and private components. The key pair will be generated on the client; probably the computer you’re using (*nix based systems). I used my iMac for this post.

Trevs-iMac:~ trevor$ ssh-keygen -t rsa

You will likely be prompted for a file location to save your key pair. The default will likely be offered to you as the path .ssh/id_rsa within your home directory.

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/trevor/.ssh/id_rsa):

Upon entering your own file name or accepting the default (enter without input), you will be given the opportunity to enter a passphrase.

Enter passphrase (empty for no passphrase):

A passphrase is optional; there are benefits to having one. Chiefly, if the key is compromised and in the hands of another it will be ineffective until the passphrase is also known. However, it does mean that you will need to type it when invoking the key for authentication; making unattended use less convenient.

Regardless of your decision concerning passphrase, your files will be located in the path you supplied; with the public component having the same name with a .pub extension.

You now have a key pair to use for authentication on a remote system.

Configure a Remote User

Using the key pair for the purposes of authentication is a straightforward exercise. You should keep your private key, private. The public key (contained in the file created with the .pub extension) is all that is required for the remote system.

If you are using a Linux based OS (OS X El Capitan does not have this command for some reason) you will likely have a terminal command that will achieve the results in one hit:

ssh-copy-id trevor@host.domain.co.uk

Where trevor is to be replaced with the user on the remote system.

Alternatively, log into the remote system and append the contents of your public key file into ~/.ssh/authorized_keys.

You can store as many public keys in this file as you wish, simply append them to the file.

Test the Configuration

You should now be able to test the connection from your client terminal. Simply enter (replacing trevor with your username):

Trevs-iMac:~ trevor$ ssh trevor@host.domain.co.uk

If you opted to use a passphrase, you will be prompted to enter it. You should be able to establish a secure connection to your remote system without requiring input of additional credentials.

Conclusion

Connecting via SSH with an authorised key offers some advantages. It keeps from having to type passwords into terminals or other input mechanisms. Password authentication could be disabled entirely, only allowing connections authenticated with public/private key.

Writing your own backup scripts on your development/hobby servers is simple and will not require a password to be stored or entered.

Consider using a passphrase if you cannot adequately secure your private key. You will gain more security at the expense of convenience.

Acknowledgement

SSH featured image.

Author TrevorPosted on June 8, 2016June 8, 2016Categories UbuntuTags authentication, key, login, ssh, sysadmin, ubuntuLeave a comment on SSH Authentication With Keys

Disabling Root User on Ubuntu Server 14.04 LTS

Root is often disabled by default when Ubuntu is installed. However, you might find it active when Ubuntu (or other Linux distribution) is deployed in automated environments E.g. when installed with a Virtual Private Server (VPS).

To disable root and administer the system from other user accounts, consider setting up sudo. As root, add a user to sudo that can execute commands as root:

$ sudo adduser trevor sudo

Note that you should replace trevor with a username on your system. Exit the shell as root and login with the username that you just added to sudo (or any other pre-configured sudoer). Use sudo to disable the root password, thus disabling login.

$ sudo passwd -dl root

Enabling Root

If you are in a circumstance where you need to enable the root account, type the following while logged in as a sudoer:

$ sudo passwd root

You will be prompted to enter and confirm the password for root.

Further Reading

Read the Ubuntu Official Documentation on RootSudo for additional information and a clear explanation on the benefits of disabling root.

Author TrevorPosted on August 14, 2015Categories UbuntuTags 14.04 LTS, disable, enable, login, root, tutorial, ubuntu1 Comment on Disabling Root User on Ubuntu Server 14.04 LTS

Recent Posts

  • Configure SMTP Server on Microsoft Windows Server 2022
  • Kata – Revolver Roulette
  • My Children and Social Media
  • Create a PFX certificate file using OpenSSL
  • SMTP Service Auto-Start on Windows Server 2016

Categories

  • Agile
    • Kata
  • Education
  • Mac
  • Personal
  • Software Development
    • Apple
      • OS X
      • Xcode
    • Clojure
    • Games Programming
    • Java
  • Software Engineering
  • Ubuntu
  • Web Development
    • Generic JavaScript
    • PHP
      • CakePHP
  • Windows
    • Windows Server 2016
    • Windows Server 2022

Top Posts & Pages

  • Create a PFX certificate file using OpenSSL
BCS Professional Member
  • Home
  • About Me
Trevweb Proudly powered by WordPress