11

That's a question I've seen several time for several Linux flavours, so let's try to be exhaustive.
What is the method to execute script/command/program before and after user login into its desktop session ?

6
  • Note that in this specific use case "before login" means "after the user has logged in but before the the user's graphical environment has been initialized or before it has been presented to the user". A user's graphical environment simply does not exist at all before the user has logged in.
    – AlexP
    Jun 20, 2018 at 11:36
  • 5
    @AlexP I don't get it, did I speel something wrong ? When I say before login it's before is logged in on Gnome KDE or XFCE. And you can run program before anyone indeed logged in
    – Kiwy
    Jun 20, 2018 at 11:44
  • 3
    You can, but not in the graphical environment. The graphical environment is always per user, and a user's graphical environment does not exist before the user logs in. You can run a program in the display manager's graphical environment, because the display manager runs under a system account.
    – AlexP
    Jun 20, 2018 at 11:49
  • 2
    @AlexP I find your comment very confusing. "Before login" means "after the user has logged in". Those are contradictory statements. I understood OP's question to mean: how can I run commands at time points X and Y? Where X is prior to entering your username and password in the DM and Y is after entering your username and password into the DM. Who said anything about "a user's graphical environment?". You added that phrase but it isn't present in the question.
    – JBentley
    Mar 24, 2021 at 15:38
  • @JBentley: The question said so: "before and after user login into its desktop session". Clarified in the comments as referring to Gnome, KDE or XFCE. This means just after the user has been authenticated but before starting up the desktop session, and then after the desktop session has started. (Otherwise the question could have been simply rephrased as to ask for running a program independent of a user's interactive login.)
    – AlexP
    Mar 24, 2021 at 15:51

3 Answers 3

19

Introduction

To run a program in graphical environement before a user logged in a graphical environment depend on your display manager. A display manager is in charge to provide you a login interface and setup your graphical environment once logged in. the most important are the following:
  • GDM is the GNOME display manager.
  • LightDM is a cross-desktop display manager, can use various front-ends written in any toolkit.
  • LXDM is the LXDE display manager but independent of the LXDE desktop environment.
  • SDDM is a modern display manager for X11 and Wayland aiming to be fast, simple and beautiful.

We will review how to setup the execution of command when the display manager popup before any user logged in and how to execute something when someone is finally logged in.

If you don't know which one you're running, you can refer to this question :
Is there a simple linux command that will tell me what my display manager is?

IMPORTANT
Before I start, you are going to edit file that except if mention execute command as root. Do not remove existing stuff in those files except if you know what you're doing and be careful in what you put in those file. This could remove your ability to log in.

GDM

Be careful with GDM, it will run all script as `root`, a different error code than 0 could limit your log in capability and GDM will wait for your script to finish making it irresponsive as long as your command run. For complete explanation [read the documentation][5].

Before Login

If you need to run commands before a user logged-in you can edit the file: `/etc/gdm3/Init/Default`. This file is a shell script that will be executed before the display manager is displayed to the user.

After Login

If you need to execute things once a user has logged in but before its session has been initialize edit the file: `/etc/gdm3/PostLogin/Default` If you want to execute command after the session of session initialization (env, graphical environment, login...) edit the file: `/etc/gdm3/PreSession/Default`

LightDM

I will talk about lightdm.conf and not about /etc/lightdm.conf.d/*.conf. You can do what you want what is important is to know the options you can use. Be careful with lightDM, you could already have several other script starting you should read precisely your config file before editing it. also the order in which you put those script might influence the way the session load.

LightDM works a bit differently from the others you will put options in the main configuration files to indicate script that will be execute.

Edit the main lightDM conf file /etc/lightdm/lightdm.conf.

You should add first line with [Seat:*], as indicated here:

Later versions of lightdm (15.10 onwards) have replaced the obsolete [SeatDefaults] with [Seat:*]

Before Login

Add a line `greeter-setup-script=/my/path/to/script` This script will be executed when lightDM shows the login interface.

After Login

Add a line `session-setup-script=/script/to/start/script` This will run the script as `root` after a user successfully logged in.

LXDM

Before Login

If you want to execute command before anyone logged in, you can edit the shell script: `/etc/lxdm/LoginReady`

After Login

If you want to execute command after someone logged in but as root, you can edit the shell script: `/etc/lxdm/PreLogin` And if you want to run command as the logged in user, you can edit the script: `/etc/lxdm/PostLogin`

SDDM

Before Login

Modify the script located at /usr/share/sddm/scripts/Xsetup. This script is executed before the login screen appears and is mostly used to adjust monitor displays in X11. Not sure what the equivalent would be for wayland

After Login

sddm will now source the script located at /usr/share/sddm/scripts/Xsession, which in turn will source the user's dotfiles depending on their default shell.

For bash shell, it will source ~/.bash_profile (among others), and for zsh, it will source ${ZDOTDIR:-$HOME}/.zprofile (among others). You can take this opportunity to modify those files to also run any other command you need after logging in.

6
  • Is it possible to run a program before the login screen appears for the user?
    – Steve3p0
    Jan 24, 2019 at 23:15
  • @Steve3p0 for lightdm you can see some alternative options here.
    – JBentley
    Mar 24, 2021 at 16:02
  • @Steve3p0, how long before? Systemd service for targets (unix.stackexchange.com/questions/47695/…), if before systemd started, then custom init script. Feb 20, 2022 at 1:51
  • How to run script as a logged in user, not root (e.g. lightDM, after)? Or how to find out who logged in to switch user inside the script? (I want to run dconf) Feb 20, 2022 at 2:28
  • GDM no longer runs /etc/gdm3/Init/Default, see gitlab.gnome.org/GNOME/gdm/-/issues/317
    – bain
    Feb 17 at 12:43
0

I needed to configure my mouse before I logged in so that all users could benefit from it. I found the solution reading this question. I thought I should look at configuring it by GDM, but to set in up at the start of any sessions, I had to set it through XDG autostart. In /etc/xdg/autostart, there are a bunch of .desktop files that are run at login time for every xsessions (in my ubuntu system).

1
  • Can you add the important parts of the solution here? If/when that page changes or disappears it's helpful to still have the solution available to people that find this question & answer. Oct 15, 2020 at 12:42
0

With GDM3 you need to put a script in /usr/share/gdm/greeter/autostart/ (requires sudo permissions) for it to run before login. For example, to get my keyboard backlight to turn on before login (so I can see the keys to type my password in a dark room):

sudo nano /usr/share/gdm/greeter/autostart/led.desktop

And write this into the file:

[Desktop Entry]
Type=Application
Name=ledstart
Exec=xset led on
NoDisplay=true
X-GNOME-AutoRestart=true

The line starting with Exec= is where you put your command.

Unfortunately it seems that Gnome resets the session after login, though, so this command doesn't persist - I then have to set a new xset led on command for every user login. If I find a fix for this I'll update the answer.

You must log in to answer this question.

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