Как сделать logout ubuntu

Обновлено: 30.06.2024

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.)


177k 45 45 gold badges 423 423 silver badges 659 659 bronze badges 5,841 15 15 gold badges 52 52 silver badges 76 76 bronze badges

12 Answers 12

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

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



2,167 1 1 gold badge 17 17 silver badges 21 21 bronze badges Works great, but why? In special, why does LightDM restart after you killed everything except for init? @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.

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:

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

In Ubuntu 12.04LTS running GNOME, the command

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

110k 51 51 gold badges 301 301 silver badges 464 464 bronze badges 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. 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. Is this the command that gets executed when clicking "Log Out. " via the GUI ?

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.:

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:

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.

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

14k 9 9 gold badges 42 42 silver badges 76 76 bronze badges 24.2k 13 13 gold badges 57 57 silver badges 89 89 bronze badges

16 Answers 16

alt text

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

or alternatively, you can use

--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.

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

(.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"?)

80.5k 42 42 gold badges 203 203 silver badges 225 225 bronze badges You might want to add DISPLAY=:0.0 or similar when trying to log out of a session from ssh or another terminal. 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

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 :

67.6k 123 123 gold badges 453 453 silver badges 649 649 bronze badges 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

Here's my personal solution!

In the terminal, run:

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

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.

67.6k 123 123 gold badges 453 453 silver badges 649 649 bronze badges

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:

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

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.

In Ubuntu 11.04, Ctrl - Alt - Del gives me a menu with shutdown, hibernate, suspend and restart, but no logout. Is there a key combination to logout of Ubuntu? Is there a way to provide one?

52k 41 41 gold badges 141 141 silver badges 193 193 bronze badges


3,883 6 6 gold badges 31 31 silver badges 40 40 bronze badges If future searchers want to know. I can type Ctrl+Alt+L in 12.04 to lock screen.

7 Answers 7

Creating custom keyboard shortcuts:

For 12.04 LTS and later:

To add a new shortcut open System Settings -> Keyboard and choose the Shortcuts tab:

enter image description here

Press the '+' sign on the bottom left to enter the name of your custom shortcut and the command you want to run (e.g. gnome-session-quit --logout --no-prompt ). After having applied this you are able to select the line of your new shortcut. Then press the desired key combination. In case the key combination already exist we will have to delete this first.

For 14.04 LTS and later:

In a default Unity session the new logout dialog is presented needing another at least two keystrokes to select poweroff from the menu:

enter image description here

The old dialog with a 60 s timeout will only be displayed in a gnome-flashback session.

134k 50 50 gold badges 297 297 silver badges 415 415 bronze badges @drevicko The menu entry System Settings calls the Gnome Control Center, so yes, its the same.

In Ubuntu 11.10 you could write a script containing

make it executable with chmod +x script-name , then set a global shortcut to execute this script in Gnome Control Center -> Keyboard.

In previous Ubuntu versions there was gnome-session-save --logout .

85k 10 10 gold badges 158 158 silver badges 172 172 bronze badges In 11.10 the default action on Ctrl+Alt+Del is to logout.

In Ubuntu 1204 there are two out-of-the-box solutions:

"Run a command" technique:

"Indicator menu" technique:

Alt - F10 | arrow right or left to gear icon | arrow down to Log Out.


5,860 4 4 gold badges 31 31 silver badges 44 44 bronze badges

If you are using Unity (the default environment in Ubuntu 11.04 and later) then you can press the Super key (aka the Windows key) to bring up the dash, type in "log" and the first option is "Log Out" so just press Enter . This will bring up a dialog box to confirm whether you want to log out, with the "Log Out" button already selected, so press Enter again. And you're logged out.

18.4k 14 14 gold badges 66 66 silver badges 90 90 bronze badges

Another way is to restart lightdm. In 14.04 that would be sudo service lightdm restart and in 15.04 systemctl restart lightdm


95.6k 18 18 gold badges 247 247 silver badges 445 445 bronze badges

I think you can just make custom shortcut for gnome-session-quit --logout --no-prompt without any bash scripting.

39.4k 24 24 gold badges 129 129 silver badges 199 199 bronze badges

If the GUI is not actually responding anymore, all these shortcuts here won't work. Also they all require you to think of that before you want to use it, which is bad, if you are in a problematic state, where you e.g. cannot use the mouse anymore or GNOME (your desktop UI) freezes or so.

However, you can usually still change the TTY to a different one that has a CLI interface, login and kill the session there.

Step by step

  1. Press Ctrl + Alt + number to go to a different TTY.
    That (TTY) number best to try is, depending on your version and how many users you have logged in currently either the number 1 (<= Ubuntu 17.10) or 3 (> Ubuntu 17.10). Either read up the details behind that or just try them one-by-one or randomly, just be a little patient as switching to a different TTY can take some seconds.
    Or, of course, just try a high number that is likely always there (like 5 or so). And remember the number you've pressed, in order not to accidentally kill yourself later.
  2. Now login by typing your (or any) user name and password.
  3. Now run loginctl like this loginctl kill-session XY by replacing XY with the session number you want to kill.
    As you likely don't know that magic number by heard, you can read here how to find it. The TL;DR Is: Use tab to autocomplete it or use loginctl list-sessions and especially have a close look at the TTY number to know what to kill.

If you need, you may prepend the commands with sudo , e.g. if you try to kill another's users session. Though loginctl will deal with that by itself and ask you for a password if you need additional authentication.

Possibly do not forget logout from your "recovery TTY" you've used (just type exit or logout – or, of course, kill your own session), as you likely won't need it anymore.
If you don't and someone clever sneaks in, they can access all your data there, as these terminal sessions usually won't lock/protect themselves.

Somewhere (likely TTY1 in new Ubuntu versions; Ctrl + Alt + 1 ) is your login screen. In case you need to go back to it…

Ubuntu Desktop Log Out

If you are using an Ubuntu system with multiple users and one of the other users asks you to use the system, you have two options for giving the system to him. One is to switch users, and the other is to log out of your user session for the other person. The difference between the two is that when you switch users, your current session continues to run in the background and you can restore your applications to the same state when you log back in. However, when you log off, your current session is terminated and all running applications are stopped. So when you log back in, you will have to reopen all the applications you need. You can choose between switching users and logging off, whichever suits you at the time.

  • Using the UI
  • Using the Keyboard shortcut
  • Through the application launcher search bar
  • Through the Ubuntu command line, the Terminal

We have run the commands and procedures mentioned in this article on a Ubuntu 20.04 LTS system.

Method 1: Log Out Using the UI

One of the simple ways to log out of your Ubuntu session is to use the menu accessible through the downward arrow located at the top right corner of your Ubuntu screen. This is the menu you will see when you click this arrow:

Ubuntu log out using desktop menu

Click your username and you will be able to see the following two main sub-menu items: Log Out and Account Settings. Click on the Log Out option in order to log out of your current Ubuntu session. The following dialog will display if you do so:

Confirm that you want to log out

This dialog lets you log out right then by clicking the Log Out button. Otherwise, the system automatically ends your session after 60 seconds unless you press the Cancel button.

Method 2: Log Out Using Keyboard Shortcut

Ubuntu comes with a set of default keyboard shortcuts that allow you to perform commonly used Ubuntu operations with keyboard shortcuts. Logging out, a very common operation is also assigned such a keyboard shortcut. When you press the key combination Ctrl+Alt+Del, the same logout dialog we described above is displayed.

You can see all of these keyboard shortcuts and even create your own using the System Preferences utility. Open the Preferences utility directly from the Keyboard view by typing the word “keyboard shortcuts” into the search bar of your application launcher as follows:

Ubuntu Keyboard Shortcuts

Click on the Keyboard search result and you will be able to see the following view: Advertisement

Log out Keyboard Shortcut

Method 3: Log Out Using the Application Launcher Search

Another quick method to log out of your session is to use the very useful application launcher search bar. This bar can be accessed by hitting the Super/Windows key on your keyboard.

Enter the keyword “logout” in this search bar and you will see the Log Out utility in the search result as follows:

Log Out desktop launcher

Click on the Log Out search result and you will be presented with the same Log Out dialog that we described above.

Method 4: Log Out Using the Terminal

For a Terminal-savvy person, the Ubuntu command line also provides a way to log out of your Ubuntu user session.

Open the Ubuntu command line, the Terminal, either through the application launcher search or the Ctrl+Alt+T shortcut.

Then enter the following command in order to end the current user’s login session:

Desktop logout using shell

When you run this command, a dialog appears that lets you log out right then by clicking the Log Out button. Otherwise, the system automatically ends your session after 60 seconds unless you press the Cancel button on this dialog.

If you want to directly log out without having to see this dialog, you can make use of the following command:

So, these were some ways you can use to safely log out of your Ubuntu user session so that another user can use the system after you.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Search

About This Site

Latest Tutorials

Читайте также: