57

I chose an Openbox DE at the time of login and the system took ages to load the DE. So I switched to CLI (Ctrl+Alt+F1) and rebooted my system (but I wanted to logout from the GUI and not restart the whole system).

My question is, can I issue some command at CLI to log me out from the GUI so that I can select different DE. (I don't want to restart my system every-time DE hangs.)


$ DISPLAY=:0 gnome-session-quit --force

** (gnome-session-quit:3144): WARNING **: Failed to call logout: The name org.gnome.SessionManager was not provided by any .service files

13 Answers 13

63

To end all user processes and be sent back to the login screen, you can use:

kill -9 -1

Don't run it as root though, for reasons discussed here.

3
  • 4
    Works great, but why? In special, why does LightDM restart after you killed everything except for init? Jul 7, 2013 at 12:41
  • 2
    @CiroSantilli巴拿馬文件六四事件法轮功 Because it forcefully KILLS (9) EVERYTHING it is allowed to. Which means every process owned by you. Using SIGKILL to terminate apps is absolutely not recommendable! Use SIGTERM (15). Applications can react upon this signal and do cleanup. May 4, 2016 at 14:27
  • 1
    DON'T DO THIS - unless you're sure you have NO other processes running under your user ID. This method will kill everything running as the executing user, including active cron jobs, remoted-in SSH connections, long-running background jobs, etc, etc. Very dangerous approach, even for non root. (and don't start with 9, start with SIGHUP, then SIGTERM, then SIGKILL) Sep 1, 2021 at 11:46
31

This can be done using the gnome-session-quit command. It needs the --force option to suppress the confirmation dialog that would appear without it.

Unlike applications run from an X terminal emulator, ending a session from a TTY requires you to append the DISPLAY variable to indicate which X display is running the session. Hence:

DISPLAY=:0 gnome-session-quit --force

assuming that you are running GNOME on :0, which is the case in normal situations.

  • In Ubuntu 12.04LTS running GNOME, the command

    "DISPLAY=:0 gnome-session-quit --logout --no-prompt" 
    

    works. The "--force" argument doesn't exist in the current update level]

5
  • thanks, I ran the command but i got some error. I have edited my post to include the error. Please share if I have done something wrong.
    – Ankit
    Aug 26, 2012 at 17:11
  • My bad, I did not notice you mentioned you are using openbox. Unfortunately, this command will only work with a standard Ubuntu installation (Unity/GNOME). As an alternative, you can completely shut down the GUI and thereby your session by running sudo service lightdm stop. edit: what desktop environment are you using? Openbox is just a window manager.
    – user77111
    Aug 26, 2012 at 18:36
  • i am trying to use kde/openbox or gnome/openbox.
    – Ankit
    Aug 27, 2012 at 2:42
  • Doesn't work if your terminal isn't part of the same dbus session as the gnome-session. How do you get into another dbus session?
    – Zan Lynx
    Feb 14, 2013 at 21:19
  • Is this the command that gets executed when clicking "Log Out..." via the GUI ?
    – Dor
    Sep 29, 2016 at 23:23
18

In modern systemd Linux distros, the answers are all a little too complicated. The solution is one tool: loginctl.

In a good shell you even have autocompletion, so make use of Tab to see the options and parameters and it is quite intuitive. The command to search for is kill-session.

If you tab, you'll notice each session has an ID, but in my case it also showed the username and TTY (that is the Ctrl+Alt+number you type) and the seat.

Here is how it looks to me, e.g.:

$ loginctl kill-session 10  
10  -- 1000 rugk seat0 tty2

You can tab through the sessions to find the correct one.

Otherwise, if that does not work you can find the session ID by running loginctl list-sessions or just loginctl. You get something like this:

$ loginctl list-sessions                            
SESSION  UID USER SEAT  TTY 
    10 1000 rugk seat0 tty2

I guess it's quite obvious the first column contains the session ID you need to pass to loginctl kill-session.

This works very well if the GUI hangs and you need to force-kill it, which seems to be your use case.

If you want that to be explained in a more elaborate way here is how you can kill your own session if the GUI is not responding or you cannot use your keyboard.

2
  • 2
    This is way the most useful answer, since loginctl workings are independent from the desktop environment, and allow you to terminate even other users sessions, asking sudo password if needed.
    – gerlos
    Mar 24, 2021 at 18:42
  • Highly underrated answer. Took me 8 pages of answers to land here! Jun 12, 2023 at 14:46
11

Please follow takkat's suggestion. The standard is Ctrl+Alt+Backspace.

You can also run:

$ sudo service lightdm restart
0
5

As an alternative, you can terminate user sessions using the following, works well to log out users except for the root user- when doing maintenance for example.

loginctl | egrep -v "root|SESSION|listed" | awk '{print $1}' | xargs loginctl terminate-session
3

Another way,

sudo pkill -u NameOftheUser

or

sudo pkill x

which kill all users.

2
  • That's a bit harsh, don't you think?
    – acolyte
    Apr 3, 2013 at 17:08
  • Harsh? It's extremely silly.
    – RichieHH
    Jan 6, 2014 at 15:36
2

The real problem is that the DBUS session variables must be set and match the session you're trying to control.

I've created this script that does set the DBUS session variables from the gnome-session environment in case you want to logout other users/sessions:

How to restart Gnome-Shell from command line?

function logout() {
    local USERNAME
    export USERNAMES=( ) 
    while [ -n "$1" ]; do case "$1" in
        -* ) break ;;
        *) USERNAMES+=( "$1" ); shift ;;
    esac; done

    for USERNAME in "${USERNAMES[@]}"; do
        local SESSION_PID=$(pgrep -fu "$USERNAME" gnome-session|head -1)
        if [ -n "$SESSION_PID" ]; then
            (
                sudo -u "$USERNAME" cat "/proc/$SESSION_PID/environ" | xargs -0 -n 1 echo export
                echo "gnome-session-quit --logout $@"
            ) | sudo -u "$USERNAME" sh -;
        fi
    done
}
1
  • Using --logout --no-prompt or --force instead of just --logout is a better idea, otherwise a logout prompt will be shown.
    – gioele
    Apr 8, 2018 at 17:21
1

If you are in a xubuntu session or similar, must use xfce4-session-logout insted of gnome session commands, that's why you see warnings

1

This is what works best for me (with xfce, lightdm and ssh):

$ DISPLAY=:0.0 xfce4-session-logout --logout

Or

$ sudo service lightdm restart
0

If you are using Openbox then you can use openbox --exit which will exit the Openbox session and go back to the login manager, in my case LightDM.

0

This command will log out whichout showing logout prompt and take you to the log in window gnome-session-quit --no-prompt
To power off you can use this command this will open a dialog box to conform power off
gnome-session-quit --power-off
You can get rid of dialog box using --no-prompt

0

Super old thread, and I am new, so cannot comment. I would add:

loginctl kill-user `whoami`

This removes the rigamarole of figuring out user session ids and such.

-2

You can use the command killall gnome-session to log out. This will work for all GNOME sessions and if I remember correctly all GNOME-related ones. It takes you right back to LightDM so you can select a new DE or a new user. :)

2
  • 2
    Not the best way to logout. Check askubuntu.com/questions/69114/…
    – jokerdino
    Aug 26, 2012 at 14:25
  • Never seen this option before. I probably should start using this. I'm supposing that killall gnome-session is a forceful way to close it. Aug 26, 2012 at 14:26

You must log in to answer this question.

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