How can I suspend or hibernate my laptop using command line, without installing additional software?
Traditionally ubuntu supported a fairly blunt method of suspend and hibernate. Neither would integrate well with other apps and sometimes not even work on some machines. This new method doesn't require root and notifies all applications listening for power events.
Starting with Ubuntu 16.04,
systemctl call must be used (See Suspend command in Ubuntu 16.04)
New Method (obsolete)
Obsolete circa Ubuntu 16.04; use
systemctl instead, as above.
See the answer here on this page from Adam Paetznick regarding the use of dbus. Ideally you would create a
~/bin/suspend shortcut/script that makes the use of this action easy.
For use over ssh, you should modify policykit rules as outlined by Peter V. Mørch
According to the Ubuntu Forum you can use the following commands:
pmi action suspend
pmi action hibernate
This requires that you install the powermanagement-interface package (not tested).
sudo apt-get install powermanagement-interface
I have also found the commands
sudo pm-suspend and
sudo pm-hibernate to work on my netbook.
The gnome-friendly way is to use dbus.
dbus-send --system --print-reply \ --dest="org.freedesktop.UPower" \ /org/freedesktop/UPower \ org.freedesktop.UPower.Suspend
There are two advantages to this command over
It will lock your screen (upon resume) if you have that option selected in gnome.
It does not require root privilege, so it is easy to add it as a keyboard shortcut, for example.
As mentioned in the comments exchanging the
Suspend in the last line to
Hibernate creates a hibernate command:
dbus-send --system --print-reply \ --dest="org.freedesktop.UPower" \ /org/freedesktop/UPower \ org.freedesktop.UPower.Hibernate
If the hibernation throws
Error org.freedesktop.UPower.GeneralError: not authorized your user might not be allowed to hibernate. Edit or create
/etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla so it contains the following section: (source)
[Re-enable hibernate by default] Identity=unix-user:* Action=org.freedesktop.upower.hibernate ResultActive=yes
This was tested on UbuntuGnome 14.04.
Note: This is basically the same as qbi's answer, but updated to work for newer versions of Ubuntu as well as including hibernate.
If you want your computer to suspend in one hour because you want to go to bed listening to your favorite radio station, open the terminal and type:
sudo bash -c "sleep 1h; pm-suspend"
and your computer will fall asleep in 1 hour. When you awake, it will have kept your open images and all your stuff.
You can replace
1h by what you want:
h for hours,
m for minutes,
s for seconds,
d for days.
You can use the file
/sys/power/state to do this. First find out what states are supported:
user@linux:_> cat /sys/power/state standby mem disk root@linux:~> echo -n mem > /sys/power/state # suspend to ram root@linux:~> echo -n disk > /sys/power/state # suspend to disk
or via dbus:
According to this entry in launchpad the above interface was removed. So it would not work anymore in Ubuntu.
To suspend a system (14.04) from the command line (or keyboard shortcut) use:
dbus-send --system --print-reply --dest="org.freedesktop.login1" /org/freedesktop/login1 org.freedesktop.login1.Manager.Suspend boolean:true
I found this out by playing around with gdbus which can list the interfaces available:
To list the services available on the bus:
dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames
To find the methods:
gdbus introspect --system --dest org.freedesktop.login1 --object-path /org/freedesktop/login1 --recurse
Adam Paetznick's dbus-send answer didn't work as purported for me on lucid; the machine woke up unlocked, even though the gnome-power-manager is set to lock the screen on wake-up. I want the screen to be locked at wake-up, and found that the following does that:
$ gnome-screensaver-command --lock && pmi action hibernate
I imagine this does not depend on the gnome configuration, but I haven't tested that.
Here's how to put a remote machine in standby over ssh:
ssh -t 192.168.1.4 'sudo nohup &>/dev/null bash -c "(sleep 1; echo -n mem >/sys/power/state) &"' email@example.com's password: [sudo] password for x: Connection to 192.168.1.4 closed.
/sys/power/state works in Ubuntu 13.10. pmi gives Dbus error.
Personally, I've been experimenting with the pmi method. However, when I tried this, I got an error message:
Error org.freedesktop.DBus.Error.Spawn.ChildExited: Launch helper exited with unknown return code 1. However, there is a workaround in the 3rd comment of this bug report, which seems to have worked for me (I'm using Ubuntu 13.03).
Update for those who, like me, still work on KDE/Ubuntu 14.04 systems. To lock use
qdbus, and to suspend use
dbus. Full command:
qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock && dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend
To hibernate, i.e. suspend to harddisk instead of RAM, replace 'Suspend' at the end of the command by 'Hibernate'.
To just lock the screen without suspending,
xscreensaver-command -lock will work, IF you type only 1 hyphen for the '-lock' option, and only if the screensaver is running. Actually not a very useful command. Using
i3lock is easier, but then you will not get a neat login dialog to get back to work, as you will when using
How to lock the screen and put the computer to sleep (suspend) in a 1-liner from the command line (could be assigned to an Ubuntu shortcut key)
Tested on Ubuntu 22.04.
This is directly from my longer answer here. Refer to that answer for a detailed explanation of this command:
You can lock the screen and put the computer to sleep in a single command like this:
# lock the screen and put the computer to sleep sudo true && gnome-screensaver-command -l && sudo pm-suspend
You can assign the above command to a shortcut key if you want to quickly put your computer to sleep.
sudo systemctl suspend, as shown in the main answer, also seems to produce a similar result to my
sudo true && gnome-screensaver-command -l && sudo pm-suspend command. I don't know how they differ or if they have any different effects.
Update: on older versions of Ubuntu, it's possible that
sudo systemctl suspend will not also lock the screen and require a password upon waking up, whereas my
sudo true && gnome-screensaver-command -l && sudo pm-suspend command will always lock the screen first and require a password after wake-up. On Ubuntu 22.04 though, the results seem to be the same. In the Edge Browser only (using any other browser won't show the full content), you can see a chat I had with Bing AI about this here.
After waking up from sleep (suspend), you can run
journalctl -n 1000 -e | grep "PM: suspend" to prove your computer really was asleep. Again, see my longer answer on this for details.
Via the GUI menus:
Running my command above produces the exact same result as clicking the top-right of your screen and going to "Power Off/Log Out" --> Suspend, as shown in this screenshot here: