Problem: SSH not working Ubuntu 22.04 VM running in VirtualBox
Virtualbox Ubuntu SSH connection refused – Ubuntu 22.04
Solution: install OpenSSH server
When you install the Ubuntu Desktop version in your VirtualBox VM and try to ssh, you may see this issue, because the Ubuntu Desktop version does not install an SSH server by default. Here are the steps to install and configure the SSH server in Ubuntu 20.04 Desktop (or in Server if required)
From a high-level view, we will be doing:
- Install ssh software package, (OpenSSH server)
- Enable the service and start even after rebooting the VM
- Start your SSH service
- Verify SSH service in Ubuntu, from a terminal (command line)
Installing SSH in Ubuntu 22.04
Open your Ubuntu Terminal and run the command,
sudo apt-get install openssh-server
Example:
ubuntu@codetryout:~$ sudo apt-get install openssh-server
...
Reading package lists... Done
Building dependency tree
...
Do you want to continue? [Y/n]
- Provide your password, if prompted.
- Give Y to continue with the installation steps.
OpenSSH – Enabling and starting the SSH Service
After installation, you need to make sure the ssh service has been enabled and started.
Enabling the service, so that the OpenSSH server will be started automatically after Ubuntu reboot.
ubuntu@codetryout:~$ sudo systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
For now, just after the installation, you have to start the SSH service. (Or reboot the system)
ubuntu@codetryout:~$ sudo systemctl start ssh
ubuntu@codetryout:~$
How to verify SSH service status in Ubuntu
ubuntu@codetryout:~$ sudo systemctl status ssh
ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since xxxxx xxxxx 16:20:29 IST; 10min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 2453 (sshd)
Tasks: 1 (limit: 2312)
Memory: 1.0M
CGroup: /system.slice/ssh.service
└─2453 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
Jan 04 16:20:29 codetryout systemd[1]: Starting OpenBSD Secure Shell server...
Jan 04 16:20:29 codetryout sshd[2453]: Server listening on 0.0.0.0 port 22.
Jan 04 16:20:29 codetryout sshd[2453]: Server listening on :: port 22.
Jan 04 16:20:29 codetryout systemd[1]: Started OpenBSD Secure Shell server.
ubuntu@codetryout:~$
FAQs
Error – Ubuntu Unable to start the ssh service
Please refer to this guide to fix this error
The takeaway
This is a Step-by-step instruction for installing OpenSSH (SSH server) in Ubuntu and or in Ubuntu Virtual Machine and Docker containers, this is suitable for your test/development environments. Additional advanced steps must be followed for production systems to secure the SSH server.
Related articles: Virtual Machine, Ubuntu, Docker