SSH
## Install SSH
#!/bin/bash
ssh -V
# install server
sudo apt update && sudo apt install -y openssh-server
# verify its running
sudo systemctl status ssh
# start
sudo systemctl start ssh
# on boot
sudo systemctl enable ssh
Configure SSH (Optional but Recommended)
Edit the SSH configuration file:
#!bin/bash
```bash
sudo nano /etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config
Some recommended changes:
-
Change the default port (optional for security):
Port 2222 -
Disable root login:
PermitRootLogin no -
Allow only specific users (replace
your_usernamewith your actual username):AllowUsers your_username -
Disable password authentication if you plan to use SSH keys:
PasswordAuthentication no
After making changes, save and restart SSH:
sudo systemctl restart ssh
Step 3: Allow SSH Through Firewall
If UFW is enabled, allow SSH:
sudo ufw allow OpenSSH
Or, if you changed the port:
sudo ufw allow 2222/tcp
Check the firewall status:
sudo ufw status
Enable UFW if it's not active:
sudo ufw enable
Step 4: Generate and Use SSH Keys (Recommended)
On your local machine (the one you’ll use to connect):
ssh-keygen -t rsa -b 4096
Press Enter to save the key in the default location (~/.ssh/id_rsa).
Now, copy the public key to your Ubuntu desktop:
ssh-copy-id your_username@your_server_ip
Alternatively, manually copy the key:
cat ~/.ssh/id_rsa.pub | ssh your_username@your_server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Set correct permissions:
ssh your_username@your_server_ip
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Step 5: Connect to Your Ubuntu Desktop via SSH
From your local machine:
ssh your_username@your_server_ip
If you changed the port:
ssh -p 2222 your_username@your_server_ip
Step 6: Troubleshooting
-
Check SSH service status:
sudo systemctl status ssh -
Restart SSH if needed:
sudo systemctl restart ssh -
Check firewall rules:
sudo ufw status -
Check logs for issues:
sudo journalctl -u ssh --no-pager | tail -20