{"id":257,"date":"2022-05-22T21:31:20","date_gmt":"2022-05-23T01:31:20","guid":{"rendered":"http:\/\/blog.signalsguru.net\/?p=257"},"modified":"2022-09-15T09:31:20","modified_gmt":"2022-09-15T13:31:20","slug":"linux-keyboard-layout-customization","status":"publish","type":"post","link":"http:\/\/blog.signalsguru.net\/archives\/257","title":{"rendered":"Linux Keyboard Layout Customization"},"content":{"rendered":"\n

Setting the keyboard layout on Linux\/X11 has always been a bit of a mystery to me. To make matters worse, I’m using Vagrant to frequently spin up a new VM; the extra step of system restart or log out so that changes can take effect is very unwelcome.<\/p>\n\n\n\n

When troubleshooting initialization and configuration issues, it’s helpful to know what kind of shell you are in. A login shell normally only runs once at login and executes .bash_profile. An interactive shell will usually run every time you open a system console and executes .bashrc. The ‘i’ in the ‘-‘ variable indicates an interactive shell.<\/p>\n\n\n\n

> echo $-\nhimBH\n\n> shopt login_shell\nlogin_shell     off<\/mark><\/code><\/pre>\n\n\n\n

You can set the keyboard layout globally by setting the system locale. The only problem with this is that it doesn’t take effect until the next login. Normally this wouldn’t be a problem, but if you are frequently starting with a new VM, logging out just to fix the keyboard layout is annoying.<\/p>\n\n\n\n

> localectl set-x11-keymap us,us \"\" dvorak,<\/mark><\/code><\/pre>\n\n\n\n

Since I’m using a VM and the host machine is already password protected, there is really no reason to require logging in at all. My KDE install is using GDM by default.<\/p>\n\n\n\n

> grep 'ExecStart=' \/etc\/systemd\/system\/display-manager.service\nExecStart=\/usr\/sbin\/gdm<\/mark><\/code><\/pre>\n\n\n\n

You can modify \/etc\/gdm\/custom.conf to log in automatically. Unfortunately, this does not take effect until the next boot, so I added it to my base Vagrant box. It may also be possible to restart the display manager<\/a>.<\/p>\n\n\n\n

[daemon] \nAutomaticLoginEnable=True \nAutomaticLogin=username<\/mark><\/code><\/pre>\n\n\n\n

This was causing some problems with my KDE autostart script, but it’s easy enough to fix<\/a>. However, I had similar problems with KDE configurations (e.g. kwinrc) that I changed not taking effect immediately when Vagrant first boots the system. You can fix this by looking to see if any new scripts have been added to the Autostart directory in the past few minutes and then restarting the KDE session. If you are modifying .bash_profile or .bashrc at first boot, you may still have a problem with these files running before your changes are made. The fix mentioned above should also fix this issue. Alternatively, if you are only modifying .bashrc, you could just sleep for a bit before starting any interactive shell. You could modify the configuration files in the base box; the only problem is that KDE doesn’t create the directories and files until your first login.<\/p>\n\n\n\n

if [ ! -z  \"$(find ~\/.kde\/Autostart -mtime -.002)\" ]; then\n    kwin --replace >& \/dev\/null &\nfi<\/mark><\/code><\/pre>\n\n\n\n

Using auto-login obviates the problem of the login screen still using the old keyboard layout, but once you are logged in, the keyboard layout is still the old one. You can also set the keyboard layout for only the current X11 session, which takes effect immediately.<\/p>\n\n\n\n

> setxkbmap dvorak\n> setxkbmap -query\nrules:      evdev\nmodel:      evdev\nlayout:     dvorak<\/mark><\/code><\/pre>\n\n\n\n

It probably won’t hurt to run this every time, but there is no need on subsequent logins. First, notice that once set-x11-keymap takes effect, the variant field of a setxkbmap query has been set.<\/p>\n\n\n\n

> more \/etc\/X11\/xorg.conf.d\/00-keyboard.conf\n# Read and parsed by systemd-localed. It's probably wise not to edit this file\n# manually too freely.\nSection \"InputClass\"\n        Identifier \"system-keyboard\"\n        MatchIsKeyboard \"on\"\n        Option \"XkbLayout\" \"us,us\"\n        Option \"XkbVariant\" \"dvorak,\"\nEndSection\n\n> setxkbmap -query\nrules:      evdev\nmodel:      evdev\nlayout:     us,us\nvariant:    dvorak,<\/mark><\/code><\/pre>\n\n\n\n

So we can add the following to .bash_profile or .bashrc, and it will only run before set-x11-keymap takes effect.<\/p>\n\n\n\n

if [[ ! $(setxkbmap -query) =~ variant:[[:space:]]*dvorak ]]; then\n    setxkbmap dvorak\nfi<\/mark><\/code><\/pre>\n\n\n\n

There is still a problem though. If you ssh in, you don’t want to change the keyboard layout because you will already be using the altered keyboard layout of the host. The xhost command will return false if it can’t connect to an X11 display.<\/p>\n\n\n\n

> xhost  # from GUI\naccess control enabled, only authorized clients can connect\nSI:localuser:root\nSI:localuser:myuser\n\n> xhost  # from ssh\nxhost:  unable to open display \"\"<\/mark><\/code><\/pre>\n\n\n\n

So, with this extra condition, we now only run setxkbmap before setx-x11-keymap takes effect and if we are in the GUI.<\/p>\n\n\n\n

if xhost >& \/dev\/null && [[ ! $(setxkbmap -query) =~ variant:[[:space:]]*dvorak<\/mark> ]]; then\n    setxkbmap dvorak\nfi<\/mark><\/code><\/pre>\n\n\n\n

Another possibility is to run setxkbmap in the auto-start script only the first time the auto-start script runs.<\/p>\n\n\n\n

Update<\/p>\n\n\n\n

There is an easier way to deal with the problem of setting the keyboard layout and KDE configuration files on first boot. If you have created a base box using Vagrant, simply leave the run level as the default. This is multi-user.target on CentOS with Systemd. Upon booting with X11, make any changes to the KDE configuration files, change to auto-login if you haven’t already, set the locale, and then change the run-level to graphical.target.<\/p>\n\n\n\n

systemctl isolate graphical.target\nsystemctl set-default graphical.target<\/code><\/pre>\n\n\n\n

The first line changes to the graphical run-level immediately. The second makes it permanent.<\/p>\n\n\n\n

There’s no longer any need to also change the keyboard layout using setxkbmap on the first login. And if you do it this way, you won’t run into any race conditions between updating configuration files and KDE loading those same configuration files.<\/p>\n","protected":false},"excerpt":{"rendered":"

Setting the keyboard layout on Linux\/X11 has always been a bit of a mystery to me. To make matters worse, I’m using Vagrant to frequently spin up a new VM; the extra step of system restart or log out so that changes can take effect is very unwelcome. When troubleshooting initialization and configuration issues, it’s …<\/p>\n

Linux Keyboard Layout Customization<\/span> Read More »<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts\/257"}],"collection":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":24,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":289,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts\/257\/revisions\/289"}],"wp:attachment":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}