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_CAP 0x04 β€” caps lock led πŸ‘‘
  • LED_NUM 0x02 β€” num lock led πŸ”’
  • LED_SCR 0x01 β€” 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, and KDGETLED/KDSETLED would 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_TEXT 0x00
  • KD_GRAPHICS 0x01

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_RAW 0x00 β€” Raw (scancode) mode
  • K_XLATE 0x01 β€” Translate keycodes using keymap
  • K_MEDIUMRAW 0x02 β€” Medium raw (scancode) mode
  • K_UNICODE 0x03 β€” Unicode mode
  • K_OFF 0x04 β€” 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_METABIT 0x03 β€” set high order bit
  • K_ESCPREFIX 0x04 β€” 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) ✨. argp points 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. argp points to a struct 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 signal argp when 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's spawn_console() over in linux/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 β€” argp is 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms