Setting Up SSH Keys with Namecheap Hosting on macOS
This guide documents the complete process of setting up SSH key authentication for Namecheap shared hosting on macOS, including solutions for timeout issues and directory-specific configurations.
Prerequisites
- macOS system
- Namecheap shared hosting account
- Terminal access
- Server details (hostname, username, SSH port)
Step 1: Check Existing SSH Keys
First, check if you already have SSH keys:
bashCopyls -la ~/.ssh/
Expected output might show:
Copydrwx------ 8 username staff 256 Jan 18 18:20 .
drwxr-x---+ 95 username staff 3040 Feb 5 20:08 ..
-rw------- 1 username staff 2610 Apr 5 2024 id_rsa
-rw-r--r-- 1 username staff 578 Apr 5 2024 id_rsa.pub
-rw------- 1 username staff 4952 Jan 18 18:05 known_hosts
Step 2: View Your Public Key
If you have existing keys, view your public key:
bashCopycat ~/.ssh/id_rsa.pub
The output should look like:
Copyssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCpcyPIlMsM1... username@host.local
Step 3: Configure SSH Config File
Create or edit your SSH config file with optimized settings for both general access and specific directories:
bashCopynano ~/.ssh/config
Add these configurations:
Copy# General server access
Host namecheap
HostName 198.54.125.196
User mnrdjmke
Port 21098
ServerAliveInterval 15
ServerAliveCountMax 240
TCPKeepAlive yes
IPQoS throughput
# Directory-specific access
Host clayspace
HostName 198.54.125.196
User mnrdjmke
Port 21098
ServerAliveInterval 15
ServerAliveCountMax 240
RemoteCommand cd clayspace.mnrdsgn.com; bash --login
RequestTTY yes
TCPKeepAlive yes
IPQoS throughput
This configuration provides:
- Anti-timeout measures (ServerAliveInterval, ServerAliveCountMax)
- TCP keep-alive for better connection stability
- Automatic directory changing for specific projects
- Optimized throughput settings
Step 4: Add Key to SSH Agent
Load your key into the SSH agent:
bashCopyssh-add ~/.ssh/id_rsa
You should see:
CopyIdentity added: /Users/username/.ssh/id_rsa (username@host.local)
Step 5: Copy Key to Server
Use ssh-copy-id to upload your key:
bashCopyssh-copy-id -i ~/.ssh/id_rsa.pub -p 21098 mnrdjmke@198.54.125.196
Step 6: Verify Setup
Test both configurations:
bashCopy# Test general server access
ssh namecheap
# Test directory-specific access
ssh clayspace
Dealing with Timeouts
If you experience timeouts, you can create a reconnection script:
- Create the script:
bashCopynano ~/reconnect-clayspace.sh
- Add this content:
bashCopy#!/bin/bash
while true; do
ssh clayspace
echo "Connection lost. Reconnecting in 5 seconds..."
sleep 5
done
- Make it executable:
bashCopychmod +x ~/reconnect-clayspace.sh
- Run when needed:
bashCopy~/reconnect-clayspace.sh
Troubleshooting
Issue 1: Connection Timeout
Solutions tried:
- Reduced ServerAliveInterval to 15 seconds
- Increased ServerAliveCountMax to 240
- Added TCPKeepAlive yes
- Added IPQoS throughput
- Created reconnection script
Issue 2: Permission Denied
Check:
- Verify key is added to ssh-agent (
ssh-add -l
) - Check key permissions (
chmod 600 ~/.ssh/id_rsa
) - Verify key is properly added to server’s authorized_keys
Issue 3: Wrong IP Address
Common mistakes:
- Double-check full IP address (e.g., 198.54.125.196 not 198.54.125.19)
- Verify port number is correct
- Confirm username is exact
Issue 4: Remote Command Conflicts
If you see “Cannot execute command-line and remote command”:
- Remove RemoteCommand from config when running direct commands
- Use appropriate host configuration for specific needs
Best Practices
- Keep separate host configurations for different needs
- Use strong keep-alive settings for stability
- Implement reconnection scripts for critical connections
- Keep your private key secure (600 permissions)
- Use descriptive SSH config host names
cPanel Configuration
- Log into cPanel
- Navigate to SSH Access > Manage SSH Keys
- Verify your public key is listed and authorized
- Check that SSH access is enabled for your account
Quick Reference Commands
bashCopy# Check SSH keys
ls -la ~/.ssh/
# View public key
cat ~/.ssh/id_rsa.pub
# Add key to agent
ssh-add ~/.ssh/id_rsa
# Check loaded keys
ssh-add -l
# Connect to server root
ssh namecheap
# Connect to specific directory
ssh clayspace#
If for some reason Ctrl + C doesn't work, you can open a new terminal window and find the process:
ps aux | grep reconnect-clayspace
# Start auto-reconnect script
~/reconnect-clayspace.sh