Skip to content

SSH (Secure Shell) — Secure Remote Access to Linux Systems

SSH (Secure Shell) is the standard protocol for securely accessing and managing remote Linux systems over a network. It encrypts all communication between the client and the server, making it the preferred method for remote administration, file transfers, automation, and infrastructure management. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, Security Engineer, and Site Reliability Engineer (SRE) uses SSH daily.


Learning Path

Linux Mastery → Module 8: Networking → Lesson 11

Difficulty: Beginner → Intermediate

Reading Time: 80 Minutes

Course Progress

Course: Linux Mastery

Module: Networking

Lesson: 11 of 13


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand SSH
  • Connect securely to remote Linux servers
  • Configure SSH authentication
  • Generate SSH key pairs
  • Transfer files using SSH
  • Configure SSH client and server
  • Use SSH port forwarding
  • Secure SSH in production

Prerequisites

Complete:

  • Module 1 – Linux Fundamentals
  • Module 2 – Linux Command Line Essentials
  • Module 3 – Text Processing
  • Module 4 – File Management
  • Module 5 – Users and Groups
  • Module 6 – Process Management
  • Module 7 – Package Management
  • Module 8 Lessons 1–10

Why Learn SSH?

Imagine:

  • Managing a cloud server on AWS, Azure, or GCP.
  • Deploying applications remotely.
  • Connecting to Kubernetes nodes.
  • Troubleshooting production servers.
  • Automating infrastructure tasks.

SSH is the secure method used for all these tasks.


What is SSH?

SSH stands for:

Secure Shell

It is a cryptographic network protocol that provides secure remote access to systems over untrusted networks.

SSH encrypts:

  • Usernames
  • Passwords
  • Commands
  • File transfers
  • Session data

How SSH Works

SSH Client
Encrypted Connection
SSH Server (sshd)
Remote Linux System

All communication is encrypted before it travels across the network.


SSH Architecture

User
SSH Client
Encrypted Channel
SSH Server
Linux Operating System

SSH Default Port

SSH uses:

TCP Port 22

Verify that SSH is listening.

sudo ss -tulpn | grep :22

Basic SSH Connection

Connect to a remote server.

ssh username@192.168.1.100

Example:

ssh admin@192.168.1.100

Connect Using a Hostname

ssh admin@example.com

Connect Using a Different Port

If the SSH server listens on port 2222:

ssh -p 2222 admin@192.168.1.100

First-Time Connection

On the first connection, SSH displays the server's fingerprint.

Example:

Are you sure you want to continue connecting?

Type:

yes

The server's public key is then stored in:

~/.ssh/known_hosts

SSH Authentication Methods

SSH supports:

  • Password authentication
  • Public key authentication

Public key authentication is recommended for production systems.


Generate an SSH Key Pair

Generate an Ed25519 key pair (recommended):

ssh-keygen -t ed25519

Or generate an RSA key pair:

ssh-keygen -t rsa -b 4096

Default location:

~/.ssh/

Generated files:

id_ed25519

id_ed25519.pub

or

id_rsa

id_rsa.pub

Copy Public Key to Server

ssh-copy-id admin@192.168.1.100

After copying the key, future logins can use key-based authentication.


Login Using a Private Key

ssh -i ~/.ssh/id_ed25519 \
admin@192.168.1.100

SSH Configuration File

Client configuration:

~/.ssh/config

Example:

Host webserver

    HostName 192.168.1.100

    User admin

    Port 22

    IdentityFile ~/.ssh/id_ed25519

Connect using:

ssh webserver

SSH Server Configuration

Server configuration file:

/etc/ssh/sshd_config

Common settings:

Port 22

PermitRootLogin no

PasswordAuthentication no

PubkeyAuthentication yes

After modifying the configuration:

sudo systemctl restart sshd

Check SSH Service

systemctl status sshd

Ubuntu may use:

systemctl status ssh

View SSH Logs

Systems using systemd:

journalctl -u sshd

Ubuntu systems may use:

journalctl -u ssh

Traditional authentication log (distribution-dependent):

tail -f /var/log/auth.log

SSH Port Forwarding

Local port forwarding:

ssh -L 8080:localhost:80 \
admin@server

This forwards:

localhost:8080


server:80

Useful for securely accessing internal services.


Remote Port Forwarding

ssh -R 9000:localhost:22 \
admin@server

Allows the remote server to access a local service.


Dynamic Port Forwarding

Create a SOCKS proxy.

ssh -D 1080 admin@server

Useful for secure tunneling of network traffic.


Common Commands

Connect to a server.

ssh admin@server

Use a custom port.

ssh -p 2222 admin@server

Generate SSH keys.

ssh-keygen -t ed25519

Copy public key.

ssh-copy-id admin@server

Check SSH service.

systemctl status sshd

Real Production Examples

Access a cloud VM.

ssh ubuntu@34.100.x.x

Connect to a Kubernetes node.

