Reason: The intro and Background section ignore the server perspective. (Discuss in )SSH keys can serve as a means of identifying yourself to an SSH server using. The major advantage of key-based authentication is that in contrast to password authentication it is not prone to and you do not expose valid credentials, if the server has been compromised.Furthermore SSH key authentication can be more convenient than the more traditional password authentication. When used with a program known as an SSH agent, SSH keys can allow you to connect to a server, or multiple servers, without having to remember or enter your password for each system.Key-based authentication is not without its drawbacks and may not be appropriate for all environments, but in many circumstances it can offer some strong advantages. A general understanding of how SSH keys work will help you decide how and when to use them to meet your needs.This article assumes you already have a basic understanding of the protocol and have the package. Contents.BackgroundSSH keys are always generated in pairs with one known as the private key and the other as the public key. The private key is known only to you and it should be safely guarded.
SSH, or secure shell, is an encrypted protocol used to administer and communicate with servers. When working with an Ubuntu server, chances are you will spend most of your time in a terminal session connected to your server through SSH. In this guide, we'll focus on setting up SSH keys for a vanilla. Apr 5, 2019 - To generate an SSH key with PuTTYgen, follow these steps: Open the PuTTYgen program. For Type of key to generate, select SSH-2 RSA. Click the Generate button. Move your mouse in the area below the progress bar. Type a passphrase in the Key passphrase field. Click the Save private key button to save the private key.
By contrast, the public key can be shared freely with any SSH server to which you wish to connect.If an SSH server has your public key on file and sees you requesting a connection, it uses your public key to construct and send you a challenge. This challenge is an encrypted message and it must be met with the appropriate response before the server will grant you access. What makes this coded message particularly secure is that it can only be understood by the private key holder. While the public key can be used to encrypt the message, it cannot be used to decrypt that very same message. Only you, the holder of the private key, will be able to correctly understand the challenge and produce the proper response.This phase happens behind the scenes and is invisible to the user.
As long as you hold the private key, which is typically stored in the /.ssh/ directory, your SSH client should be able to reply with the appropriate response to the server.A private key is a guarded secret and as such it is advisable to store it on disk in an encrypted form. When the encrypted private key is required, a passphrase must first be entered in order to decrypt it. While this might superficially appear as though you are providing a login password to the SSH server, the passphrase is only used to decrypt the private key on the local system. Note: You can use the -a switch to specify the number of KDF rounds on the password encryption.You can also add an optional comment field to the public key with the -C switch, to more easily identify it in places such as /.ssh/knownhosts, /.ssh/authorizedkeys and ssh-add -L output. For example:$ ssh-keygen -C '$(whoami)@$(hostname)-$(date -I)'will add a comment saying which user created the key on which machine and when.Choosing the authentication key typeOpenSSH supports several signing algorithms (for authentication keys) which can be divided in two groups depending on the mathematical properties they exploit:. and, which rely on the of factoring the product of two large prime numbers,.
and, which rely on the elliptic curve problem. (ECC) algorithms are a to public key cryptosystems. One of their main advantages is their ability to provide, which makes for less computationally intensive operations ( i.e. Faster key creation, encryption and decryption) and reduced storage and transmission requirements.OpenSSH 7.0 due to discovered vulnerabilities, therefore the choice of lies within RSA or one of the two types of ECC.keys will give you the greatest portability, while will give you the best security but requires recent versions of client & server.
Is likely more compatible than Ed25519 (though still less than RSA), but suspicions exist about its security (see below). Note: These keys are used only to authenticate you; choosing stronger keys will not increase CPU load when transferring data over SSH.RSAssh-keygen defaults to RSA therefore there is no need to specify it with the -t option. It provides the best compatibility of all algorithms but requires the key size to be larger to provide sufficient security.Minimum key size is 1024 bits, default is 3072 (see ) and maximum is 16384.If you wish to generate a stronger RSA key pair ( e.g.
To guard against cutting-edge or unknown attacks and more sophisticated attackers), simply specify the -b option with a higher bit value than the default:$ ssh-keygen -b 4096Be aware though that there are diminishing returns in using longer keys. The GnuPG FAQ reads: ' If you need more security than RSA-2048 offers, the way to go would be to switch to elliptical curve cryptography — not to continue using RSA'.On the other hand, the latest iteration of the suggests a minimum 3072-bit modulus for RSA while ' preparing for the upcoming quantum resistant algorithm transition'.ECDSAThe Elliptic Curve Digital Signature Algorithm (ECDSA) was introduced as the preferred algorithm for authentication. Some vendors also disable the required implementations due to potential patent issues.There are two sorts of concerns with it:.
Political concerns, the trustworthiness of NIST-produced curves after revelations that the NSA willingly inserts backdoors into softwares, hardware components and published standards were made; well-known cryptographers about how the NIST curves were designed, and voluntary tainting has already in the past. Technical concerns, about the and the which reduce security in insufficiently precautious implementations.Both of those concerns are best summarized in.
Although the political concerns are still subject to debate, there is a that is technically superior and should therefore be preferred.Ed25519was introduced in of January 2014: ' Ed25519 is an elliptic curve signature scheme that offers better security than ECDSA and DSA and good performance'. Its main strengths are its speed, its constant-time run time (and resistance against side-channel attacks), and its lack of nebulous hard-coded constants. See also by a Mozilla developer on how it works.It is already implemented in and is the (which is different from key signature) in OpenSSH.Ed25519 key pairs can be generated with:$ ssh-keygen -t ed25519There is no need to set the key size, as all Ed25519 keys are 256 bits.Keep in mind that older SSH clients and servers may not support these keys.Choosing the key location and passphraseUpon issuing the ssh-keygen command, you will be prompted for the desired name and location of your private key.
By default, keys are stored in the /.ssh/ directory and named according to the type of encryption used. You are advised to accept the default name and location in order for later code examples in this article to work properly.When prompted for a passphrase, choose something that will be hard to guess if you have the security of your private key in mind. A longer, more random password will generally be stronger and harder to crack should it fall into the wrong hands.It is also possible to create your private key without a passphrase.
While this can be convenient, you need to be aware of the associated risks. Without a passphrase, your private key will be stored on disk in an unencrypted form. Anyone who gains access to your private key file will then be able to assume your identity on any SSH server to which you connect using key-based authentication. Furthermore, without a passphrase, you must also trust the root user, as he can bypass file permissions and will be able to access your unencrypted private key file at any time. Note: Previously, the private key password was encoded in an insecure way: only a single round of an MD5 hash. OpenSSH 6.5 and later support a new, more secure format to encode your private key.
This format is the default since. Ed25519 keys have always used the new encoding format. To upgrade to the new format, simply change the key's passphrase, as described in the next section. Changing the private key's passphrase without changing the keyIf the originally chosen SSH key passphrase is undesirable or must be changed, one can use the ssh-keygen command to change the passphrase without changing the actual key. Note: This method might fail if the remote server uses a non- sh shell such as tcsh as default and uses OpenSSH older than 6.6.1p1. See.If your key file is /.ssh/idrsa.pub you can simply enter the following command.$ ssh-copy-id remote-server.orgIf your username differs on remote machine, be sure to prepend the username followed by @ to the server name.$ ssh-copy-id [email protected] your public key filename is anything other than the default of /.ssh/idrsa.pub you will get an error stating /usr/bin/ssh-copy-id: ERROR: No identities found. In this case, you must explicitly provide the location of the public key.$ ssh-copy-id -i /.ssh/ided25519.pub [email protected] the ssh server is listening on a port other than default of 22, be sure to include it within the host argument.$ ssh-copy-id -i /.ssh/ided25519.pub -p 221 [email protected] methodBy default, for OpenSSH, the public key needs to be concatenated with /.ssh/authorizedkeys.
Begin by copying the public key to the remote server.$ scp /.ssh/idecdsa.pub [email protected]:The above example copies the public key ( idecdsa.pub) to your home directory on the remote server via scp. Do not forget to include the: at the end of the server address. Note: If you use GNOME, this environment variable is overridden by default. Ssh-agent as a wrapper programAn alternative way to start ssh-agent (with, say, each X session) is described in.
A basic use case is if you normally begin X with the startx command, you can instead prefix it with ssh-agent like so:$ ssh-agent startxAnd so you do not even need to think about it you can put an alias in your.bashaliases file or equivalent:alias startx='ssh-agent startx'Doing it this way avoids the problem of having extraneous ssh-agent instances floating around between login sessions. Exactly one instance will live and die with the entire X session.
Note: As an alternative to calling ssh-agent startx, you can add eval $(ssh-agent) to /.xinitrc.See for an idea on how to immediately add your key to the agent.GnuPG AgentThe has OpenSSH agent emulation. See for necessary configuration.Keychainis a program designed to help you easily manage your SSH keys with minimal user interaction. It is implemented as a shell script which drives both ssh-agent and ssh-add. A notable feature of Keychain is that it can maintain a single ssh-agent process across multiple login sessions. This means that you only need to enter your passphrase once each time your local machine is booted.Installationthe package.Configuration. Note: /.bashrc is used instead of the upstream suggested /.bashprofile because on Arch it is sourced by both login and non-login shells, making it suitable for textual and graphical environments alike.
See for more information on the difference between those.In the above example,. the -eval switch outputs lines to be evaluated by the opening eval command; this sets the necessary environments variables for SSH client to be able to find your agent.quiet will limit output to warnings, errors, and user prompts.Multiple keys can be specified on the command line, as shown in the example. By default keychain will look for key pairs in the /.ssh/ directory, but absolute path can be used for keys in non-standard location.
You may also use the -confhost option to inform keychain to look in /.ssh/config for IdentityFile settings defined for particular hosts, and use these paths to locate keys.See keychain -help or for details on setting keychain for other shells.To test Keychain, simply open a new terminal emulator or log out and back in your session. It should prompt you for the passphrase of the specified private key(s) (if applicable), either using the program set in $SSHASKPASS or on the terminal.Because Keychain reuses the same ssh-agent process on successive logins, you should not have to enter your passphrase the next time you log in or open a new terminal. You will only be prompted for your passphrase once each time the machine is rebooted.Tips. keychain expects public key files to exist in the same directory as their private counterparts, with a.pub extension.
If the private key is a symlink, the public key can be found alongside the symlink or in the same directory as the symlink target (this capability requires the readlink command to be available on the system). to disable the graphical prompt and always enter your passphrase on the terminal, use the -nogui option.
This allows to copy-paste long passphrases from a password manager for example. if you do not want to be immediately prompted for unlocking the keys but rather wait until they are needed, use the -noask option. Note: Keychain is able to manage keys in the same fashion. By default it attempts to start ssh-agent only, but you can modify this behavior using the -agents option, e.g.agents ssh,gpg. X11-ssh-askpassThe package provides a graphical dialog for entering your passhrase when running an X session. X11-ssh-askpass depends only on the and libraries, and the appearance of x11-ssh-askpass is customizable. While it can be invoked by the ssh-add program, which will then load your decrypted keys into, the following instructions will, instead, configure x11-ssh-askpass to be invoked by the aforementioned script.Install the and packages.Edit your /.xinitrc file to include the following lines, replacing the name and location of your private key if necessary.
Be sure to place these commands before the line which invokes your window manager./.xinitrc keychain /.ssh/idecdsa -f /.keychain/$HOSTNAME-sh &&. /.keychain/$HOSTNAME-sh 2/dev/null -f /.keychain/$HOSTNAME-sh-gpg &&.
/.keychain/$HOSTNAME-sh-gpg 2/dev/null.exec openbox-sessionIn the above example, the first line invokes keychain and passes the name and location of your private key. If this is not the first time keychain was invoked, the following two lines load the contents of $HOSTNAME-sh and $HOSTNAME-sh-gpg, if they exist.
These files store the environment variables of the previous instance of keychain.Calling x11-ssh-askpass with ssh-addThe ssh-add manual page specifies that, in addition to needing the DISPLAY variable defined, you also need SSHASKPASS set to the name of your askpass program (in this case x11-ssh-askpass). It bears keeping in mind that the default Arch Linux installation places the x11-ssh-askpass binary in /usr/lib/ssh/, which will not be in most people's PATH. This is a little annoying, not only when declaring the SSHASKPASS variable, but also when theming. You have to specify the full path everywhere. Both inconveniences can be solved simultaneously by symlinking:$ ln -sv /usr/lib/ssh/x11-ssh-askpass /bin/ssh-askpassThis is assuming that /bin is in your PATH. So now in your.xinitrc, before calling your window manager, one just needs to export the SSHASKPASS environment variable:$ export SSHASKPASS=ssh-askpassand your will contain something like:ssh-askpass.background: #000000Doing it this way works well with.
You start X with ssh-agent startx and then add ssh-add to your window manager's list of start-up programs.ThemingThe appearance of the x11-ssh-askpass dialog can be customized by setting its associated. Some examples are the.ad files at. See 2019-05-05 for full details.Alternative passphrase dialogsThere are other passphrase dialog programs which can be used instead of x11-ssh-askpass. The following list provides some alternative solutions. uses the. uses the library.pamsshThe project exists to provide a (PAM) for SSH private keys. This module can provide single sign-on behavior for your SSH connections.
On login, your SSH private key passphrase can be entered in place of, or in addition to, your traditional system password. Once you have been authenticated, the pamssh module spawns ssh-agent to store your decrypted private key for the duration of the session.To enable single sign-on behavior at the tty login prompt, install the unofficial AUR package. Note: pamssh 2.0 now requires that all private keys used in the authentication process be located under /.ssh/login-keys.d/.Create a symlink to your private key file and place it in /.ssh/login-keys.d/.
Replace the idrsa in the example below with the name of your own private key file.$ mkdir /.ssh/login-keys.d/$ cd /.ssh/login-keys.d/$ ln -s./idrsaEdit the /etc/pam.d/login configuration file to include the text highlighted in bold in the example below. The order in which these lines appear is significiant and can affect login behavior. Warning: Misconfiguring PAM can leave the system in a state where all users become locked out. Before making any changes, you should have an understanding of how PAM configuration works as well as a backup means of accessing the PAM configuration files, such as an Arch Live CD, in case you become locked out and need to revert any changes. An IBM developerWorks is available which explains PAM configuration in further detail. /etc/pam.d/login #%PAM-1.0auth required pamsecuretty.soauth requisite pamnologin.soauth include system-local-loginauth optional pamssh.so tryfirstpassaccount include system-local-loginsession include system-local-loginsession optional pamssh.soIn the above example, login authentication initially proceeds as it normally would, with the user being prompted to enter his user password.
The additional auth authentication rule added to the end of the authentication stack then instructs the pamssh module to try to decrypt any private keys found in the /.ssh/login-keys.d directory. The tryfirstpass option is passed to the pamssh module, instructing it to first try to decrypt any SSH private keys using the previously entered user password. If the user's private key passphrase and user password are the same, this should succeed and the user will not be prompted to enter the same password twice.
In the case where the user's private key passphrase user password differ, the pamssh module will prompt the user to enter the SSH passphrase after the user password has been entered. The optional control value ensures that users without an SSH private key are still able to log in. In this way, the use of pamssh will be transparent to users without an SSH private key.If you use another means of logging in, such as an X11 display manager like or and you would like it to provide similar functionality, you must edit its associated PAM configuration file in a similar fashion.