Enable SSH public key authentication - (2024)

Last updated on: 2020-10-22

Authored by: James Andrade

This article describes the procedure to set up a Secure Shell (SSH) public key authentication.

  1. Log in to the server.

  2. Verify that the user exists:

     `getent passwd <username>`
  3. Look up the value assigned to the AuthorizedKeysFile parameter within/etc/ssh/sshd_config to determine the file where the key is stored:

     `grep AuthorizedKeysFile /etc/ssh/sshd_config`

    Note: The default location is ~/.ssh/authorized_keys within theuser’s default home directory.

  4. Switch to the user’s home directory:

     `cd /directory-path`

    Note: Substitute directory-path with user’s home directory path.

  5. Check permission levels for the .ssh/ directory. It should have 0700permissions and be owned by the user.

     `ls .ssh`

    a. If the directory does not exist, create it and set the permissions to 0700:

     `mkdir -m 700 .ssh`

    b. If the directory exists, you can set ownership separately:

     `chmod 700 .ssh/` `chown -R username:username /path/to/home/.ssh`
  6. Switch to .ssh/ directory and authorized_keys file:

     ```cd .ssh/ vim authorized_keys ll```
  7. Add the SSH Public Key to the end of the authorized_keys file:

     `vim authorized_keys`
  8. Change permissions to 600 and ensure proper ownership of the file:

     `chmod 600 authorized_keys` `chown -R username:username authorized_keys`

Disable password authentication

If you want all users to log in with public keys and not passwords, you can disable password authentication.

