0

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?

Rothrock
  • 465

1 Answers1

2

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:

https://stackoverflow.com/questions/10310299/proper-way-to-sudo-over-ssh?noredirect=1&lq=1

https://stackoverflow.com/a/4627834

https://stackoverflow.com/questions/39066852/how-to-use-sudo-programmatically-via-a-pipe?noredirect=1&lq=1

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.