Ubuntu Linux: Start / Stop / Restart / Reload OpenSSH Server

I need to provide a remote access to my Ubuntu Linux based server. How do I start / stop OR restart the ssh server under Ubuntu Linux operating system using command line options?

You need to run a script called /etc/init.d/ssh to stop, start, and restart the OpenSSH server. You can also use the service command to control a System V init script. If you are using the latest version of Ubuntu such as 12.04 LTS or 13.04+, you need to use upstart job based commands as listed below.

Start / Stop / Restart / Reload OpenSSH Server on Ubuntu

Latest version of Ubuntu such as Ubuntu Linux 16.04 LTS or 18.04 LTS and others use the systemctl command to control ssh server on Ubuntu. Older version use the service command. To find out your Ubuntu Linux version, type:
lsb_release -a
cat /etc/*release*

Sample outputs:

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 17.10
Release:	17.10
Codename:	artful

Ubuntu Linux: Start OpenSSH Server

Type the following command:
$ sudo /etc/init.d/ssh start
OR
$ sudo service ssh start
OR for systemd based Ubuntu Linux 16.04 LTS or above server:
$ sudo systemctl start ssh

Ubuntu Linux: Stop OpenSSH server

Type the following command:
$ sudo /etc/init.d/ssh stop
OR
$ sudo service ssh stop
OR for systemd based Ubuntu Linux 16.04 LTS or above server:
$ sudo systemctl stop ssh

Ubuntu Linux: Restart OpenSSH server

Type the following command:
$ sudo /etc/init.d/ssh restart
OR
$ sudo service ssh restart
OR for systemd based Ubuntu Linux 16.04 LTS or above server:
$ sudo systemctl restart ssh

systemctl command in action on Ubuntu Linux desktop

Ubuntu Linux: See status of OpenSSH server

Type the following command:
$ sudo /etc/init.d/ssh status
OR
$ sudo service ssh status
OR for systemd based Ubuntu Linux 16.04 LTS or above server:
$ sudo systemctl status ssh
Sample outputs:

? ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-08-30 01:25:08 UTC; 1h 14min ago
  Process: 987 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 672 (sshd)
    Tasks: 13 (limit: 4915)
   CGroup: /system.slice/ssh.service
           ??672 /usr/sbin/sshd -D
           ??785 sshd: veryv [priv]
           ??791 sshd: [email protected]/0
           ??792 -bash
           ??797 sudo -i
           ??798 -bash
           ??950 sshd: vivek [priv]
           ??956 sshd: [email protected]/1
           ??957 -bash
           ??968 sudo -i
           ??969 -bash
           ??992 systemctl status ssh
           ??993 systemctl status ssh

Aug 30 02:31:30 gce-usa-debian-9-backup-1 sshd[950]: pam_unix(sshd:session): session opened for user vivek by (uid=0)
Aug 30 02:31:41 gce-usa-debian-9-backup-1 sudo[962]:    vivek : TTY=pts/1 ; PWD=/home/vivek ; USER=root ; COMMAND=/bin/bash
Aug 30 02:31:41 gce-usa-debian-9-backup-1 sudo[962]: pam_unix(sudo:session): session opened for user root by vivek(uid=0)
Aug 30 02:32:56 gce-usa-debian-9-backup-1 sudo[968]:    vivek : TTY=pts/1 ; PWD=/home/vivek ; USER=root ; COMMAND=/bin/bash
Aug 30 02:32:56 gce-usa-debian-9-backup-1 sudo[968]: pam_unix(sudo:session): session opened for user root by vivek(uid=0)
Aug 30 02:37:25 gce-usa-debian-9-backup-1 systemd[1]: Reloading OpenBSD Secure Shell server.
Aug 30 02:37:25 gce-usa-debian-9-backup-1 sshd[672]: Received SIGHUP; restarting.
Aug 30 02:37:25 gce-usa-debian-9-backup-1 systemd[1]: Reloaded OpenBSD Secure Shell server.
Aug 30 02:37:25 gce-usa-debian-9-backup-1 sshd[672]: Server listening on 0.0.0.0 port 22.
Aug 30 02:37:25 gce-usa-debian-9-backup-1 sshd[672]: Server listening on :: port 22.

Controlling sshd using upstart based commands (this syntax is no longer works with the latest version of Ubuntu)

Since the script /etc/init.d/ssh has been converted to an Upstart based job, try the following commands to start / stop / restart the OpenSSH server:

Stop/Start/Restart the OpenSSH using the following commands

The syntax is:

sudo stop ssh
sudo start ssh 
sudo restart ssh
sudo status ssh

sudo stop ssh
sudo start ssh
sudo restart ssh
sudo status ssh

Sample outputs:

Fig.01: Ubuntu using initctl (upstart) to control sshd process

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.