Skip to main content

Configuring SSH from Scratch

This document provides a detailed guide on how to configure SSH from scratch, including installing SSH, generating key pairs, configuring the SSH service, allowing root login, and setting up key-based authentication.

Installing SSH

Install the SSH Server

On the server, you need to install the SSH server. For Debian/Ubuntu:

sudo apt update
sudo apt install openssh-server

On CentOS/RHEL:

sudo yum install openssh-server

After installation, you can start the SSH service with the following command:

sudo systemctl start sshd
sudo systemctl enable sshd

Generate an SSH Key Pair

Generate an SSH key pair on the client machine:

ssh-keygen

Generate a standard key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This command generates a 4096-bit RSA key pair. Follow the prompts; by default the key will be saved to ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

Configure the SSH Service

Set the Listening Port and Address

Edit the SSH configuration file /etc/ssh/sshd_config on the server to set the listening port and address, and allow password authentication:

Port 22
ListenAddress 0.0.0.0
PasswordAuthentication yes

You can change Port to any port you prefer for enhanced security, typically 22.

Allow Public Key Authentication

Ensure public key authentication is allowed:

PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Allow Root User Login (Optional)

If you need to allow root user login, ensure the following configuration:

PermitRootLogin yes

Restart the SSH Service

After making changes, restart the SSH service to apply the configuration:

sudo systemctl restart sshd

For WSL, use this:

service ssh restart

Complete Configuration Example

The relevant sections of your /etc/ssh/sshd_config file may look like this:

# Set listening port and address
Port 22
ListenAddress 0.0.0.0

# Allow public key authentication
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

# Allow root user login (optional)
PermitRootLogin yes

# Disable password authentication (optional)
PasswordAuthentication no

My SSH Public Keys

Windows Laptop

# DingZhiyu_magicbook_pro
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDZe/qz3NoyO5FT2O8bjRGP0ToB08ZOUuxSdpySYKwb9SpZvS9Q5M5kNpw/iS+WAzNYztp2rRWFUem512/w5nDoQWzUw8zEsGNIalhJOSpbDFCuBcaBPmdOsft8p+k2G8bwOJNpyyJsim9Rb0LRtfxvH7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