How to Install and Use SSH on Ubuntu Server
Secure Shell (SSH) is one of the most essential tools for system administrators, developers, and homelab enthusiasts. It allows you to securely connect to remote servers, run commands, transfer files, and manage your infrastructure from anywhere. In this blog, we’ll cover how to install SSH on an Ubuntu server, enable it, and start using some basic commands.
What is SSH?
SSH (Secure Shell) is a protocol that provides secure encrypted communication between two computers over a network. Unlike traditional methods like Telnet, SSH encrypts all traffic, making it much safer to manage your remote server.
With SSH, you can:
- Log into your server remotely.
- Execute commands on the server.
- Transfer files securely (using
scporsftp). - Create secure tunnels for services.
Step 1: Update Your Ubuntu Server
Before installing SSH, make sure your server packages are up-to-date. Run:
sudo apt update && sudo apt upgrade -y
Step 2: Install the OpenSSH Server
Ubuntu provides the openssh-server package to enable SSH access. Install it using:
sudo apt install openssh-server -y
Step 3: Enable and Start SSH Service
After installation, the SSH service should be running. To check its status:
sudo systemctl status ssh
If it’s not running, start and enable it:
sudo systemctl start sshsudo systemctl enable ssh
This ensures SSH will automatically start on boot.
Step 4: Check SSH Listening Port
By default, SSH listens on port 22. To verify:
sudo ss -tulpn | grep ssh
If you want extra security, you can change the port number in the SSH configuration file:
sudo nano /etc/ssh/sshd_config
Look for the line:
#Port 22
Uncomment and change it (e.g., Port 2222), then restart SSH:
sudo systemctl restart ssh
Step 5: Connect to Your Ubuntu Server via SSH
From another computer (Linux, macOS, or Windows with PowerShell), run:
ssh username@server_ip
Replace username with your Ubuntu user and server_ip with your server’s IP address.
Example:
If this is your first connection, it will ask you to confirm the server’s fingerprint. Type yes and enter your password.
Step 6: Useful Basic SSH Commands
Once you’re inside your Ubuntu server via SSH, you can use it like a normal terminal. Here are some useful commands:
- Check who’s logged in:
who - Show system information:
uname-a - Check disk usage:
df-h - Check memory usage:free -m
- Exit SSH session:
exit
Step 7: Transferring Files with SCP
SSH also allows secure file transfer. Example:
- Copy a file from your local computer to server:scp file.txt username@server_ip:/home/username/
- Copy a file from server to your local computer:scp username@server_ip:/home/username/file.txt .
Conclusion
SSH is a must-have for anyone managing Ubuntu servers. With just a few steps, you can install and enable SSH, then securely log in, execute commands, and transfer files. Once you get comfortable with the basics, you can explore advanced features like SSH key authentication, port forwarding, and hardening your SSH server for extra security.
👉 Would you like me to also add a section on setting up SSH key-based authentication (so you don’t need a password every time) for this blog? It’s a common next step.
You said:
yes
ChatGPT said:
Perfect 👍 Here’s the updated blog with SSH Key-based Authentication added in:
How to Install and Use SSH on Ubuntu Server
Secure Shell (SSH) is one of the most essential tools for system administrators, developers, and homelab enthusiasts. It allows you to securely connect to remote servers, run commands, transfer files, and manage your infrastructure from anywhere. In this blog, we’ll cover how to install SSH on an Ubuntu server, enable it, and start using some basic commands. We’ll also set up SSH key-based authentication for easier and more secure logins.
What is SSH?
SSH (Secure Shell) is a protocol that provides secure encrypted communication between two computers over a network. Unlike traditional methods like Telnet, SSH encrypts all traffic, making it much safer to manage your remote server.
With SSH, you can:
- Log into your server remotely.
- Execute commands on the server.
- Transfer files securely (using
scporsftp). - Create secure tunnels for services.
Step 1: Update Your Ubuntu Server
sudo apt update && sudo apt upgrade -y
Step 2: Install the OpenSSH Server
sudo apt install openssh-server -y
Step 3: Enable and Start SSH Service
sudo systemctl enable ssh
sudo systemctl start ssh
Check status:
sudo systemctl status ssh
Step 4: Check SSH Listening Port
By default, SSH listens on port 22. To confirm:
sudo ss -tulpn | grep ssh
You can change the port in /etc/ssh/sshd_config for extra security.
Step 5: Connect to Your Ubuntu Server via SSH
From your client computer:
ssh username@server_ip
Example:
Step 6: Useful Basic SSH Commands
- Show logged-in users:
who - Check system info:
uname-a - Check disk usage:
df-h - Check memory usage:free -m
- Exit session:
exit
Step 7: Transferring Files with SCP
- Upload file to server:scp file.txt username@server_ip:/home/username/
- Download file from server:scp username@server_ip:/home/username/file.txt .
Step 8: Set Up SSH Key-Based Authentication (Recommended)
Using SSH keys improves both security and convenience. Instead of typing a password each time, your server will authenticate you with a cryptographic key pair.
1. Generate SSH Keys on Your Local Machine
Run the following command on your local computer:
ssh-keygen -t rsa -b 4096
Press Enter to save in the default location (~/.ssh/id_rsa) and optionally set a passphrase for extra protection.
2. Copy Your Public Key to the Server
Use ssh-copy-id to upload your key:
ssh-copy-id username@server_ip
If ssh-copy-id isn’t available, you can manually copy your public key:
cat ~/.ssh/id_rsa.pub | ssh username@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
3. Test Key-Based Login
Now try logging in:
ssh username@server_ip
You should log in without entering a password (unless you set a passphrase for your private key).
4. Disable Password Authentication (Optional but More Secure)
Once key authentication works, you can disable password logins to improve security.
Edit SSH config on server:
sudo nano /etc/ssh/sshd_config
Find and set:
PasswordAuthentication no
Save and restart SSH:
sudo systemctl restart ssh
⚠️ Make sure your key works before disabling passwords, otherwise you might lock yourself out.
Conclusion
With SSH set up on your Ubuntu server, you now have a secure way to log in, execute commands, and transfer files. By configuring SSH keys, you not only save time but also make your server much more secure. From here, you can explore advanced features like SSH port forwarding, restricting users, and integrating with tools like Ansible for automation.
Thank you so much for taking the time to read my blog! Your support and engagement truly mean a lot and inspire me to keep creating and sharing more valuable content. If you enjoyed this post, I’d love to hear your thoughts—feel free to leave a comment in the box below and join the conversation. And if you’d like to stay updated with the latest posts, tips, and insights, don’t forget to subscribe to my newsletter. By joining, you’ll be the first to know when new content is published, so you never miss an update.
