Debian and console-setup
On Debian Linux and Debian FreeBSD, the design is for the X server and the kernel virtual terminals to share one set of configuration information. The console-setup package provides tools and startup scripts that take the keyboard/font configuration of the X server, convert it, and use it to configure the kernel virtual terminals.
Specifically: The console-setup service invokes the setupcon --save command, which generates scripts in /etc/console-setup containing the converted keyboard and font setup instructions, which are run against the kernel virtual terminal devices by udev rules. The keyboard map is converted from XKB to loadkeys format by ckbcomp, and the generated script invoked from the udev rule runs loadkeys.
So on Debian Linux and Debian FreeBSD you need to ensure that your XKBMODEL, XKBLAYOUT, XKBVARIANT, XKBOPTIONS variables are properly set in /etc/default/keyboard. In your case in particular you need to ensure that the XKBOPTIONS variable has caps:swapescape. Then you need to ensure that you have the console-setup package properly installed.
systemd Linux
On systemd operating systems, things are somewhat less integrated than on Debian. There's a configuration file named /etc/vconsole.conf and a service named systemd-vconsole-setup.service that processes it at system bootstrap (in response to a udev rule announcing the existence of the kernel virtual terminal subsystem). But this isn't joined up with the X server configuration at all.
The vconsole.conf file has a KEYMAP setting denoting the keyboard map that is passed to loadkeys, which again one does not need to explicitly run oneself. But it's left entirely up to you to create a (customized) keyboard map that swaps Caps Lock and Escape and put it where loadkeys can find it.
To get more joined up settings, one has to involve another two services, systemd-localed and the Desktop Bus. One runs, say,
localectl set-x11-keymap pl pc105 "" "caps:swapescape"
and the locale D-BUS service goes and rewrites
/etc/vconsole.conf with its best guess as to the
nearest equivalent map for the kernel virtual terminal. This nearest equivalent may not be
exactly equivalent, though, and you may find it just outright ignoring options and suchlike.
Of course, you could always alternatively use Debian's ckbcomp by hand to directly convert an XKB keyboard map. ☺
Further reading
loadkeys, so that I can put it in login script. Or shall I make another executable which callsloadkeysand set sticky bit on that. – saga Nov 29 '16 at 11:19saga ALL = NOPASSWD: /usr/local/bin/swap-esc-tabto/etc/sudoers(use e.g.EDITOR=nano visudoto edit the file withnano), create root-executable shell script/usr/local/bin/swap-esc-tabthat runs the command shown in my answer, and finally addsudo /usr/local/bin/swap-esc-tabto your login script. – Nominal Animal Nov 29 '16 at 20:49/usr/local/admin-bin/, with simpleexec sudo /usr/local/admin-bin/command "$@"wrapper bash/dash scripts in/usr/local/bin/, and addnominal-animal ALL = NOPASSWD: /usr/local/admin-bin/*to/etc/sudoers, so I can run those scripts as a normal user, only acquiring root for those specific scripts. – Nominal Animal Nov 29 '16 at 20:55