25

I'm having trouble setting my keyboard settings to swedish, running Raspbian on a raspberry pi. I've tried everything and the only thing that worked was the command setxkbmap se.

But this doesn't permanently save the settings, and it's back to default after reboot. Is there a command to permanently save these settings?

1 Answers1

33

An easy way would be to add the command to your $HOME/.profile file (you can create it if it doesn't exist):

setxkbmap se

That should make it run every time you log in.

Note that you should use $HOME/.profile rather than $HOME/.bash_profile, $HOME/.bashrc or some other similar file. This is because this setting should be read by your login manager.

terdon
  • 234,489
  • 66
  • 447
  • 667
  • I have 3 .profile files, located in /home/pi/, /root/ and /etc/skel/. I suppose you're referring to the one in /home/pi/? As you can see I'm not completely comfortable with linux. .bash_profile doesn't exist. – justanotherhobbyist Nov 06 '13 at 16:08
  • Adding `setxkbmap se` to `/home/pi/.profile` fixed it. Thanks, you saved me from alot of headache. – justanotherhobbyist Nov 06 '13 at 16:13
  • 3
    @hustlerinc the `/etc/profile` is the global one which will always be read, whichever user is logging on. Each user then has their own personal `.profile` file in their home folders. So, `/root/.profile` is `root`'s and `/home/pi/.profile` is the one for the user `pi`. I asked about the other files because if `$HOME/.bash_profile` or `$HOME/.bash_login` exist those files are read _instead_ of `$HOME/.profile`. If they don't exist, never mind. For more details, read the `INVOCATION` section of the `man bash` or see [here](http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files). – terdon Nov 06 '13 at 16:20
  • So putting it in `/etc/skel/.profile` would be better if I have more than 1 user? Or maybe `/root/.profile`? Which is best? – justanotherhobbyist Nov 06 '13 at 16:26
  • @hustlerinc if you want all users to have that layout, add it to `/etc/profile`. Adding it to `/skel/profile` will cause any new user to have the layout set in their personal `$HOME/.profile` but will not affect existing users. – terdon Nov 06 '13 at 16:28
  • Ok, I have no such file in etc, and creating a new .profile didn't fix it. – justanotherhobbyist Nov 06 '13 at 16:34
  • @hustlerinc The file in `/etc` should be called `profile` not `.profile` (no `.`). Does that work? – terdon Nov 06 '13 at 16:36
  • Oh, that's why I didn't find it. Problem solved, thanks! – justanotherhobbyist Nov 06 '13 at 16:39
  • Interestingly, it doesn't work if I put the setting in `~/.bash_profile` - it only works when I add it to `~/.profile` (at least on Linux Mint 17.1). – Jan Warchoł Nov 20 '15 at 21:22
  • 1
    @JanekWarchoł yes, that's because you are logging in graphically and while some login managers source `~/.profile`, to my knowledge, none of them source `~/.bash_profile`. It will work in `~/.bash_profile` if you log in from the command line (using `ssh`, for example). – terdon Nov 20 '15 at 21:56
  • 1
    @terdon but does using `setxkbmap` make sense in a command-line login at all? If I ssh from host A to host B, and I have some xkb settings on host B, they won't matter anyway because my keyboard is controlled by host A. After all, isn't xkb a tool for configuring keyboard in an X environment? – Jan Warchoł Nov 23 '15 at 11:08
  • @JanekWarchoł yes, it only makes sense in X which is why it should be in `.profile` and not `.bash_profile`. – terdon Nov 23 '15 at 11:35
  • @terdon ok, I have edited the answer to reflect that: http://unix.stackexchange.com/review/suggested-edits/117339 – Jan Warchoł Nov 23 '15 at 11:45
  • 1
    What if I want to use it in login password field? I notice that content in `$HOME/.profile` is not loaded before login. So when I enter password it is still old QWERTY layout. – WesternGun May 29 '18 at 15:36
  • 1
    @WesternGun that's a very different question. As you point out, the `.profile` is a user-specific setting. You could probably add it in `/etc/profile`, but that would affect _all_ users. It would be much better to do it by configuring your login manager. But for that, please ask a new question, explaining what operating system you are using, and what login manager. – terdon May 29 '18 at 15:39
  • OK I have posted another question [here](https://unix.stackexchange.com/questions/446756/centos-7-make-setxkbmap-configuration-permanent-after-reboot-in-login-screen). – WesternGun May 29 '18 at 17:48
  • Doesn't work in CentOS 7. I try to remap the CAPS to ESC. So I use `setxkbmap -option caps:swapescape` command. I placed it in `.profile` but no effect so in `.xinitrc`. When I placed it in `.bashrc` it only works with terminal. But I need it when I graphically log in. – LRDPRDX Dec 27 '19 at 03:49
  • @LRDPRDX please ask a new question. This will depend on what tool you use to log in, probably. – terdon Dec 27 '19 at 09:41
  • 3
    That doesn't seem to work (anymore?) on Ubuntu 20.04, I added it to both `/etc/profile` and `home/me/.profile`. – jay.sf Jun 01 '21 at 10:00