Table of Contents

Start and Stop SSH Server in Ubuntu Linux

Source: https://www.cyberciti.biz/faq/howto-start-stop-ssh-server/

Start

sudo systemctl start ssh

Stop

sudo systemctl stop ssh

Restart

sudo systemctl restart ssh

Status

sudo systemctl status ssh

Helper Scripts

Bash

ssh_server_control
#!/usr/bin/bash
 
if [ $# -lt 1 ]; then
    echo "Usage: $0 arg1"
    echo "  where arg1 is one of:"
    echo "    start"
    echo "    stop"
    echo "    status"
    exit 1
fi
 
if [ "$1" == "status" ]; then
    sudo systemctl status ssh
fi
 
if [ "$1" == "start" ]; then
    sudo systemctl start ssh
fi
 
if [ "$1" == "stop" ]; then
    sudo systemctl stop ssh
fi

Python

ssh_server_control.py
#!/usr/bin/env python3
 
import argparse
import shlex
import subprocess
 
 
def run_process(process_name):
    """
    Run the given process.
    """
    return subprocess.run(shlex.split(process_name))
 
 
if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        prog="ssh_server_control",
        description="Start, stop, or show the status of the SSH server.",
    )
    _ = parser.add_argument(
        "--start", action="store_true", default=False, help="Start the SSH server"
    )
    _ = parser.add_argument(
        "--stop", action="store_true", default=False, help="Stop the SSH server"
    )
    _ = parser.add_argument(
        "--status",
        action="store_true",
        default=False,
        help="Show the status of the SSH server",
    )
    args = parser.parse_args()
 
    if args.start:
        run_process("sudo systemctl start ssh")
 
    if args.stop:
        run_process("sudo systemctl stop ssh")
 
    if args.status:
        run_process("sudo systemctl status ssh")