How to Run Startup Scripts in KDM before KDE Starts

KDE has an easy-to-use system for configuring programs or scripts to run at startup. Linux distributions also have methods of starting programs at boot, but there are some unique instances when it would be ideal to start a script at the moment KDM starts, in between the start of X and the start of your desktop environment.

KDM, the default display manager for KDE, starts when your X server instance begins. This means that the user has still not logged in, and any settings established at this point will apply to all users. Moreover, making these changes in KDM is often safer and easier than modifying your xorg.conf file or coming up with some hack that will only work for a particular user once he or she has logged in.

KDM default theme

KDM has its own startup script called Xsetup. When X is started, KDM will execute anything within this script file. To run your own scripts, all you have to do is tell Xsetup to execute them.

For example, you might want to run xrandr when KDM starts. This will automatically configure your monitor or multiple monitors to your preferred settings, and those settings will stick when any user logs into KDE, unless they override them. To accomplish this, follow these steps:

  1. Locate the Xsetup file. It may vary depending on your distribution. In Kubuntu, it is located at: /etc/kde4/kdm/Xsetup
  2. Edit the file as root.
    example: Press Alt+F2 and type “kdesudo kate /etc/kde4/kdm/Xsetup
  3. Enter the commands you want to execute (note: They will be run as root before the login dialog appears)
    example: xrandr --auto --output VGA1 --mode 1440x900 --right-of LVDS1 --refresh 60.1
  4. Click “Save” and close your text editor.

In most cases, it is a good idea to place your command string before this line:

/sbin/initctl -q emit login-session-start DISPLAY_MANAGER=kdm

That will execute your script before KDM starts.

Xsetup editing in Kate

Another example of something you might want to execute before KDM starts is to set Numlock to either “on” or “off”. KDE has a setting for this, but setting it in KDM will essentially enable a global default. To enable or disable numlock, follow these steps:

  1. Install numlockx.
    sudo apt-get install numlockx
  2. In your Xsetup file, add the following line:
    numlockx on
  3. Save your Xsetup file.

Just to reiterate an important point, you should always be mindful that anything you put in Xsetup will be executed by root and will affect anyone who logs in with KDM, no matter the user or desktop environment they choose from the KDM menu. This can have far-reaching consequences, particularly if you have a multi-user system.

Safety issues aside, using KDM’s Xsetup script is a great way to get your desktop exactly the way you want it even before you login. Whether you are enabling numlock or even setting up a custom keymap with Xmodmap, you can do it from Xsetup and not have to worry about configuring it after you start your user session.