Ubuntu Linux: Start / Stop / Restart / Reload OpenSSH ( SSH ) Server

Posted on in Categories , , last updated August 30, 2017

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.

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

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: veryv@pts/0
           ??792 -bash
           ??797 sudo -i
           ??798 -bash
           ??950 sshd: vivek [priv]
           ??956 sshd: vivek@pts/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

Sample outputs:

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

7 comment

Leave a Comment