ssh admin@worker-node

Access a database server.

ssh dbadmin@db01

Forward a local port.

ssh -L 5432:localhost:5432 dbadmin@db01

Production Perspective

SSH is used for:

  • Cloud administration
  • Kubernetes management
  • CI/CD deployments
  • Infrastructure automation
  • Remote troubleshooting
  • Secure file transfers
  • Database administration
  • Server maintenance

It is the standard protocol for secure remote Linux administration.


Hands-on Lab

Task 1

Verify the SSH service.

systemctl status sshd

Task 2

Generate an SSH key pair.

ssh-keygen -t ed25519

Task 3

Display the public key.

cat ~/.ssh/id_ed25519.pub

Task 4

Connect to a remote server.

ssh username@server-ip

Task 5

Create an SSH client configuration.

Host lab-server
    HostName 192.168.1.100
    User admin
    IdentityFile ~/.ssh/id_ed25519

Task 6

Test the configuration.

ssh lab-server

Task 7

Verify that SSH is listening.

sudo ss -tulpn | grep :22

Task 8

Review SSH logs.

journalctl -u sshd

Command Deep Dive

Command Purpose Production Example
ssh user@host Connect to a server Remote administration
ssh -p Use custom port Hardened servers
ssh-keygen Generate key pair Secure authentication
ssh-copy-id Install public key Passwordless login
ssh -L Local port forwarding Database access
ssh -R Remote port forwarding Remote tunneling
ssh -D Dynamic port forwarding SOCKS proxy
systemctl status sshd Verify SSH service Troubleshooting

Common SSH Errors

Error Possible Cause
Connection refused SSH service not running or port blocked
Permission denied (publickey) Public key missing or incorrect permissions
Permission denied (password) Incorrect credentials or password authentication disabled
Connection timed out Firewall, routing, or network issue
Host key verification failed Server host key has changed

Production Troubleshooting Scenario

Scenario

A DevOps engineer cannot SSH into a production server.

Investigation:

Verify network connectivity.

ping server-ip

Check whether SSH is listening.

sudo ss -tulpn | grep :22

Verify the SSH service.

systemctl status sshd

Review logs.

journalctl -u sshd

The logs indicate that the user's public key is not authorized.

Copy the public key to the server.

ssh-copy-id admin@server

Reconnect.

ssh admin@server

Access is restored successfully.


Best Practices

  • Use SSH key-based authentication instead of passwords.
  • Disable direct root login.
  • Disable password authentication when key-based authentication is fully deployed.
  • Protect private keys with strong passphrases.
  • Restrict SSH access using firewalls and security groups.
  • Keep OpenSSH updated with security patches.
  • Monitor SSH logs for unauthorized access attempts.

Common Mistakes

❌ Using weak passwords for SSH.

✅ Avoid using weak passwords for SSH when a safer approach exists.


❌ Enabling direct root login in production.

✅ Avoid this mistake: enabling direct root login in production.


❌ Sharing private SSH keys.

✅ Avoid this mistake: sharing private SSH keys.


❌ Leaving password authentication enabled unnecessarily.

✅ Do not leave password authentication enabled unnecessarily.


❌ Ignoring SSH log files after failed login attempts.

✅ Always review SSH log files after failed login attempts.


Interview Questions

Beginner

  1. What is SSH?
  2. Which port does SSH use by default?
  3. How do you connect to a remote server?
  4. What is the difference between a public key and a private key?

Intermediate

  1. Why is SSH key authentication preferred over passwords?
  2. What is the purpose of ssh-copy-id?
  3. What is local port forwarding?
  4. Where is the SSH server configuration stored?

Architect Level

  1. How would you secure SSH access for hundreds of production servers?
  2. How would you implement centralized SSH key management?
  3. How would you troubleshoot intermittent SSH connection failures in a cloud environment?

Summary

In this lesson, you learned:

  • SSH fundamentals
  • Secure remote access
  • SSH authentication
  • SSH key pairs
  • SSH configuration
  • Port forwarding
  • SSH troubleshooting
  • Production security best practices

SSH is the standard protocol for secure remote Linux administration. Mastering SSH enables administrators to manage servers, automate deployments, transfer files, and securely access infrastructure across on-premises and cloud environments.


Key Takeaways

  • SSH provides encrypted remote access to Linux systems.
  • SSH uses TCP port 22 by default.
  • SSH key-based authentication is more secure than passwords.
  • SSH configuration files simplify repeated connections.
  • Port forwarding enables secure access to remote services.
  • Securing SSH is critical for protecting production infrastructure.

What's Next?

SCP (Secure Copy Protocol) — Secure File Transfer Between Linux Systems

You'll explore:

  • Secure file transfers using SCP
  • Copying files between local and remote systems
  • Recursive transfers
  • Preserving permissions
  • Production file transfer practices

Then you'll continue with rsync for efficient file synchronization and backups.