313

Is there a terminal command that will log you out of your current desktop session and take you back to the login screen?

TheXed
  • 26,536

15 Answers15

238

11.10 and above

gnome-session-quit

11.04 and previous versions

dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1

(via alt textDoR, see his answer to "Reboot without sudoer privileges?" for more dbus goodness!)

or alternatively, you can use

gnome-session-save --force-logout

--force-logout in contrast to just --logout will not ask the user to deal with unsaved documents and so on.

is this the easiest way? no simple one line command like sudo logout?? I will never remember all that.

Yes, there is a command called logout, but it concerns the Terminal. gnome-session-save is the program that actually quits the gnome-session, which you can of course kill, but that wouldn't qualify as logging out. :-)

Notice as well that these commands don't require you to be root.

You can always add an alias to your system if you want to have a shorter command.

Open ~/.bash_aliases with a text editor, or create it if it isn't there, and add something like this to it:

alias logout-gnome="gnome-session-save --force-logout"

(.bashrc is a script that is run every time a new virtual terminal is started up, you should set up all your permanent aliases there, see also: How to create a permanent "alias"?)

  • 2
    Well, there's always Ctrl-Alt-Backspace (if you enable it), but again that's killing the session rather than logging out from it. – koanhead Dec 02 '10 at 03:04
  • 1
    You might want to add DISPLAY=:0.0 or similar when trying to log out of a session from ssh or another terminal. – patrikf Mar 11 '13 at 16:31
  • 3
    It seems to me that "--force-logout" doesn't quite work, as it gives this error: WARNING **: Unable to start: Unknown option --force-logout. After digging around with man gnome-session-quit, I found that "--force" and "--logout" are actually meant to be different arguments. Therefore, "--force --logout" (two minus sings before "force" and "logout", with a space between) would be better as you won't have errors. At least, that's the case with Ubuntu 13.04 and Linux Mint 15. IDK about other/older versions of either distro. – TSJNachos117 Jun 13 '13 at 03:19
  • aaronfranke@aaron-xub16desk$ dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1 Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files – Aaron Franke Apr 18 '17 at 21:42
  • 1
    To state the (non-)obvious: for Ubuntu MATE-users, that's mate-session-save --force-logout – Frank N Mar 23 '18 at 12:33
  • Thanks. gnome-session-quit works fine in Ubuntu 18. – Gabriel Staples Mar 03 '19 at 17:44
117

I had installed mate desktop and none of the menus worked, even the f-keys didn't work. I managed to get xterm through browsing with file manager.

The only thing that worked was :

sudo pkill -u username
Jorge Castro
  • 71,754
Dan
  • 1,171
  • 2
    I also did an alias out of it for me alias ulogout="sudo pkill -KILL -u $1". Might come in handy for someone. Usage: ulogout user_name – Eugene Jan 17 '13 at 09:49
  • 4
    @Eugene How about alias logout="sudo pkill -u ${USER}"? – Georgi-it Jan 04 '16 at 09:18
  • This is a great solution for VM's like in Virtualbox. – Phil Lawlor Aug 23 '17 at 13:34
  • 2
    Is there a reason you need the sudo? Also, I think this is different than logging out, since it will kill all of your processes (for instance, if you have another remote session, it will also be killed). I'm also not sure that it will necessarily kill all of your processes (I can imagine it might kill itself first?) Do you know if that is possible? – capybaralet Oct 20 '17 at 18:38
  • 1
    For mate, use mate-session-save --force-logout. See this answer. – Jérôme Oct 05 '18 at 07:50
  • Great tip! This is the only method that works if Unity didn't startup properly. And as mentioned, you don't need the sudo part. – Stand with Gaza Jun 03 '19 at 13:18
  • I was running another ubuntu 22 LTS in virtual box & one ISO download via torrent in brave browser, And system froze, couldn't logout to shutdown via UI options, the 'genome-session-quit' also didn't help, But your suggestion helped, Thanks a lot :) – Aravinth Jun 29 '23 at 06:37
34

11.10 and above

Here's my personal solution!

In the terminal, run:

gedit ~/.bash_aliases

And add:

alias log-out="gnome-session-quit"

to the file! Now you just have to run log-out!

Nick
  • 341
15

