Install SSHD on Ubuntu: Enable and Start Are Two Commands

Updated

Terminal window linked by a dotted line to a server panel with a padlock, for installing the SSH daemon on Ubuntu

sudo apt install openssh-server, then sudo systemctl enable ssh and sudo systemctl start ssh — two separate commands: enable makes the service start on boot, start starts it now, and you need both the first time. After any config edit, run sudo sshd -t to syntax-check sshd_config before you restart.

Running them as one command does not work:

sudo systemctl enable ssh sudo systemctl start ssh

That’s two commands concatenated onto one line with no separator. systemctl doesn’t know where the first command ends — it reads every word after enable as one argument list. Standing in a shell function for systemctl to show exactly what it receives (no real systemd involved, this runs on any machine):

$ systemctl(){ echo "argc=$# args=[$*]"; }
$ sudo systemctl enable ssh sudo systemctl start ssh
argc=6 args=[enable ssh sudo systemctl start ssh]

One call, six arguments, none of which start the service. Two commands need two lines (or a ; or && between them):

sudo systemctl enable ssh
sudo systemctl start ssh

enable makes the service start on boot; start starts it now. You need both the first time. The rest of the install, cited from Ubuntu/OpenSSH documentation rather than run on Linux:

sudo apt update
sudo apt install openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh
sudo systemctl status ssh

Check sshd_config before you restart, not after

sshd -t -f <path> validates a config file’s syntax without starting anything, and it works the same on any OpenSSH build, including the one already on macOS. Demonstrated there against a scratch config and a disposable key — never the system’s real ones:

$ ssh-keygen -t ed25519 -f ./scratch_host_key -N "" -q
$ cat > scratch_sshd_config <<EOF
Port 2222
HostKey $(pwd)/scratch_host_key
PermitRootLogin no
PasswordAuthentication no
EOF
$ sshd -t -f ./scratch_sshd_config
$ echo "exit: $?"
exit: 0

And with a typo’d directive value, so you can see it actually checks something:

$ sed -i.bak 's/PermitRootLogin no/PermitRootLogin maybe/' scratch_sshd_config
$ sshd -t -f ./scratch_sshd_config
./scratch_sshd_config line 3: Bad yes/no argument: maybe
$ echo "exit: $?"
1

Run sudo sshd -t (no -f, checks the real /etc/ssh/sshd_config) after every edit and before every systemctl restart ssh. A syntax error in that file otherwise surfaces as “the service won’t restart,” discovered after you’ve already lost the connection you were editing it over.

No root is needed for the shell-function demo or the two sshd -t -f runs, so install-sshd-service-ubuntu.sh repeats them on any machine with bash and OpenSSH and never touches /etc/ssh; the apt and systemctl steps are not part of it.

The two settings that actually matter

Changing PermitRootLogin yes to PermitRootLogin no is correct — keep it. The other half of hardening default SSH access is disabling password authentication once key-based login is confirmed working: set PasswordAuthentication no and add your public key to ~/.ssh/authorized_keys first. Test the key-based login in a second terminal before restarting with passwords off — if the key doesn’t work, you want your first session still open to fix it, not a locked door.

Don’t disable host key checking (StrictHostKeyChecking no) to work around a “host key changed” warning — that warning exists specifically to catch a server impersonation or a man-in-the-middle attempt.

For managing an existing sshd install that already has this backwards — enabling something the docs call “restart” when it isn’t — see restarting sshd on Solaris, which covers the equivalent SMF commands on a non-systemd system.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.