Steps to verify and resolve the issue:
Check if SSH server (
sshd
) is installed1
dpkg -l | grep openssh-server
If not installed:
1
sudo apt-get install openssh-server
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
2sudo systemctl start ssh.service
sudo systemctl enable ssh.serviceIf the service name differs:
1
systemctl list-units --type=service | grep ssh
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
.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
4HandleLidSwitch=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
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
Custom scrtipt for preventing suspend mode from /usr/lib/systemd/system-sleep/
Create script fiel
ssh_keep_awake.sh
1
sudo nano /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
Add there script for getting
ip
of connected via SSH remote machine1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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
fiCheck 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
Grant access to the file
1
sudo chmod +x /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
For checking active SSH session on remote Linux machine execute
1
w -h
Console should show exisintg session for all users.
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.”.
Custom scrtipt for preventing suspend mode from /etc/pm/sleep.d/
Create script fiel
ssh_keep_awake.sh
1
sudo nano /usr/lib/systemd/system-sleep/ssh_keep_awake.sh
Add there script for getting
ip
of connected via SSH remote machine1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
fiCheck 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
Grant access to the file
1
sudo chmod +x /etc/pm/sleep.d/ssh_keep_awake.sh
For checking active SSH session on remote Linux machine execute
1
w -h
Console should show exisintg session for all users.
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.”.