I recently read that SSH keys provide a secure way of logging into a Linux and Unix-based server. How do I set up SSH keys on a Linux or Unix-based system? In SSH for Linux/Unix, how do I set up public-key authentication?
This page explains a public key and shows you how to set up SSH keys on a Linux or Unix-like server. 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, macOS/OSX, AIX, HP-UX and co).
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux terminal |
Category | Terminal/ssh |
Prerequisites | OpenSSH client and server |
OS compatibility | *BSD • Linux • Unix • WSL • macOS |
Est. reading time | 8 minutes |
What is 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 a home-based server.
How to set up SSH keys
Steps to setup secure ssh keys:
- Create the ssh key pair using the ssh-keygen command.
- Copy and install the public ssh key using the ssh-copy-id command on a Linux or Unix server.
- Add yourself to sudo or wheel group admin account.
- Disable the password login for the root account.
- Test your password fewer ssh keys login using the ssh user1@server-name command.
Let us see all steps in detail.
How do I set up public-key authentication?
You must generate both a public and a private key pair. For example:
- server1.zpenterprises.co – You store your public key on the remote hosts, and you have an account on this Linux/Unix-based server.
- client1.zpenterprises.co – Your private key stays on the desktop/laptop/ computer (or local server) you use to connect to server1.zpenterprises.co. Do not share or give your private file to anyone.
In the public key-based method, you can log into remote hosts and servers and transfer files to them without using your account passwords. Feel free to replace server1.zpenterprises.co and client1.zpenterprises.co names with your actual setup. Enough talk; let’s set up public-key authentication. Open the Terminal and type the following commands if the .ssh directory does not exist:
mkdir -p $HOME/.ssh chmod 0700 $HOME/.ssh
1: Create the key pair
The computer (such as client1.zpenterprises.co) generates a key pair for the protocol. In this step, the client uses a particular utility, ssh-keygen, to generate an SSH key pair for authentication with the server. This utility is bundled with OpenSSH, and by default, it creates a 2048-bit RSA key pair. It supports RSA and DSA, both with different lengths of keys. A key length of 4096 bits is recommended for establishing a secure connection between two machines. The following diagram shows how to create an RSA key pair of 2048-bit:
ssh-keygen -t rsa
Sample outputs:
You need to set the Key Pair location and name. I recommend you use the default location if you do not yet have another key there, for example, $HOME/.ssh/id_rsa. You will be prompted to supply a passphrase (password) for your private key. I suggest that you set up a passphrase when prompted. You should see two new files in $HOME/.ssh/ directory:
- $HOME/.ssh/id_rsa– contains your private key.
- $HOME/.ssh/id_rsa.pub – contain your public key.
The optional syntax for advanced users
The following syntax specifies the 4096 bits in the RSA key to creation (default 2048):
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/vps-cloud.web-server.key -C "My web-server key"
Where,
- -t rsa : Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa”, “ed25519”, or “rsa” for protocol version 2.
- -b 4096 : Specifies the number of bits in the key to create
- -f ~/.ssh/vps-cloud.web-server.key : Specifies the filename of the key file.
- -C "My web-server key" : Set a new comment.
2: Install the public key in the 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., [email protected]). To do so, enter the following command on your client1.zoenterprises.co:
ssh-copy-id -i $HOME/.ssh/id_rsa.pub user1@server1.zpenterprises.co
OR copy the public key in the remote server as authorized_keys in ~/.ssh/ directory:
scp $HOME/.ssh/id_rsa.pub user1@server1.zpenterprises.co:~/.ssh/authorized_keys
A note about appending the public key in the remote server
On some systems ssh-copy-id command may not be installed, so use the following commands (when prompted, provide the password for a remote user account called user1) to install and append the public key:
## First create .ssh directory on server ## ssh user1@server1.zpenterprises.co "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 user1@server1.zpenterprises.co "cat >> .ssh/authorized_keys"
I do this often with keys set up from my office desktop to add to the story.
for i in server1 server2 server3 server4; do ssh mylogin@$i "hostname" ; done
This one line will log in to the four servers and run the command hostname.
3: Test it (type command on client1.zpenterprises.co)
The syntax is as follows for the ssh command:
ssh user@server1.cyberciti.biz ssh user@your-server-ip-address ssh -i ~/.ssh/your-key user@your-server-ip-address
Or copy a text file called foo.txt:
scp foo.txt user@server1.cyberciti.biz:/tmp/
You will be prompted for a passphrase. Try ssh-agent and ssh-add commands to get rid of the passphrase whenever you log in to the remote host.
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)
Type the ssh-add command to prompt the user for a private key passphrase and adds it to the list maintained by the ssh-agent command:
ssh-add
Enter your private key passphrase. Now try again to log into [email protected], and you will not be prompted for a password:
ssh user1@server1.zpenerprises.co
One can list public key parameters of all identities with the -L option:
ssh-add -L
Deleting all private keys from the ssh-agent can be done with the -D option as follows:
ssh-add -D
When you log out and kill the ssh-agent, run:
kill $SSH_AGENT_PID
You can also add something like the below to your shell startup to kill ssh-agent at logout:
trap "kill $SSH_AGENT_PID" 0
4: Disable the password-based login on a server
Login to your server, type:
## client commands ## eval $(ssh-agent) ssh-add ssh [email protected]
Edit /etc/ssh/sshd_config on server1.zpenterprises.co using a text editor such as nano or vim:
KEY TAKEAWAYS
- A credit score is a number that depicts a consumer’s creditworthiness. FICO scores range from 300 to 850.
- Factors used to calculate your credit score include repayment history, types of loans, length of credit history, debt utilization, and whether you’ve applied for new accounts.
- A credit score plays a key role in a lender’s decision to offer credit and for what terms.
- The three main U.S. credit bureaus (Equifax, Experian, and TransUnion) may each calculate your FICO score differently.
$ sudo vim /etc/ssh/sshd_config
OR directly jump to the PermitRootLogin line using a vim text editor:
$ sudo vim +/PermitRootLogin /etc/ssh/sshd_config
Find PermitRootLogin and set it as follows:
PermitRootLogin no
Save and close the file. I am going to add a user named user1vivek to sudoers on Ubuntu Linux:
# adduser
user1
Finally, reload/restart the sshd server, type the command as per your Linux/Unix version:
## CentOS/RHEL/Fedora (older version) Linux server reload sshd ## sudo service sshd reload ## CentOS/RHEL/Fedora (latest version i.e. systemd based) Linux server reload sshd ## sudo systemctl reload sshd ## Debian/Ubuntu Linux (older version) server reload sshd ## sudo /etc/init.d/ssh reload ## Debian/Ubuntu Linux (systemd based latest) server reload sshd ## sudo systemctl reload ssh ## Generic Unix method to reload sshd ## sudo kill -HUP `cat /var/run/sshd.pid` OR sudo kill -HUP $(cat /var/run/sshd.pid)
5: How to add or replace a passphrase for an existing private key?
How-To to change your passphrase, type the following command:
ssh-keygen -p
6: How do I backup an existing private/public key?
## 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/
How do I protect my ssh keys?
- Always use a strong passphrase.
- Please do not share your private keys online or store them in insecure cloud storage.
- Restrict privileges of the account.
Conclusion
This page explained how to set up ssh keys for authentication purposes. Read the following man pages using the man command:
man ssh
man ssh-agent
man ssh-keygen
- keychain: Set Up Secure Passwordless SSH Access For Backup Scripts
- Ubuntu / Debian Linux Server Install Keychain SSH Key Manager For OpenSSH
- OpenSSH project homepage here.
And, there you have it, ssh set up with public key-based authentication for Linux or Unix-like systems.