Skip to content

Overview

SSH (Secure Shell) is a protocol used for secure remote login and communication over a network. It is widely used for:

  • Remote server access

  • Secure file transfer

  • System administration

  • Cybersecurity operations


SSH Installation

Install SSH Client and Server

apt install openssh-client
apt install openssh-server

Explanation

  • openssh-client → allows you to connect to remote systems

  • openssh-server → allows others to connect to your system


SSH Usage

Basic Login

ssh user@IP_ADDRESS

Example

ssh admin@192.168.0.1

Explanation

  • Connects to remote machine

  • Prompts for password authentication


Specify Custom Port

ssh user@IP_ADDRESS -p PORT

Example

ssh admin@192.168.0.1 -p 2222

SSH Server Security Hardening

Edit SSH Configuration

vim /etc/ssh/sshd_config

Disable Root Login

Find and modify:

PermitRootLogin no

Explanation

  • Prevents direct root login

  • Reduces attack surface


Limit Sessions

MaxSessions 2

Explanation

  • Controls number of concurrent sessions

Restart SSH Service

systemctl restart sshd.service

Lock Root Account

passwd -l root

Explanation

  • Disables root password login

  • Adds an extra security layer


SSH Key-Based Authentication

Generate SSH Key Pair

ssh-keygen -t rsa

Explanation

  • Creates public and private keys

  • Files generated:

    • id_rsa → private key

    • id_rsa.pub → public key


Copy Public Key to Server

ssh-copy-id admin@192.168.0.1

Explanation

  • Adds public key to server’s authorized_keys

  • Enables passwordless login


Disable Password Authentication

Edit SSH config:

vim /etc/ssh/sshd_config

Change:

PasswordAuthentication no

Restart service:

systemctl restart sshd.service

Login Using Private Key

ssh -i id_rsa admin@192.168.0.1

Explanation

  • Uses private key instead of password

Secure Private Key

chmod 400 id_rsa

Explanation

  • Restricts access to owner only

  • Required for SSH to accept the key


File Transfer Using SCP

Copy File to Remote Server

scp filename admin@192.168.0.1:/path

Example

scp file.txt admin@192.168.0.1:/home/admin/

Explanation

  • Transfers file securely over SSH

Copy Directory Recursively

scp -r folder admin@192.168.0.1:/path

SSH Brute Force Protection with Fail2Ban

Enable Fail2Ban at Startup

systemctl enable fail2ban.service

ls -al /etc/fail2ban/

Create Custom Configuration

nano /etc/fail2ban/jail.local

Example Configuration

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
ignoreip = 127.0.0.1

Explanation

  • enabled → activate protection

  • port → service port

  • maxretry → allowed failed attempts

  • bantime → ban duration (seconds)

  • ignoreip → trusted IP addresses


Restart Fail2Ban

systemctl restart fail2ban

Practical Workflow

Secure SSH Setup

apt install openssh-server
ssh-keygen -t rsa
ssh-copy-id user@IP
vim /etc/ssh/sshd_config
systemctl restart sshd

Transfer File Securely

scp file.txt user@IP:/home/user/

Important Notes

  • Always disable root login for security

  • Prefer key-based authentication over passwords

  • Protect private keys with strict permissions

  • Use Fail2Ban to prevent brute-force attacks

  • Restart services after configuration changes


Summary Table

Command Purpose
ssh Remote login
ssh -p Custom port login
ssh-keygen Generate keys
ssh-copy-id Copy public key
scp Secure file transfer
chmod 400 Secure private key
systemctl Manage SSH service
fail2ban Protect against brute force

Conclusion

SSH is a critical tool for secure remote access. Proper configuration and hardening ensure:

  • Secure communication

  • Protection against unauthorized access

  • Efficient system management

Mastering SSH improves both system administration and cybersecurity capabilities.