Accessing Virtual Machine (SSH)
- Generate SSH Key
- Install Private Key (SSH)
- Access Virtual Machine (VM) using SSH Key (Windows / Macbook)
Generate SSH Key
Here are step-by-step instructions to generate an SSH key on both Windows and Mac systems:
For Windows (Windows 10/11):
-
Ensure OpenSSH Client is Installed
- Open Settings > Apps > Apps & Features > Optional Features.
- Check if OpenSSH Client is listed.
- If not, click Add a feature, find OpenSSH Client, and install it.
- Restart your computer if needed.
-
Open Command Prompt or PowerShell
- Press
Windows + R, typecmdorpowershell, and press Enter.
- Press
-
Generate SSH Key
- Type the command:
ssh-keygen - Press Enter to accept the default save location (
C:\Users\<username>\.ssh\id_rsa).
- Type the command:
-
Set a Passphrase (Optional)
- When prompted, enter a passphrase for extra security, or just press Enter twice to skip.
-
Complete
- The keys are generated, usually two files:
id_rsa(private key) andid_rsa.pub(public key) under.sshfolder in your user directory.
- The keys are generated, usually two files:
For Mac (macOS):
-
Open Terminal
- Find Terminal via Spotlight Search or in Applications > Utilities.
-
Generate SSH Key
- Run the command, replacing your email with your own:
ssh-keygen -t ed25519 -C "your_email@example.com" - If your macOS or server doesn't support Ed25519, use:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Run the command, replacing your email with your own:
-
Save Key Location
- Press Enter to accept the default file location (
/Users/you/.ssh/id_ed25519or/Users/you/.ssh/id_rsa).
- Press Enter to accept the default file location (
-
Set a Passphrase (Optional)
- Enter a secure passphrase or press Enter to leave it blank.
-
Add SSH Key to ssh-agent
- Start the ssh-agent:
eval "$(ssh-agent -s)" - Add your private key:
ssh-add -K ~/.ssh/id_ed25519 - If using RSA:
ssh-add -K ~/.ssh/id_rsa
- Start the ssh-agent:
In both OS, your generated SSH public key is the one you copy and add to services like GitHub, servers, etc. The private key should remain secure and never shared.
Install Private Key (SSH)
Here are step-by-step instructions on how to store an SSH private key securely once generated on both Windows and Mac systems:
Windows
-
Default Location
- By default, SSH private keys are saved in your user profile directory under:
C:\Users\<your_username>\.ssh\ - The private key file is usually named
id_rsaor similar.
- By default, SSH private keys are saved in your user profile directory under:
-
Set File Permissions
- Ensure only your user account has access to the private key file:
- Right-click the private key file > Properties > Security tab.
- Remove access for any other users or groups except yourself.
- Ensure only your user account has access to the private key file:
-
Use SSH-Agent for Secure Key Storage
- Open PowerShell as Administrator.
- Enable and start the ssh-agent service:
Get-Service ssh-agent | Set-Service -StartupType Automatic Start-Service ssh-agent - Add your private key to ssh-agent to avoid typing your passphrase every time:
ssh-add $env:USERPROFILE\.ssh\id_rsa - The ssh-agent stores your private keys encrypted, protected by your Windows user credentials.
-
Backup Private Key Securely
- Copy the private key file to an encrypted external drive or secure password manager.
- Avoid storing private keys in shared or non-encrypted folders.
- Do NOT share the private key.
Mac (macOS)
-
Default Location
- Private keys are stored in the hidden
.sshfolder in your home directory:~/.ssh/id_rsa - Use
ls -la ~/.sshin Terminal to view.
- Private keys are stored in the hidden
-
Set Proper File Permissions
- Ensure your private key file is readable only by you:
chmod 600 ~/.ssh/id_rsa
- Ensure your private key file is readable only by you:
-
Add SSH Key to ssh-agent
- Start the ssh-agent:
eval "$(ssh-agent -s)" - Add your private key with:
ssh-add -K ~/.ssh/id_rsa - This stores your key securely in the macOS keychain.
- Start the ssh-agent:
-
Backup Private Key Safely
- Store a copy on an encrypted USB drive or a password manager like 1Password.
- Never upload your private key unencrypted to cloud storage.
- Keep backups separate from your main machine to avoid loss.
Security best practices:
- Always protect your private key with a strong passphrase when generating it.
- Keep private key files with strict permissions so only your user account can read them.
- Use ssh-agent to manage keys in memory instead of repeatedly entering passwords or exposing keys.
- Backup private keys securely and do not share your private keys with anyone.
Access Virtual Machine (VM) using SSH Key (Windows / Macbook)
Here are step-by-step instructions to access a Server or Virtual Machine using SSH on both Windows and Mac:
Windows (Using PowerShell or Command Prompt)
-
Ensure OpenSSH Client is Installed
- Go to Settings > Apps > Optional Features.
- Confirm OpenSSH Client is installed; if not, click Add a feature, find it, and install.
-
Open PowerShell or Command Prompt
- Press
Windows + R, typepowershellorcmd, and hit Enter.
- Press
-
Connect via SSH
- Use the command:
ssh username@server_ip - Replace
usernamewith your remote server username. - Replace
server_ipwith the server’s domain name or IP address. - If the server uses a non-standard port, add
-pfollowed by the port number:ssh username@server_ip -p port_number
- Use the command:
-
Verify and Accept Host Key
- At first connection, you’ll be asked to verify the server’s fingerprint.
- Type
yesand press Enter to continue.
-
Authenticate
- Enter your password when prompted (or if using key authentication, make sure your private key is loaded or specify it with
-i).
- Enter your password when prompted (or if using key authentication, make sure your private key is loaded or specify it with
-
You are connected when your prompt changes to the remote server’s shell.
Mac (Using Terminal)
-
Open Terminal
- Use Spotlight or find Terminal in Applications > Utilities.
-
Connect via SSH
- Type the command:
ssh username@server_ip - Substitute
usernameandserver_ipwith appropriate remote account and server info. - For custom ports, add
-poption:ssh username@server_ip -p port_number
- Type the command:
-
Accept Host Key
- For the first-time connection, review and accept the host key by typing
yes.
- For the first-time connection, review and accept the host key by typing
-
Authenticate
- Enter your password or ensure your SSH private key is loaded (use
ssh-addif needed).
- Enter your password or ensure your SSH private key is loaded (use
-
You are connected once you see the shell prompt of the remote server.
Additional Tips:
- To specify a custom private key file, use the
-iflag:ssh -i /path/to/private_key username@server_ip - To exit the SSH session, type:
exit