You are currently viewing SSH Command Cheat Sheet

SSH Command Cheat Sheet

1. Connect as Root with Disabled Public Key Authentication

ssh -o PubkeyAuthentication=no root@example-host

2. Connect with Private Key

ssh -i ~/.ssh/alice.pem alice@example.com

3. SSH Configuration File

3.1. View and Edit SSH Config File

cat ~/.ssh/config

3.2. Check Configuration Loading

ssh -v server

3.3. Directory Permissions

ls -ld -- .ssh*/

3.4. Multiple Private Keys in Config

cat ~/.ssh/config

4. SSH-Keyscan

ssh-keyscan -H 192.168.1.1 >> ~/.ssh/known_hosts
ssh-keyscan hostname
ssh-keyscan -p 10.0.0.0
ssh-keyscan -t rsa 10.0.0.0

5. SSH Troubleshooting

journalctl -u sshd
grep sshd /var/log/auth.log
grep sshd /var/log/secure
ssh -vvvvvv host
ssh -T user@example.com
ssh -vT user@example.com
ssh -G host

6. PuTTY Debug

Navigate to "Session" -> "Logging" -> "All session output" -> "SSH packet data"

7. SSH Key Management

7.1. Verify SSH Agent

eval "$(ssh-agent -s)"

7.2. Add SSH Private Key to Agent

ssh-add ~/.ssh/id_rsa

7.3. Change Passphrase

ssh-keygen -p

7.4. Test SSH Connection

ssh -T user@example.com

7.5. SSH over HTTPS

7.6. SSH Config for HTTPS

Create or edit ~/.ssh/config:

Host example.com
  Hostname ssh.example.com
  Port 443

Test the configuration:

ssh -T user@example.com

7.7. Fixing Permissions Issue

chmod 600 ~/.ssh/config

7.8. Multiple GitHub Accounts

Configure SSH and multiple accounts:

ssh-keygen -t rsa -b 4096 -C "email@examplePersonal"
ssh-keygen -t rsa -b 4096 -C "email@exampleWork"

Update ~/.ssh/config:

Host example-personal
  Hostname ssh.examople.com
  User user
  Port 443
  IdentityFile ~/.ssh/id_rsa_example-personal

Host example-work
  Hostname ssh.example.com
  User user
  Port 443
  IdentityFile ~/.ssh/id_rsa_example-work

7.9. SSH Copy-ID

ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-server

8. SSH Server Configuration

8.1. Directory Permissions

chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts
chmod 644 ~/.ssh/config
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/github_rsa
chmod 644 ~/.ssh/github_rsa.pub
chmod 600 ~/.ssh/mozilla_rsa
chmod 644 ~/.ssh/mozilla_rsa.pub

8.2. Copy Public Key Methods

ssh sheena@192.168.0.11 "chmod 700 .ssh; chmod 600 .ssh/authorized_keys"
cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
scp -pr ~/.ssh/id_rsa.pub user@example.com:/tmp
cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
ssh-copy-id -i mykey.rsa.pub -o "IdentityFile hostkey.rsa" user@target

8.3. Change SSH Listening Port

Edit /etc/ssh/sshd_config:

Port 22
Port 2022

8.4. Troubleshoot SSH Permission Issues

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa

9. SSH Agent Management

eval "$(ssh-agent -s)"
eval `ssh-agent`
SSH_AGENT_PID="$(pidof ssh-agent)" ssh-agent -k
kill -9 $(pidof ssh-agent)
eval "$(ssh-agent -k)"

9.1. Start SSH Agent in Background

exec ssh-agent bash

9.2. Add and List Identities

ssh-add
ssh-add -l
ssh-add -L

9.3. Delete Identities

ssh-add -D
ssh-add -d /home/user/.ssh/id_rsa

9.4. Lock and Unlock SSH Agent

ssh-add -x

10. SSH-Keygen and Permissions

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-agent -s
ssh-add ~/.ssh/id_rsa
ssh-keygen -p
ssh -T user@example.com
ssh -T -p 443 user@ssh.example.com
chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts
chmod 644 ~/.ssh/config
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys2
chmod 644 ~/.ssh/known_hosts

11. SSH to Multiple Hosts via Bastion Host

Edit ~/.ssh/config:

Host private1
  IdentityFile ~/.ssh/rsa_private_key
  ProxyCommand ssh user@example -W %h:%p

Host bastion
  IdentityFile ~/.ssh/example_rsa_key

12. Fixing Permissions Issue

chmod 600 privatekey.ppk

13. PuTTY Key Format

puttygen

14. Convert PuTTY PPK to OpenSSH Key on Windows

Putty Key Generator - Load private key
Putty Key Generator - Conversions - Export OpenSSH key

Note: Ensure to replace placeholder values like user, host, etc., with your actual information.

Table Of Commands

#CommandDescription
1ssh -o PubkeyAuthentication=no root@vg-ubuntu-02Connect as Root with Disabled Public Key Authentication
2ssh -i ~/.ssh/alice.pem alice@ec2-23-22-230-24.compute-1.amazonaws.comConnect with Private Key
3cat ~/.ssh/configView and Edit SSH Config File
3.1ssh -v serverCheck Configuration Loading
3.2ls -ld -- .ssh*/Directory Permissions
3.3cat ~/.ssh/configMultiple Private Keys in Config
4ssh-keyscan -H 192.168.1.162 >> ~/.ssh/known_hostsSSH-Keyscan
5journalctl -u sshdSSH Troubleshooting
6PuTTY DebugNavigate to “Session” -> “Logging” -> “All session output” -> “SSH packet data”
7eval "$(ssh-agent -s)"Verify SSH Agent
7.1ssh-add ~/.ssh/id_rsaAdd SSH Private Key to Agent
7.2ssh-keygen -pChange Passphrase
7.3ssh -T git@github.comTest SSH Connection
7.4ssh -T -p 443 git@ssh.github.comSSH over HTTPS
7.5Create or Edit ~/.ssh/configSSH Config for HTTPS
7.6chmod 600 ~/.ssh/configFixing Permissions Issue
7.7Configure SSH and Multiple AccountsMultiple GitHub Accounts
7.8ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-serverSSH Copy-ID
8Directory PermissionsSSH Server Configuration
8.1chmod 700 ~/.sshCopy Public Key Methods
8.2chmod 644 ~/.ssh/authorized_keysChange SSH Listening Port
8.3Troubleshoot SSH Permission Issues
9eval "$(ssh-agent -s)"SSH Agent Management
9.1exec ssh-agent bashStart SSH Agent in Background
9.2ssh-addAdd and List Identities
9.3ssh-add -DDelete Identities
9.4ssh-add -xLock and Unlock SSH Agent
10ssh-keygen -t rsa -b 4096 -C "your_email@example.com"SSH-Keygen and Permissions
11Edit ~/.ssh/configSSH to Multiple Hosts via Bastion Host
12chmod 600 privatekey.ppkFixing Permissions Issue
13PuttygenPuTTY Key Format
14PuTTY Key Generator – Conversions – Export OpenSSH keyConvert PuTTY PPK to OpenSSH Key on Windows
Note: The description column provides a brief explanation of each command.

Leave a Reply