I have set up a remote machine to use for deep learning — ubuntu 16.04, anaconda 3, jupyter notebook, etc. I've set up openssh with port forwarding, strong keys, no password and that is all working great.

Between sessions, I want to be able to have my machine hibernate/suspend. My understanding is that 16.04 now uses systemd to manage that kind of behavior and that sudo systemctl suspend is the command I should be using to put that to sleep. I've tried that and it works, yeah!

If I put the machine to sleep from my SSH session the terminal on my laptop then locks up because the other end is now asleep. I don't know that this is causing problems, but it feels not good to me.

If I log out of my ssh session first, then I can't suspend the machine because I'm not authorized.

I'd like to create a little script that lets me, logout then suspend the machine.

I've done a little research, most things I'm finding on google are "how do I log out of ssh and keep my session alive" so I haven't quite hit on the right terms. Also I did find some things relating to dbus or startup, but nothing specifically about systemd. Someone I know suggested the following, but it didn't work. It logged me out, but the machine didn't suspend/sleep/hibernate.

logout && sleep 2 && sudo systemctl suspend

I'm not sure if it was because I needed to authenticate as sudo or because the logout killed the short-circuit evaluation.

So I'm looking for a script I could run that will do the following (order doesn't mater to me as long as the whole thing works).

  • make sure I'm authenticated as sudo (if sudo is needed)
  • log out of my ssh session
  • put the machine into suspend

Or I'm thinking maybe there is a way to logout and then run a script on my local machine to suspend?


1 Answer 1


Have you considered ending your ssh session then executing the suspend command over ssh instead of opening a whole new session?

ssh -t user@host sudo systemctl suspend

That should do what you want. Otherwise you could set it up for passwordless sudo but you should carefully consider the security implications of that.

Some sources to look at for ways to set that up:




Edit: Also, if you want some more secure (I believe) way to do it passwordless you could add a sudoers entry for that. For example create a file called suspend-nosudo in directory /etc/sudoers.d with content:

# Allow yourusername user to suspend without sudo
yourusername ALL=(ALL) NOPASSWD: /usr/bin/systemctl suspend

Where yourusername is of course your username. Then if you do

ssh -t user@host sudo systemctl suspend

You get no password prompt.

  • As I typed it up this idea started to come to me, but I'm a total newbie and I'm not sure how to make that work. Can you share a resource or link on how to make that work? Something I found suggested using the -t option. I'm not sure I understand that.
    – Rothrock
    Mar 14, 2017 at 21:12
  • Just the execution or adjusting your sudo settings? They go over executing a command here: cyberciti.biz/faq/unix-linux-execute-command-using-ssh As far as getting sudo to take your password without a terminal session you can check: stackoverflow.com/a/4627834 Mar 14, 2017 at 21:20
  • Yes the -t flag is the ticket. would you edit your answer to ssh user@host -t 'sudo systemctl suspend so I can give it credit? Thanks.
    – Rothrock
    Mar 16, 2017 at 4:26
  • Thanks for following up. I'm glad you got it working. How do you bring it back when you're ready to work again? Wake on LAN? Mar 16, 2017 at 14:51
  • Would you add the -t to the command itself? I'm thinking that would help people in the future who might look at commands, but not read the words. Funny you ask about the bringing it back.. I'll be posting another question about that....
    – Rothrock
    Mar 16, 2017 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .