Setting Up SSH Access to an Ubuntu Server from macOS and Windows Using a 4096-Bit Key
SSH (Secure Shell) allows secure remote access to an Ubuntu server. Follow these instructions to create a 4096-bit key, copy it to the server, and enable passwordless login.
1. Generate a 4096-Bit SSH Key
macOS
- Open the Terminal.
- Generate an SSH key pair:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"-t rsa: Specifies the RSA algorithm.-b 4096: Specifies the key length as 4096 bits.-C "your_email@example.com": Adds a label to your key.
- When prompted:
- Save the key in the default location (
~/.ssh/id_rsa) by pressing Enter. - Optionally, set a passphrase (recommended for added security).
- Save the key in the default location (
Windows (Using Command Prompt, PowerShell, or Windows Subsystem for Linux (WSL))
- Open Command Prompt, PowerShell, or WSL.
- Generate an SSH key pair:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" - Follow the same prompts as macOS.
2. Copy the Public Key to the Ubuntu Server
Step 2.1: Use ssh-copy-id (Preferred Method)
- Copy the public key to the Ubuntu server:
ssh-copy-id -i ~/.ssh/id_rsa.pub username@your_server_ipReplace:usernamewith your Ubuntu server username.your_server_ipwith your server’s IP address.
- Enter your server password when prompted.
Step 2.2: Manual Method (If ssh-copy-id Is Not Available)
- Display your public key:
cat ~/.ssh/id_rsa.pub - Copy the key (select and copy the output).
- Log in to the Ubuntu server with your username and password:
ssh username@your_server_ip - On the server, create the
~/.sshdirectory (if it doesn’t exist):mkdir -p ~/.ssh chmod 700 ~/.ssh - Add your public key to the
authorized_keysfile:echo "your-public-key" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keysReplaceyour-public-keywith the content of yourid_rsa.pub.
3. Test Passwordless SSH Login
- On your local machine, log in to the server:
ssh username@your_server_ip - If setup was successful, you won’t be prompted for a password.
4. Enhance SSH Security
Step 4.1: Disable Password Authentication (Optional but Recommended)
- Edit the SSH configuration file on the Ubuntu server:
sudo nano /etc/ssh/sshd_config - Update the following settings:
PasswordAuthentication no PermitRootLogin no - Restart the SSH service:
sudo systemctl restart ssh
Step 4.2: Test Again
Open a new terminal and try logging in to ensure passwordless access works before closing existing sessions.
5. Troubleshooting
- Ensure the permissions of your
~/.sshdirectory and files are correct:- On the Ubuntu server:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys - On your local machine:
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa
- On the Ubuntu server:
- Verify the server’s SSH service is running:
sudo systemctl status ssh
This setup ensures secure, passwordless SSH access to your Ubuntu server using a 4096-bit RSA key. Always keep your private key secure and consider using a passphrase for added protection.
