ioctl_kd(2)
okay bestie π«Ά you found ioctl_kd, and it's a whole thing. It hangs out in section 2 (System Calls Manual) of your friendly neighbourhood Linux system, and it's basically the master key to every console terminal and virtual console you could ever want to poke at. Strap in, hydrate, take a lil sip of water, because this is β¨longβ¨ and we are NOT skipping a single byte. Periodt.
β¨ NAME β¨
ioctl_kd β every single ioctl you can throw at a console terminal or a virtual console. consoley business only π
π¦ SYNOPSIS π¦
#include <linux/kd.h> /* Definition of op constants */
#include <sys/ioctl.h>
int ioctl(int fd, unsigned long op, void *argp);
π DESCRIPTION π
okay so, the entire roster here is Linux-specific ioctl(2) operations, and literally all of them are for console terminals and virtual consoles. Here's the full lineup, no cuts, no skips, and yes we're going through all of them together π
KDGETLED
Reading the LED state, bestie π‘. argp points to a uint8_t, and the lower three bits of *argp come back telling you which LEDs are lit, like so:
LED_CAP0x04β caps lock led πLED_NUM0x02β num lock led π’LED_SCR0x01β scroll lock led π
KDSETLED
Setting the LEDs, and it's giving main character energy. The LEDs get set to whatever the lower three bits of the unsigned long in argp say. BUT β plot twist β if any higher order bit is set, the LEDs snap right back to normal and go back to showing the state of the keyboard functions: caps lock, num lock, and scroll lock. iconic behaviour honestly.
π°οΈ lore drop, no cap: Before Linux 1.1.54, the LEDs just reflected the state of the corresponding keyboard flags, andKDGETLED/KDSETLEDwould also change the keyboard flags. Since Linux 1.1.54, the LEDs can be made to display arbitrary information, but by default they display the keyboard flags. The following two ioctls are the ones you use to reach the keyboard flags themselves.
KDGKBLED
Getting the keyboard flags CapsLock, NumLock, ScrollLock β and note, bestie, these are the flags not the lights π. argp points to a uint8_t which gets set to the flag state. Here's the breakdown: the low order three bits (mask 0x7) get the current flag state, and the low order bits of the next nibble (mask 0x70) get the default flag state. (Since Linux 1.1.54.)
KDSKBLED
Setting those same keyboard flags CapsLock, NumLock, ScrollLock β again, not the lights. argp is an unsigned long integer that holds the flag state you want. The low order three bits (mask 0x7) have the flag state, and the low order bits of the next nibble (mask 0x70) have the default flag state. (Since Linux 1.1.54.)
KDGKBTYPE
Getting the keyboard type πΉ. This one returns the value KB_101, which is defined as 0x02. that's it, that's the whole vibe.
KDADDIO
Adding an I/O port as valid β
. Equivalent to ioperm(arg,1,1).
KDDELIO
Deleting an I/O port as valid β. Equivalent to ioperm(arg,1,0).
KDENABIO
Enabling I/O to the video board π₯οΈβ¨. Equivalent to ioperm(0x3b4, 0x3df-0x3b4+1, 1).
KDDISABIO
Disabling I/O to the video board π«π₯οΈ. Equivalent to ioperm(0x3b4, 0x3df-0x3b4+1, 0).
KDSETMODE
Setting the text/graphics mode. argp is an unsigned integer holding one of these little guys:
KD_TEXT0x00KD_GRAPHICS0x01
KDGETMODE
Getting the text/graphics mode. argp points to an int which gets set to one of the values shown above for KDSETMODE. matching set, slay.
KDMKTONE
Generating a tone of a specified length π. The lower 16 bits of the unsigned long integer in argp specify the period in clock cycles, and the upper 16 bits give the duration in msec. If the duration is zero, the sound is turned off. Control returns immediately. For example, argp = (125<<16) + 0x637 would specify the beep normally associated with a ctrl-G. (Thus, since Linux 0.99pl1; broken in Linux 2.1.49-50.)
π yes bestie that "broken in Linux 2.1.49-50" is real, write it down, it's in the source.
KIOCSOUND
Starting or stopping sound generation πΆ. The lower 16 bits of argp specify the period in clock cycles (that is, argp = 1193180/frequency). And argp = 0 turns the sound off. In either case, control returns immediately. no waiting around, we love a fast return.
GIO_CMAP
Getting the current default color map from the kernel π. argp points to a 48-byte array. (Since Linux 1.3.3.)
PIO_CMAP
Changing the default text-mode color map π¨. argp points to a 48-byte array which contains, in order, the Red, Green, and Blue values for the 16 available screen colors: 0 is off, and 255 is full intensity. The default colors are, in order: black, dark red, dark green, brown, dark blue, dark purple, dark cyan, light grey, dark grey, bright red, bright green, yellow, bright blue, bright purple, bright cyan, and white. (Since Linux 1.3.3.)
GIO_FONT
Getting the 256-character screen font in expanded form π€. argp points to an 8192-byte array. It fails with error code EINVAL if the currently loaded font is a 512-character font, or if the console is not in text mode. β οΈ so watch out for that.
GIO_FONTX
Getting the screen font plus all its associated info, because the font wanted company π«Ά. argp points to a struct consolefontdesc (see PIO_FONTX). On the call, the charcount field should be set to the maximum number of characters that would fit in the buffer pointed to by chardata. On return, the charcount and charheight are filled with the respective data for the currently loaded font, and the chardata array contains the font data if the initial value of charcount indicated enough space was available; otherwise, the buffer is untouched and errno is set to ENOMEM. (Since Linux 1.3.1.)
PIO_FONT
Setting the 256-character screen font π‘. This loads the font into the EGA/VGA character generator. argp points to an 8192-byte map, with 32 bytes per character. Only the first N of them are used for an 8xN font (0 < N <= 32). This call also invalidates the Unicode mapping. π yeah it does that.
PIO_FONTX
Setting the screen font and its associated rendering info β¨. argp points to a
struct consolefontdesc {
unsigned short charcount; /* characters in font
(256 or 512) */
unsigned short charheight; /* scan lines per
character (1-32) */
char *chardata; /* font data in
expanded form */
};
If necessary, the screen will be appropriately resized, and SIGWINCH sent to the appropriate processes. This call also invalidates the Unicode mapping. (Since Linux 1.3.1.)
PIO_FONTRESET
Resetting the screen font, size, and Unicode mapping back to the bootup defaults π. argp is unused, but should be set to NULL to ensure compatibility with future versions of Linux. (Since Linux 1.3.28.)
GIO_SCRNMAP
Getting the screen mapping from the kernel πΊοΈ. argp points to an area of size E_TABSZ, which is loaded with the font positions used to display each character. Heads up bestie: this call is likely to return useless information if the currently loaded font is more than 256 characters. π
GIO_UNISCRNMAP
Getting the full Unicode screen mapping from the kernel πβ¨. argp points to an area of size E_TABSZ*sizeof(unsigned short), which is loaded with the Unicodes each character represent. A special set of Unicodes, starting at U+F000, are used to represent "direct to font" mappings. (Since Linux 1.3.1.)
PIO_SCRNMAP
Loading the "user definable" (fourth) table in the kernel which maps bytes into console screen symbols π₯. argp points to an area of size E_TABSZ.
PIO_UNISCRNMAP
Loading the "user definable" (fourth) table in the kernel which maps bytes into Unicodes, which are then translated into screen symbols according to the currently loaded Unicode-to-font map π. Special Unicodes starting at U+F000 can be used to map directly to the font symbols. (Since Linux 1.3.1.)
GIO_UNIMAP
Getting the Unicode-to-font mapping from the kernel π§. argp points to a
struct unimapdesc {
unsigned short entry_ct;
struct unipair *entries;
};
where entries points to an array of
struct unipair {
unsigned short unicode;
unsigned short fontpos;
};
(Since Linux 1.1.92.)
PIO_UNIMAP
Putting the unicode-to-font mapping in the kernel π€. argp points to a struct unimapdesc. (Since Linux 1.1.92)
PIO_UNIMAPCLR
Clearing the table, and possibly advising a hash algorithm π§Ή. argp points to a
struct unimapinit {
unsigned short advised_hashsize; /* 0 if no opinion */
unsigned short advised_hashstep; /* 0 if no opinion */
unsigned short advised_hashlevel; /* 0 if no opinion */
};
(Since Linux 1.1.92.)
KDGKBMODE
Getting the current keyboard mode β¨οΈ. argp points to a long which is set to one of these:
K_RAW0x00β Raw (scancode) modeK_XLATE0x01β Translate keycodes using keymapK_MEDIUMRAW0x02β Medium raw (scancode) modeK_UNICODE0x03β Unicode modeK_OFF0x04β Disabled mode (since Linux 2.6.39)
KDSKBMODE
Setting the current keyboard mode. argp is a long equal to one of the values shown for KDGKBMODE. same menu, you're just picking one now π
KDGKBMETA
Getting the meta key handling mode ποΈ. argp points to a long which is set to one of these:
K_METABIT0x03β set high order bitK_ESCPREFIX0x04β escape prefix
KDSKBMETA
Setting the meta key handling mode. argp is a long equal to one of the values shown above for KDGKBMETA.
KDGKBENT
Getting one entry in the key translation table (keycode to action code) π. argp points to a
struct kbentry {
unsigned char kb_table;
unsigned char kb_index;
unsigned short kb_value;
};
with the first two members filled in: kb_table selects the key table (0 <= kb_table < MAX_NR_KEYMAPS), and kb_index is the keycode (0 <= kb_index < NR_KEYS). kb_value is set to the corresponding action code, or K_HOLE if there is no such key, or K_NOSUCHMAP if kb_table is invalid. π kb_table bestie what did you do
KDSKBENT
Setting one entry in the translation table. argp points to a struct kbentry.
KDGKBSENT
Getting one function key string πΉπ. argp points to a
struct kbsentry {
unsigned char kb_func;
unsigned char kb_string[512];
};
kb_string is set to the (null-terminated) string corresponding to the kb_functh function key action code.
KDSKBSENT
Setting one function key string entry. argp points to a struct kbsentry.
KDGKBDIACR
Reading the kernel accent table β¨Γ©β¨. argp points to a
struct kbdiacrs {
unsigned int kb_cnt;
struct kbdiacr kbdiacr[256];
};
where kb_cnt is the number of entries in the array, each of which is a
okay bestie that's where part 1 leaves us hanging π β there's more coming, so don't go anywhere π
struct kbdiacr {
unsigned char diacr;
unsigned char base;
unsigned char result;
};
KDGETKEYCODEβ bestie, this one reads a single entry out of the kernel's keycode table (that's your scan code β‘οΈ keycode translation) β¨.argppoints at a:
struct kbkeycode {
unsigned int scancode;
unsigned int keycode;
};
and keycode comes back set to match whatever scancode you handed over π
. Two things to know, okay? It only actually looks things up for 89 <= scancode <= 255, and for 1 <= scancode <= 88 you just get keycode==scancode because the kernel keeps those identity-mapped, no cap π«Ά. Been around since Linux 1.1.63.
KDSETKEYCODEβ the βοΈ write-y twin: writes an entry into that same kernel keycode table.argppoints to astruct kbkeycode, same shape as above. (Since Linux 1.1.63.)KDSIGACCEPTβ okay so this one is delulu powerful, listen π¨: the calling process straight-up declares that it's willing to accept the signalargpwhen that signal gets generated by someone pressing the appropriate key combination on the keyboard β¨οΈπ₯. Constraint:1 <= argp <= NSIG. (If you wanna go read the source, it'sspawn_console()over inlinux/drivers/char/keyboard.cβ iykyk π₯.)
π RETURN VALUE π
Slay, here's the good news: on success you get 0 back (unless it says otherwise somewhere above, so likeβ¦ read the receipts π). And on failure? -1 π« , and errno gets set so it can tell you exactly what went wrong. The way that the kernel just tells you, iconic β¨.
π¨ ERRORS π¨
EINVALβargpis invalid, bestie. It's giving "you passed me nonsense" π.
π STANDARDS π
Linux. That's it, that's the standard, it's a Linux thing, periodt π.
π¦ SEE ALSO π¦
and that's a wrap on ioctl_kd(2) π
πΈ go forth and talk to your keyboard, she's been waiting. this lil masterpiece lives in Linux man-pages 6.20, dated 2026-02-08 β screenshot it, bookmark it, kiss it goodnight π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-2.0-or-later
- Copyright 1995, James R. Van Zandt <jrv@vanzandt.mv.com>
- Copyright 1995, Andries E. Brouwer <aeb@cwi.nl>
- Copyright 1995, Rickard E. Faith <faith@cs.unc.edu>
- Copyright 1995, H. Peter Anvin <hpa@storm.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms