This tutorial will guide you through generating SSH keys on Linux, saving them to a custom folder, and using them to securely connect to a Linux server with a specified port.
Before you begin, ensure the following:
Linux Server with SSH Access: You should have access to a Linux server via SSH. This allows you to remotely manage and configure the server.
User Account with `sudo` Privileges: You'll need a user account on the system with `sudo` privileges to install packages and perform administrative tasks.
SSH Client Installed: Verify that the `SSH client` (ssh) is installed on your Linux system. You can check its version by running `ssh -V` in your terminal. If it's not installed, you can typically install it using your package manager (e.g., `sudo apt install openssh-client` for Debian/Ubuntu systems).
If you need help with these prerequisites, refer to these tutorials:
Now let's proceed with the steps:
Open your terminal application. You can typically find this in your applications menu or by pressing `Ctrl + Alt + T`.
Use the `ssh-keygen` command to generate an SSH key pair. Replace `your_email@example.com` with your email address and adjust the file path as needed.
Linux (Debian/Ubuntu)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f "/home/YourUsername/Desktop/CustomFolder/my_ssh_key"
Replace `/home/YourUsername/Desktop/CustomFolder/` with your desired path.
You can set a passphrase for added security when prompted. This step is optional but recommended.
The generated keys will be saved in the specified folder:
`/home/YourUsername/Desktop/CustomFolder/my_ssh_key` (private key)
`/home/YourUsername/Desktop/CustomFolder/my_ssh_key.pub` (public key)
Copy the Public Key Content. Use a command like `cat` to display the public key content so you can copy it to your clipboard manually or through a terminal tool.
Linux (Debian/Ubuntu)
cat /home/YourUsername/Desktop/CustomFolder/my_ssh_key.pub
Connect to your Linux system using SSH. Replace `username`, `server_ip_address`, and `port_number` with your server's details.
Linux (Debian/Ubuntu)
ssh -p port_number username@server_ip_address
Create `.ssh` Directory (if it doesn't exist) on the Linux Server
Ensure that the `.ssh` directory exists on your Linux server. Run the following command:
Linux (Debian/Ubuntu)
mkdir -p ~/.ssh
Use a text editor like `nano` or `vim` to open or create the `authorized_keys` file:
Linux (Debian/Ubuntu)
nano ~/.ssh/authorized_keys
Paste the public key you copied earlier into the `authorized_keys` file. To paste in `nano`, `right-click` or use `Shift + Insert`.
Save and exit the editor (`nano` in this case). Press `Ctrl + X`, then `Y` to confirm changes, and `Enter` to exit.
Ensure the correct permissions are set for the `.ssh` directory and `authorized_keys` file:
Linux (Debian/Ubuntu)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Apply the changes by restarting the SSH service on your Linux server:
Linux (Debian/Ubuntu)
sudo systemctl restart sshd
Use the `-i` option with the `ssh` command to specify the private key when connecting to the Linux server.
Linux (Debian/Ubuntu)
ssh -i "/home/YourUsername/Desktop/CustomFolder/my_ssh_key" -p port_number username@server_ip_address
Replace `username`, `server_ip_address`, and `port_number` with your server details.
By following these steps and ensuring you meet the prerequisites, you have successfully generated SSH keys on Linux, copied the public key to your Linux server, and configured your SSH client to use the private key for secure connections, specifying the port number each time.
Published: June 23, 2024
Have a question or suggestion? Want to request a tutorial or simply leave me a message? I'd love to hear from you! Join our community on Discord for exclusive content, engaging discussions, and more. Thank you! 🌟