SSH Mastery

SSH is one of the most essential tools for Linux administration, DevOps, cloud computing, and cybersecurity. It provides encrypted remote access, secure file transfer, and advanced capabilities such as port forwarding and tunneling.

1. Introduction
SSH (Secure Shell) is a secure network protocol used to remotely connect to another computer over an unsecured network.It encrypts all communication between the client and server, preventing hackers from reading passwords or data.

SSH is commonly used by:

  • Linux Administrators
  • DevOps Engineers
  • Cloud Engineers
  • Developers
  • System Administrators
  • Security Engineers
Default Port:22/TCP

2.Why People use SSH

SSH allows users to

  • Login remotely
  • Execute commands
  • Transfer files
  • Manage servers
  • Forward ports
  • Create encrypted tunnels

Without SSH

User -------- Password -------- Server

Password travels in plain text.
With SSH

User === Encrypted Data === Server

Everything is encrypted.

Benefits

  • Secure Login
  • Remote Administration
  • Secure File Transfer
  • Data Encryption
  • Authentication
  • Port Forwarding

4. SSH Architecture

img_1784353220_7980696f_ssh.webp

5.SSH Key Exchange Flow
img_1784354095_1f92469c_image.webp
6.SSH Public Key Authentication
img_1784354195_3b6a0d84_image.webp
7.SSH Components
img_1784354297_b0885d21_image.webp
8.Complete SSH Workflow
img_1784354403_78940586_image.webp


9. SSH Encryption
Symmetric encryption uses the exact same secret key to both lock (encrypt) and unlock (decrypt) the data. Because both your local computer and the remote server share this identical key, the encryption process is incredibly fast.
Examples: AES-128, AES-192, AES-256, ChaCha20

Asymmetric encryption is a security method that uses two different, mathematically linked keys to protect data: a Public Key and a Private Key.

1. The Public Key (The Lock)
This key is completely open to the world. Anyone can use it to scramble (encrypt) a message. However, once the data is locked with this public key, it cannot be unlocked by the public key again.

2. The Private Key (The Secret Key)
This key is kept strictly secret by the owner. It is the only key capable of unscrambling (decrypting) the data locked by the corresponding public key.
ExampleRSA, ECDSA, ED25519

Generate Key

ssh-keygen

Output
img_1784356018_4ecc6b0f_image.webp
Note: kali is a username of linux system and when enter the upper command then will ask the file name but you can simply click on Enter  


10. SSH Directory Structure

img_1784356297_537e485f_image.webp

id_ed25519: Your strictly secret private key used to prove your identity.
id_ed25519.pub: Your public key that you copy to remote servers so they recognize you.
known_hosts: A list of fingerprints from servers you trust to prevent connection tampering.
known_hosts.old: The automatically generated backup of your trusted servers list.
authorized_keys: A list of public keys belonging to other users allowed to log into your machine.
agent: A temporary background background process file that holds your active keys to skip typing passphrases.

11. Most Important SSH Commands
Connect

ssh username@hostname   OR  ssh ubuntu@192.168.1.100

Connect using Port

ssh -p 2222 user@server

Connect using Private Key

ssh -i private-key user@server

Execute Command

ssh user@server "ls -l"

Copy Public Key

ssh-copy-id user@server

Note: After this command ssh will not ask you to provide password of this server

Generate RSA Key

ssh-keygen -t rsa -b 4096

Generate ED25519 Key

ssh-keygen -t ed25519

Remove Known Host

ssh-keygen -R hostname

Verbose Mode

ssh -v user@server

Simple Verbose

ssh -vv user@server

More verbose

ssh -vvv user@server

Maximum debugging


12. Port Forwarding
SSH Port Forwarding (also called SSH Tunneling) lets you securely route network traffic through an existing SSH connection. It wraps normal network traffic inside a secure, encrypted SSH tunnel.

Local Port Forwarding
Lets you access a service running on the remote server (or its network) using a port on your local machine.

ssh -L 8080:localhost:80 user@server

Remote Port Forwarding
The exact opposite. It lets the remote server access a service running on your local machine.

ssh -R 9090:localhost:80 user@server

Dynamic SOCKS Proxy
Turns the SSH connection into a private VPN/SOCKS proxy.

ssh -D 1080 user@server

Disable Password Authentication

ssh -o PasswordAuthentication=no user@server

13. SCP Commands
SCP (Secure Copy Protocol) is a command-line tool used to securely copy files and directories between computers over a network.It uses SSH under the hood, meaning the file transfer is fully encrypted and uses the exact same keys and passwords as your normal SSH connections.

Copy file to server
scp file.txt user@server:/home/user/

Copy folder

scp -r folder user@server:/home/user/

Download file

scp user@server:/tmp/file.txt .

Use custom port

scp -P 2222 file.txt user@server:/home/user/

14. SSH Server Configuration
Location
/etc/ssh/sshd_config

Important Settings

Port 22: Sets the network port the SSH server listens on for incoming connections.
PermitRootLogin no: Blocks the highly targeted root (admin) account from logging in directly over SSH.
PasswordAuthentication no: Disables standard text passwords to eliminate the risk of brute-force guessing attacks.
PubkeyAuthentication yes: Enables secure cryptographic SSH key pairs as the mandatory way to log in.
MaxAuthTries 3: Disconnects the session if a user fails to log in successfully within three attempts.
ClientAliveInterval 300: Forces the server to check if the user is still there every 5 minutes (300 seconds) during inactivity.
ClientAliveCountMax 2: Drops the connection if the user's computer fails to respond to two active checks in a row.

15. SSH Service Commands
Ubuntu / Debian Based linux

sudo systemctl status ssh
sudo systemctl start ssh
sudo systemctl stop ssh
sudo systemctl restart ssh
sudo systemctl enable ssh

RHEL / Fedora 

sudo systemctl status sshd
sudo systemctl start sshd
sudo systemctl stop sshd
sudo systemctl restart sshd
sudo systemctl enable sshd

17. Common Troubleshooting
ProblemPossible CauseSolution
Connection refusedSSH service stoppedsudo systemctl start ssh
Permission deniedWrong key/passwordVerify keys and permissions  400
Host key changedServer reinstalledssh-keygen -R hostname
TimeoutFirewall/network issueCheck firewall and connectivity
No route to hostNetwork unreachableVerify IP address and routing

18.SSH vs Telnet Overview
FeatureSSH (Secure Shell)Telnet
Full FormSecure ShellTelecommunication Network
Security✅ Encrypted❌ Not Encrypted
Default Port2223
AuthenticationPassword, Public Key, MFAUsername & Password
Data EncryptionAES, ChaCha20, etc.None
Password ProtectionEncryptedPlain Text
File TransferSCP, SFTPNot Supported
Remote Command ExecutionYesYes
Integrity CheckYesNo
CompressionYesNo
Port ForwardingYesNo
X11 ForwardingYesNo
Public Key AuthenticationYesNo
Recommended Today✅ Yes❌ No


19. Conclusion
SSH is one of the most essential tools for Linux administration, DevOps, cloud computing, and cybersecurity. It provides encrypted remote access, secure file transfer, and advanced capabilities such as port forwarding and tunneling. Mastering SSH commands, key management, configuration, and security best practices is fundamental for anyone managing Linux systems or cloud infrastructure.

Share This Post

Latest Comments (0)

No comments yet. Be the first to comment!