Troubleshooting SSH connection termination after `sshd_config` changes

Steps to verify and resolve the issue:

  1. Check if SSH server (sshd) is installed

    1
    dpkg -l | grep openssh-server

    If not installed:

    1
    sudo apt-get install openssh-server
  2. Check if the SSH service is active
    Identify the SSH service and verify its status:

    1
    sudo systemctl status ssh.service

    If not running:

    1
    2
    sudo systemctl start ssh.service
    sudo systemctl enable ssh.service

    If the service name differs:

    1
    systemctl list-units --type=service | grep ssh
  3. Validate Changes to sshd_config
    Test for syntax errors in the configuration:

    1
    sudo sshd -t

    Correct any errors found in /etc/ssh/sshd_config.

  4. Prevent system suspend by disabling suspend mode.
    Temporarily disable suspend mode:

    1
    sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

    To make this change persistent, edit /etc/systemd/logind.conf:

    1
    sudo nano /etc/systemd/logind.conf

    Add or modify the following lines:

    1
    2
    3
    4
    HandleLidSwitch=ignore
    HandleSuspendKey=ignore
    HandleLidSwitchDocked=ignore
    IdleAction=ignore

    !!! IMPORTANT
    `Do not mixed up configuration if you did any manipulations with preventing machine be sleeping when ‘lid’ is closed.’

    Reload the configuration:

    1
    sudosystemctlrestartsystemd−logindz
  5. Confirm SSH Usage

    Check for active SSH sessions:

    1
    who∣grepssh

    Verify the SSH port is open:

    1
    sudonetstat−tuln∣grep:22

    If using a custom port, update firewall rules:

    1
    sudoufwallow<customport>/tcpsudoufwreload
  6. Custom scrtipt for preventing suspend mode from /usr/lib/systemd/system-sleep/

    1. Create script fielssh_keep_awake.sh

      1
      sudo nano /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
    2. Add there script for getting ip of connected via SSH remote machine

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      #!/bin/bash
      # add some sleep delay to manage execution of script.
      sleep 1
      echo "Checking for active SSH sessions..."

      # Run the 'w' command and store the output
      w_output=$(w -h)
      echo "w command output: $w_output"

      # Extract the IP addresses of users logged in via SSH
      ip=$(echo "$w_output" | grep -oP '(\d{1,3}\.){3}\d{1,3}')

      echo "Detected IP: $ip"

      if [ -z "$ip" ]; then
      echo "No active SSH session detected."
      exit 0
      else
      echo "User is still logged in from $ip"
      exit 1
      fi
    3. Check the script location and permissions:

      1
      sudo ls -l /usr/lib/systemd/system-sleep/ssh_keep_awake.sh

      It should show something like:

      1
      -rwxr-xr-x 1 root root 123 Dec 19 14:00 /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
    4. Grant access to the file

      1
      sudo chmod +x /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
    5. For checking active SSH session on remote Linux machine execute

      1
      w -h 

      Console should show exisintg session for all users.

    6. Run and check script execution via

      1
      sudo /usr/lib/systemd/system-sleep/ssh_keep_awake.sh

      Expected Output:

      • The output should print the raw result of w -h and then show the detected IP address.
      • If there is an active SSH session, it should print “User is still logged in from “.
      • If no session is found, it should print “No active SSH session detected.”.
  7. Custom scrtipt for preventing suspend mode from /etc/pm/sleep.d/

    1. Create script fielssh_keep_awake.sh

      1
      sudo nano /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
    2. Add there script for getting ip of connected via SSH remote machine

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      #!/bin/bash
      sleep 1
      echo "Checking for active SSH sessions..."

      # Run the 'w' command and store the output
      w_output=$(w -h)
      echo "w command output: $w_output"

      # Extract the IP addresses of users logged in via SSH
      ip=$(echo "$w_output" | grep -oP '(\d{1,3}\.){3}\d{1,3}')

      echo "Detected IP: $ip"

      if [ -z "$ip" ]; then
      echo "No active SSH session detected."
      exit 0
      else
      echo "User is still logged in from $ip"
      exit 1
      fi
    3. Check the script location and permissions:

      1
      sudo ls -l /etc/pm/sleep.d/ssh_keep_awake.sh

      It should show something like:

      1
      -rwxr-xr-x 1 root root 123 Dec 19 14:00 /etc/pm/sleep.d/ssh_keep_awake.sh
    4. Grant access to the file

      1
      sudo chmod +x /etc/pm/sleep.d/ssh_keep_awake.sh
    5. For checking active SSH session on remote Linux machine execute

      1
      w -h 

      Console should show exisintg session for all users.

    6. Run and check script execution via

      1
      sudo /etc/pm/sleep.d/ssh_keep_awake.sh

      Expected Output:

      • The output should print the raw result of w -h and then show the detected IP address.
      • If there is an active SSH session, it should print “User is still logged in from “.
      • If no session is found, it should print “No active SSH session detected.”.