SSH key authentication is a convenient and secure way to log in to a Linux system. In this guide, you'll learn how to create a user, generate SSH keys, and configure a Linux server for passwordless SSH access.
Create a New User
If you haven't already created the user that will access the server, run:
Replace newuser with your preferred username.
Switch to the New User
Log in as the newly created user:
This ensures all SSH files are created in the correct home directory.
Create the SSH Directory
Create the .ssh directory inside the user's home folder:
Set the Correct Permissions
SSH is very particular about file permissions. Incorrect permissions can prevent key authentication from working.
The required permissions are:
.sshdirectory:700authorized_keysfile:600
Run the following commands:
Generate an SSH Key Pair
On your local machine (not on the Linux server), generate a new SSH key pair:
This creates:
- Private key:
newuser_key - Public key:
newuser_key.pub
Important: Never share your private key.
Copy the Public Key to the Server
Transfer the public key to the server:
Append it to the authorized_keys file:
This authorizes the key for future logins.
After copying the key, verify the permissions and ownership:
Connect Using the SSH Key
You can now connect to the server using your private key:
Replace your_server_ip with your server's IP address or hostname.
If everything is configured correctly, you'll be logged in without being prompted for a password.
#linux
0 Comments