This approach is helpful if you need to manage multiple SSH keys for different servers or projects. It allows you to store SSH keys in custom subfolders, providing a structured way to organize and manage your keys across environments.
You can generate an SSH key in a subfolder within .ssh/ to keep things organized and easily manage multiple keys for different projects or servers.
To generate an SSH key in a custom subfolder inside the .ssh/ directory, follow these steps:
.ssh/:
Create the Subfolder (if it doesn’t already exist):
mkdir-p~/.ssh/my_custom_folder
Replace my_custom_folder with the desired folder name.
Generate the Key in the Custom Subfolder: Run the ssh-keygen command and specify the full path for the key file:
Use the following command to copy your public key to the server:
ssh-copy-id-i.ssh/id_rsa.pubusername@server_ip
Replace username with your server username.
Replace server_ip with the IP address or domain of the server.
It will prompt you for your server password. Once authenticated, the public key will be added to the server under ~/.ssh/authorized_keys for that user.
To make your SSH login process easier, you can configure the SSH client to remember and automatically use your custom SSH key(s) for specific servers by modifying the ~/.ssh/config file. Here’s how you can do it:
To set “PasswordAuthentication” to “no” on the server, you need to modify the SSH server configuration file (sshd_config). This ensures that only key-based authentication is allowed, further enhancing server security. Here’s how you can do it:
3.5.1. Disable Password Authentication on the Server
Find and Update the PasswordAuthentication Setting:
Search for the PasswordAuthentication line in the file. If it doesn’t exist, add it. Set its value to no:
PasswordAuthentication no
(Optional) Also, ensure that PubkeyAuthentication is set to yes to allow key-based authentication:
PubkeyAuthentication yes
Public key authentication (SSH key authentication) is enabled by default. This means you don’t need to explicitly add or modify the line PubkeyAuthentication yes unless your configuration has been changed or you want to explicitly ensure that it’s set.
Save and Exit:
Restart the SSH Service:
Restart the SSH server to apply the changes:
sudosystemctlrestartssh
Test the Configuration:
Ensure that you can log in to the server using your key before closing your current SSH session: