
September 11, 2026
11 min read
By Kokil Thapa | Last reviewed: September 2026
Ubuntu SSH server setup is the first task on almost every fresh VPS I touch for client work. Without SSH, you cannot deploy Laravel apps, tune Apache, or run backups from your laptop. This guide walks through installing OpenSSH on Ubuntu 22.04 or 24.04, hardening sshd_config, and fixing the connection errors that waste hours on a Sunday night. If you are preparing a box for PHP or a full Ubuntu server for PHP apps, start here before anything else.
openssh-server package, enabling the ssh service, opening port 22 in UFW, creating key-based logins, and hardening /etc/ssh/sshd_config before exposing the host to the internet.What is Ubuntu SSH server setup and why does it matter?
SSH (Secure Shell) gives you an encrypted terminal session on a remote Linux machine. The server side runs sshd, the OpenSSH daemon. Your laptop runs the ssh client. Every command, file transfer, and Git deploy over SSH travels inside a TLS-like tunnel.
On production boxes I maintain with Deployer 7 and GitLab CI, SSH is the front door. A misconfigured door invites brute-force bots within minutes. A properly hardened one stays quiet for years. That is why Ubuntu server setup always starts with SSH, not with Nginx or MySQL.
SSH also powers scp, sftp, and rsync. Ansible playbooks and GitLab CI deploy jobs depend on the same daemon. Treat SSH as infrastructure, not a convenience feature you enable and forget.
How do you install and enable OpenSSH on Ubuntu?
Ubuntu Server images usually ship with the client preinstalled. The server package is separate. Install it on a fresh VPS before you close the provider's web console.
Step 1: Update packages and install openssh-server
sudo apt update
sudo apt install openssh-server -y On Ubuntu 24.04 LTS, this pulls OpenSSH from the official repositories. Verify the service is running:
sudo systemctl status ssh
sudo systemctl enable ssh Expected output shows active (running). Enable ensures SSH starts after a reboot. I have seen hosts lose remote access after kernel updates because someone forgot this step.
Step 2: Confirm the listening port
sudo ss -tlnp | grep sshd Default port is 22. You can change it later in /etc/ssh/sshd_config. If you do, update UFW and your SSH client config at the same time.
Step 3: Open the firewall
UFW is the standard firewall on Ubuntu server images. Allow SSH before enabling UFW, or you lock yourself out:
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose For a custom port such as 2222, use sudo ufw allow 2222/tcp instead. Pair this with the broader checklist in our initial Ubuntu server setup guide.
Step 4: Create a non-root sudo user
Logging in as root over SSH is convenient and risky. Create a deploy user first:
sudo adduser deploy
sudo usermod -aG sudo deploy Detailed user and group patterns live in the Ubuntu user management guide. For sister sites on shared EC2 infrastructure, every site gets its own deploy user with a restricted sudoers file.
How do you set up SSH key authentication on Ubuntu?
Password logins get hammered by bots. Key-based auth removes that attack surface. Generate a key pair on your local machine, not on the server.
Generate keys on your workstation
ssh-keygen -t ed25519 -C "deploy@your-laptop" -f ~/.ssh/id_ed25519_deploy Ed25519 keys are short, fast, and widely supported in 2026. Use RSA 4096 only if an old system demands it. Protect the private key with a passphrase. A strong passphrase generator helps if you rotate credentials regularly.
Copy the public key to the server
ssh-copy-id -i ~/.ssh/id_ed25519_deploy.pub deploy@YOUR_SERVER_IP Manual method if ssh-copy-id is unavailable:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "YOUR_PUBLIC_KEY" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys Ownership must match the login user. Wrong permissions cause silent rejections. See the dedicated SSH key-only auth setup article for a full walkthrough.
Test before you disable passwords
Open a second terminal. Connect with your key:
ssh -i ~/.ssh/id_ed25519_deploy deploy@YOUR_SERVER_IP Only after this works should you edit sshd_config. I keep the provider's web console open during every hardening session. One typo has cost me a support ticket on a Friday evening.
Which sshd_config settings should you change for production?
The main config file lives at /etc/ssh/sshd_config. Ubuntu also reads drop-in files from /etc/ssh/sshd_config.d/. Prefer drop-ins for clarity:
sudo nano /etc/ssh/sshd_config.d/99-hardening.conf Add these production-oriented settings:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
AllowUsers deploy
ClientAliveInterval 300
ClientAliveCountMax 2 Validate syntax before reload:
sudo sshd -t
sudo systemctl reload ssh Never use restart during a live session unless you must. reload applies config without dropping existing connections. Official reference: the Ubuntu sshd_config man page.
| Setting | Dev / staging | Production | Why it matters |
|---|---|---|---|
PermitRootLogin | prohibit-password | no | Root is a predictable target for bots |
PasswordAuthentication | yes (temporary) | no | Stops brute-force password attacks |
Port | 22 | custom (optional) | Reduces noise, not real security alone |
AllowUsers | omitted | deploy | Limits who can authenticate at all |
MaxAuthTries | 6 | 3 | Closes failed attempts faster |
Changing the port cuts log noise but does not replace keys and firewall rules. Combine it with fail2ban on Ubuntu for automatic IP bans after repeated failures.
Optional: client-side config for daily use
Store connection shortcuts in ~/.ssh/config on your laptop:
Host prod-law-portal
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519_deploy
Port 22 Then connect with ssh prod-law-portal. This saves time when you juggle multiple client servers across Nepal and abroad.
How do you harden SSH beyond basic config?
Config changes are the baseline. Layer these extras before you host client data or payment flows.
- Install fail2ban — bans IPs after failed SSH attempts. Documented in our server hardening guide.
- Keep packages current —
sudo apt update && sudo apt upgrade openssh-serverpatches CVEs quickly. - Restrict source IPs — if your office IP is stable, allow SSH only from that range in UFW.
- Use a bastion or VPN — for high-value systems, SSH should not face the public internet directly.
- Audit auth logs —
sudo journalctl -u ssh -n 50shows recent login attempts.
The broader picture sits in the Ubuntu security hardening guide. For ongoing patches, follow the Ubuntu security updates guide.
On legal-tech portals and eCommerce backends I maintain, SSH access is limited to named deploy users. GitLab CI uses a dedicated key with forced command restrictions where possible. That pattern aligns with Linux system administration work I deliver for Nepal clients who need hands-on server care without hiring a full-time ops engineer.
Two-factor authentication (optional)
Google Authenticator or hardware keys add a second factor on top of SSH keys. Install libpam-google-authenticator and configure PAM. This is overkill for a small brochure site but sensible for databases holding sensitive documents.
How do you troubleshoot common Ubuntu SSH connection errors?
SSH failures usually fall into a short list. Work through them in order.
Connection refused
The daemon is not running or the firewall blocks the port. Check:
sudo systemctl status ssh
sudo ufw status
sudo ss -tlnp | grep ssh Start the service with sudo systemctl start ssh if it is stopped.
Permission denied (publickey)
Wrong key, wrong user, or bad file permissions on the server. Fix ownership:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R deploy:deploy ~/.ssh Run the client in verbose mode to see what the server rejects:
ssh -vvv deploy@YOUR_SERVER_IP Connection timed out
Usually a cloud security group or network ACL blocks port 22. Check your VPS provider's firewall panel in addition to UFW. I have lost an hour to AWS security groups while UFW looked perfect.
Locked out after a bad config edit
Use your VPS provider's web console (VNC or serial). Fix sshd_config, run sudo sshd -t, then reload. Keep a backup:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak Document the rollback path in your server backup strategy so config files restore with the rest of the stack.
How does SSH fit into a full Ubuntu web stack?
SSH is step one. Step two is the application layer. Typical next installs on boxes I configure:
- Install Nginx on Ubuntu as the web front end
- Install PHP on Ubuntu for Laravel 12 or 13 apps (PHP 8.3+ required)
- Install MySQL on Ubuntu for relational data
- Server monitoring for uptime alerts
Sister legal-tech sites on shared EC2—such as flows documented under Notary Kathmandu—share Deployer 7 pipelines that depend on reliable SSH from GitLab runners. Break SSH and every deploy stops.
If you prefer infrastructure-as-code over manual steps, the Ansible automation guide codifies the same hardening. For hosting selection before you even get SSH access, see domain registration and hosting in Nepal.
Official Ubuntu documentation for the OpenSSH server package is at ubuntu.com/server/docs/service-openssh. The upstream project overview lives at openssh.com.
Key Takeaways
- Install
openssh-server, enable the service, and allow SSH in UFW before you close the provider console. - Generate ed25519 keys locally, copy them with
ssh-copy-id, and test login before disabling passwords. - Harden via
/etc/ssh/sshd_config.d/: no root login, no password auth,AllowUsersset, thensshd -tand reload. - Layer fail2ban, security updates, and optional IP restrictions for production Laravel and WordPress hosts.
- Diagnose failures with
systemctl status ssh, UFW rules, cloud security groups, andssh -vvv. - Keep provider console access open until key auth is confirmed—lockouts are common and fixable but stressful.
People Also Ask
Is OpenSSH preinstalled on Ubuntu Server?
Ubuntu Server includes the SSH client by default. The server daemon requires the openssh-server package. Desktop editions may omit it entirely. Run dpkg -l openssh-server to check.
Should I change the default SSH port on Ubuntu?
Changing port 22 reduces log noise from automated scanners. It does not stop targeted attacks. Always combine a custom port with key-only auth, UFW rules, and fail2ban for real protection.
Can I use SSH on Ubuntu without a static IP?
Yes. Connect with your VPS public IP or a DNS A record. Dynamic home IPs work for outbound SSH client connections. For inbound access to a home server, use a dynamic DNS service or a reverse tunnel.
What is the difference between ssh restart and reload?
systemctl reload ssh applies config changes without dropping active sessions. restart stops and starts the daemon, which can disconnect your current shell. Use reload after validated config edits.
Next steps for your Ubuntu SSH server setup
Ubuntu SSH server setup takes twenty focused minutes on a fresh VPS. Install OpenSSH, open the firewall, deploy keys, harden sshd_config, and add fail2ban. That foundation supports everything else—PHP-FPM, MySQL, Deployer releases, and nightly backups.
If you want someone to harden servers, wire CI/CD, and keep them patched, review our support and maintenance services or contact us with your stack details. Solid SSH today prevents emergency console logins tomorrow.
Frequently Asked Questions
0 Comments
Leave a comment
Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