For gnome sessions, gnome-session-quit works well. By default it asks for confirmation and then logs you out (i.e., the --logout argument is assumed unless overridden with --power-off explicitly). You can also tell the command to not prompt for confirmation on logout:

--no-prompt
      End the session without user interaction. This only works with --logout.

gnome-session-quit is still valid in 12.04.

Since the OP didn't specify the window/desktop manager, and the gnome-session-quit might not work with all possibilities, here's a generic X-windows way to return to the login screen or chooser which I've used several times over the last several releases:

From a terminal (invoked with, variously, 'Alt-F2 + xterm, or Ctrl-T, or Crt-Alt-F1, or by right-clicking on the desktop in Nautilus and using the "open in terminal" shortcut, etc.) type

sudo pkill X

This is generally overkill for most situations. I tend to use it when I've got a hung process or when I've got to log out quickly. When I've done this, Ubuntu has politely returned me to the login screen by re-spawning X-windows.

I've not yet used the power-off feature of the gnome-session-quit, as I typically use shutdown -P now for that purpose.

belacqua
  • 23,120
15

Looks like gnome-session-save was renamed to gnome-session-quit for 11.10. Everything else in the main answer should still work with that single change.

http://mail.gnome.org/archives/desktop-devel-list/2011-February/msg00147.html

Jorge Castro
  • 71,754
Mark Ferree
  • 281
  • 2
  • 4
12

On Xubuntu (or whenever using an Xfce session), you can logout via the terminal by using

xfce4-session-logout --logout

You must specify --logout or the logout confirmation screen will be displayed. This will work whether you have specified 'Xubuntu session' or 'Xfce session' when you logged in with Lightdm, as the session is still managed by the Xfce session manager.

In addition, another option will log you out as well:

xfce4-session-logout --fast  

If this command is issued the session is not saved, so it is normally best to use xfce4-session-logout --logout.

The ability to logout using the terminal will be very useful to you, as will the other options such as suspend that are available with xfce4-session-logout.

For more information, see man xfce4-session-logout or the Ubuntu manpages online.

10

You can use below command:

sudo service lightdm restart
9

For Ubuntu Mate use mate-session-save --force-logout. It is like gnome-session-quit.

heemayl
  • 91,753
7

I tried gnome-session-quit --force and it works!

Note: I use ubuntu 15.04

guntbert
  • 13,134
5

lubuntu 12.10 LXDE running on a Samsung netbook (CPU: 1.66 GHZ Intel Atom; RAM: 2GB)

The below two commands logged me out immediately:

sudo pkill -u username
sudo service lightdm restart
Eric Carvalho
  • 54,385
afc888ny
  • 103
  • 2
  • 7
5

In case you don't want to kill your session, you can go back to the login screen using:

dm-tool switch-to-greeter

If you select your user and enter your password, the session will be resumed, so it's not a full logout.

Zanna
  • 70,465
2

And in kubuntu 12.04+ it is:

qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout 0 0 0
Adobe
  • 3,891
2

Define a function by adding the following line to your .bashrc, then invoke by typing logoff at the shell prompt.

  logoff() { gnome-session-quit --logout --no-prompt ; } # Log Out
  • With Ubuntu 23.10 I use logout() { gnome-session-quit --no-prompt ; } without the --logout option. This way applications ask to save documents before exit and browser don't ask to recover when logged back in. Defining an alias also works: alias logout-gnome="gnome-session-quit --no-prompt" – Mauricio Feb 25 '24 at 18:21
0

There is a way which will work whether you are running X or you are in a TTY. There you go:

  • If you use lightdm

    sudo service lightdm restart
    
  • if you use gdm or in Ubuntu GNOME

    sudo service gdm restart
    
  • If you use kdm or in Kubuntu

    sudo service kdm restart
    

The downside of this command is that it will ask for your password.

muru
  • 197,895
  • 55
  • 485
  • 740
user258456
  • 27
  • 4
-1

I found this accidentally.

luvpreet@DHARI-Inspiron-3542:~/go/bin$ who -uH
NAME     LINE         TIME             IDLE          PID COMMENT
luvpreet tty7         2017-12-27 17:33  old        27458 (:0)

Here you get the process id of your current session.

So, easy. Kill it

luvpreet@DHARI-Inspiron-3542:~$ kill 27458

There you go.

luv.preet
  • 5,787