Important: Disabling password authentication locks users who used a passwordto access the server if SSH authentication is not configured for their account.

  1. Create a backup of the sshd_config file before makingany changes:

    mkdir /home/username/backupcp /etc/ssh/sshd_config /home/username/backup/sshd_config.bak
  2. Open the sshd_config file:

     `vim /etc/ssh/sshd_config`
  3. Find the PubkeyAuthentication parameter and set it to yes. If the line is commented, remove anycomment indicators (#).

  4. Find fthe PasswordAuthentication parameter within the same file and set it to no.

  5. Save the changes to the file and exit the file.

  6. Check the syntax by using sshd -t. If there are no errors, reload sshd:

     `service sshd reload`

Additional notes:

  1. The private key file on your local workstation (client-side) should have permissions set to600, and the .ssh directory should have the permissions set to 700. Theauthorized_keys files also work with 644 permissions, but 600 ismore secure.

©2020 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

Enable SSH public key authentication - (1)

See license specifics and DISCLAIMER

Enable SSH public key authentication - (2024)

FAQs

How to enable public key SSH? ›

Enable SSH public key authentication
  1. Log in to the server.
  2. Verify that the user exists: `getent passwd <username>`
  3. Look up the value assigned to the AuthorizedKeysFile parameter within /etc/ssh/sshd_config to determine the file where the key is stored: `grep AuthorizedKeysFile /etc/ssh/sshd_config`
Oct 22, 2020

How to enable or disable public key authentication in SSH? ›

  1. Log into SSH.
  2. Edit the file with your favorite editor: /etc/ssh/sshd_config.
  3. Lookup the variable: PasswordAuthentication and change 'no' to 'yes'
  4. Save and close the file.
  5. Run this command: service sshd reload.

How do I enable SSH public key authentication in Windows? ›

To enable the Public Key Authentication you must edit the sshd_config file which located in C:\Programdata\ssh and change from no to yes the line with the PubKey Authentication. Don't forget to disable the Password authentication while changing the value from Yes to No.

Does SSH use public key authentication? ›

An SSH key relies upon the use of two related keys, a public key and a private key, that together create a key pair that is used as the secure access credential. The private key is secret, known only to the user, and should be encrypted and stored safely.

How to enable SSH using command prompt? ›

You can start an SSH session in your command prompt by executing ssh user@machine and you will be prompted to enter your password. You can create a Windows Terminal profile that does this on startup by adding the commandline setting to a profile in your settings.json file inside the list of profile objects.

How to add public key to SSH command? ›

How to Add SSH Public Key to Server
  1. Method 1: Automatically copy the ssh key to server.
  2. Method 2: Manually copy the public ssh key to the server. Step 1: Get the public key. Step 2: Create ssh directory in the user's home directory (as a sysadmin) Step 3: Set appropriate permission to the file.

What is the public key permission for SSH? ›

ssh directory permissions should be 700 (drwx------). The public key (. pub file) should be 644 (-rw-r--r--). The private key (id_rsa) on the client host, and the authorized_keys file on the server, should be 600 (-rw-------).

How do I allow SSH access? ›

Answer
  1. Connect to the server via SSH.
  2. Escalate privileges to 'root': ...
  3. Set password for 'root' user: ...
  4. Make sure that the configuration file /etc/ssh/sshd_config has parameters PermitRootLogin and PasswordAuthentication are set to yes and not prepended by # symbol.

How to generate SSH key for authentication? ›

For Windows 10 & 11
  1. Press the Windows key or open up the Start Menu. Type “cmd”.
  2. Under “Best Match”, click “Command Prompt”.
  3. In the command prompt, use the ssh-keygen command: ...
  4. The system will now generate the key pair and display the key fingerprint and a randomart image. ...
  5. Open your file explorer.

How do I find my SSH public key on Windows? ›

Command Prompt (for Windows 10 only)
  1. Open Command Prompt or Windows PowerShell.
  2. Issue the command: ssh-keygen.
  3. To view public key, navigate to C:\Users\<username>/.ssh/id_rsa.pub (see image below) or execute this command in the command prompt: more C:\Users\<username>/.ssh/id_rsa.pub.
Sep 13, 2022

Where is my SSH public key? ›

By default, the private key is stored in ~/. ssh/id_rsa and the public key is stored in ~/. ssh/id_rsa.

How do I authenticate with a public key? ›

In public key authentication, the scheme is based on public key cryptography, using cryptosystems where encryption and decryption are done using separate keys, and it is not feasible to derive the decryption key from the encryption key. Each user creates a public/private key pair for authentication purposes.

How to configure SSH in Windows? ›

To configure SSH on Windows:
  1. Install or enable SSH server on your Windows machine. ...
  2. To enable the built-in SSH server for older Windows versions, see Install Win32 OpenSSH.
  3. Configure and start your SSH server.
  4. If you have firewall, open the SSH port in your firewall.

How do I connect to SSH on Windows? ›

Connect
  1. Open a Command prompt window on your technician PC.
  2. Connect to the device: To connect using a username and password: Windows Command Prompt Copy. ssh user@192. 168. ...
  3. Enter the password for your user if you're connecting with a username and password, or if you configured your key to require a password.
Jun 24, 2021

Where is SSH config on Windows 10? ›

A user's configuration file at %userprofile%\. ssh\config. The system-wide configuration file at %programdata%\ssh\ssh_config.

How to access SSH with public key from PuTTY? ›

Use the main PuTTY application to configure the PuTTY client to use key-based authentication.
  1. Launch PuTTY but do not connect to a remote system.
  2. In the Category window, browse to Connection>Data.
  3. Set the Auto-login username to the remote SSH username. ...
  4. Browse to Connection>SSH>Auth.

How does SSH key authentication work? ›

An SSH key is used to access a remote server through an SSH connection. The keys come in pairs, a public key and a private key. The public key is kept within the server and the private key is with the user or the client. The server authenticates the user by sending a message encrypted using the public key.

Why is SSH key not working? ›

Make sure the authorized_keys file and the private key itself have the correct permissions and ownership. Check that key-based authentication is allowed by the server. Make sure the private key is readable by the SSH client. If you're using PuTTY, make sure your SSH keys are properly configured for the session.

How do I check permissions on SSH key? ›

Correct SSH Directory Permissions
  1. chmod go-w ~/
  2. chmod 700 ~/. ssh.
  3. chmod 600 ~/. ssh/authorized_keys.
Jan 13, 2022

How to generate SSH public key in Linux? ›

Generate an SSH Key Pair
  1. Run the ssh-keygen command. You can use the -t option to specify the type of key to create. ...
  2. The command prompts you to enter the path to the file in which you want to save the key. ...
  3. The command prompts you to enter a passphrase. ...
  4. When prompted, enter the passphrase again to confirm it.

What is the difference between public key and private key in SSH? ›

Private key stays with the user (and only there), while the public key is sent to the server. Typically with the ssh-copy-id utility. Server stores the public key (and "marks" it as authorized). Server will now allow access to anyone who can prove they have the corresponding private key.

What is SSH authentication? ›

SSH is used to establish a secure connection between two remote systems over the internet. x. 509 certificates require the key pairs used in the asymmetric encryption process to be affixed with a digital certificate, which has the digital signature of a trusted issuing body (Certificate Authorities, or CAs).

How do I add a SSH key to Windows? ›

Set up personal SSH keys on Windows
  1. Install OpenSSH on your device.
  2. Start the SSH service.
  3. Create an SSH key pair.
  4. Add your key to the SSH agent.
  5. Provide Bitbucket Cloud with your public key.
  6. Check that your SSH authentication works.

How do I ensure my public key is valid? ›

The verification is done by signing a the certificate using a private key (from this CA), then both peers in the connection will check that the certificate was signed by a trusted CA and validate the connection.

How do I register a public key in Windows? ›

Setup server-side (sshd)
  1. Append contents of id_rsa. pub (client's public key) to the following file in corresponding user's directory (create one if needed). ...
  2. Double check access permissions on authorized_keys (only System, Administrators and owner can have access). icacls %systemdrive%\Users\.ssh\authorized_keys.
Aug 16, 2019

How to login via SSH key? ›

Step 1 Create and Copy Private Key to remote VM
  1. Copy private key to new file called centos7template01.txt.
  2. Type ls to verify file is there.
  3. Copy file to remote VM.
  4. Type yes to connect and transfer file.
  5. SSH into remote VM (Cent7-07)
  6. Type ls to confirm file copied successfully.

Why is my SSH key not public? ›

Here are some steps you can take to troubleshoot this issue: Make sure the authorized_keys file and the private key itself have the correct permissions and ownership. Check that key-based authentication is allowed by the server. Make sure the private key is readable by the SSH client.

Where is my public key for SSH? ›

Open TerminalTerminalGit Bash. Enter ls -al ~/.ssh to see if existing SSH keys are present. Check the directory listing to see if you already have a public SSH key.

Where do I get my public SSH key? ›

Command Prompt (for Windows 10 only)
  • Open Command Prompt or Windows PowerShell.
  • Issue the command: ssh-keygen.
  • To view public key, navigate to C:\Users\<username>/.ssh/id_rsa.pub (see image below) or execute this command in the command prompt: more C:\Users\<username>/.ssh/id_rsa.pub.
Sep 13, 2022

How do I enable SSH for a user? ›

Granting SSH access to a user
  1. Locate the following OpenSSH file: /etc/ssh/sshd_config.
  2. Make a backup of this file so you can revert if necessary. cp /etc/ssh/sshd_config{,.' date +%s'}
  3. Edit the file by using the OpenSSH keywords.

How to access ssh with public key from PuTTY? ›

Use the main PuTTY application to configure the PuTTY client to use key-based authentication.
  1. Launch PuTTY but do not connect to a remote system.
  2. In the Category window, browse to Connection>Data.
  3. Set the Auto-login username to the remote SSH username. ...
  4. Browse to Connection>SSH>Auth.

How to login using SSH key? ›

Step 1 Create and Copy Private Key to remote VM
  1. Copy private key to new file called centos7template01.txt.
  2. Type ls to verify file is there.
  3. Copy file to remote VM.
  4. Type yes to connect and transfer file.
  5. SSH into remote VM (Cent7-07)
  6. Type ls to confirm file copied successfully.

How do I get access to ssh? ›

How to connect via SSH:
  1. Open the list of your servers and click on the one you need. Click the Instructions button. ...
  2. Open a terminal (for Linux) or a command line (for Windows) on your computer. Enter the command: ssh [username]@[server IP] ...
  3. The connection will prompt you for a password.

What is ssh authentication? ›

SSH is used to establish a secure connection between two remote systems over the internet. x. 509 certificates require the key pairs used in the asymmetric encryption process to be affixed with a digital certificate, which has the digital signature of a trusted issuing body (Certificate Authorities, or CAs).

How do I set permissions for SSH? ›

SSH Into a Unix Computer
  1. You can do this from a Windows computer by following these steps: ...
  2. Change to the appropriate directory if you need to. ...
  3. To change the permissions of all of the files within that directory, type “chmod -R 755 directoryname” . ...
  4. Type “exit” to log out of the SSH session when you are done.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5796

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.