DEV Community

vast cow
vast cow

Posted on

How to Use a Japanese Keyboard (JIS Layout) in GRUB: Rocky Linux / RHEL Compatible

How to Use a Japanese Keyboard (JIS Layout) in GRUB: Rocky Linux / RHEL Compatible

When working with the GRUB menu or command line, you may encounter issues where the keyboard is treated as a US layout.

This is particularly problematic with Japanese keyboards, as:

  • @
  • :
  • "
  • \
  • _

These symbols are located in different positions on US and JIS layouts, making it quite inconvenient to edit kernel parameters or enter commands in GRUB.

This article summarizes how to change the GRUB keyboard layout to the Japanese JIS layout on RHEL-based Linux distributions such as Rocky Linux, AlmaLinux, RHEL, and CentOS Stream.

Note that the "Japanese keyboard" referred to here does not refer to the Japanese input IME. It is not intended to enable Japanese character input in GRUB, but rather to configure it to match the physical key layout of a JIS keyboard.


Linux Keyboard Settings and GRUB Keyboard Settings Are Different

Normally, you can change the Linux console keymap with the following command:

localectl set-keymap jp
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can specify it in the kernel parameters or configuration file, such as:

vconsole.keymap=jp
Enter fullscreen mode Exit fullscreen mode

However, these are settings that take effect after the Linux kernel has booted.

Since GRUB runs before Linux, the Linux keyboard settings are not reflected in GRUB.

In other words, in the boot sequence:

GRUB
  ↓
Linux kernel
  ↓
systemd / console
Enter fullscreen mode Exit fullscreen mode

The keyboard layout for GRUB must be configured within GRUB itself.


Packages Required for Rocky Linux / RHEL

In Rocky Linux and RHEL-based systems, GRUB-related commands have grub2- prefixes.

Install the necessary packages:

sudo dnf install grub2-tools-extra kbd
Enter fullscreen mode Exit fullscreen mode

The following commands are mainly used:

grub2-kbdcomp
grub2-mkconfig
Enter fullscreen mode Exit fullscreen mode

Use grub2-kbdcomp to create a keyboard layout file that GRUB can read.


Create a Japanese Keymap for GRUB

First, create a save location:

sudo mkdir -p /boot/grub2/layouts
Enter fullscreen mode Exit fullscreen mode

Next, create the Japanese JIS layout keymap:

sudo grub2-kbdcomp -o /boot/grub2/layouts/jp.gkb jp
Enter fullscreen mode Exit fullscreen mode

Verify that it was created:

ls -l /boot/grub2/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

If a file exists as follows, the preparation is complete:

/boot/grub2/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

In GRUB, the keyboard layout is changed by loading this .gkb file.


Temporarily Change to JIS Layout Only During GRUB Boot

Instead of making a permanent change all at once, it is recommended to first load it temporarily from the GRUB command line and verify that it works.

When the GRUB menu is displayed, press:

c
Enter fullscreen mode Exit fullscreen mode

to enter the GRUB command line.

Execute the following command:

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

This will change to the Japanese JIS layout for that GRUB session only.

Since it will revert to the original upon reboot, it is suitable for testing the settings.


If keymap jp Cannot Be Loaded

Check the prefix used by GRUB:

echo $prefix
Enter fullscreen mode Exit fullscreen mode

For example, it may display:

(hd0,gpt2)/grub2
Enter fullscreen mode Exit fullscreen mode

Next, check if the keymap is visible:

ls $prefix/layouts/
Enter fullscreen mode Exit fullscreen mode

If:

jp.gkb
Enter fullscreen mode Exit fullscreen mode

is displayed, GRUB can access the file.

In this case, it should normally be loaded with:

keymap jp
Enter fullscreen mode Exit fullscreen mode

Depending on the environment, you may want to explicitly specify the file to confirm:

keymap $prefix/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

Make the JIS Layout Permanent

Once you have verified that it works, incorporate it into the GRUB configuration.

On RHEL-based systems, appending to /etc/grub.d/40_custom is a clear method.

sudo vi /etc/grub.d/40_custom
Enter fullscreen mode Exit fullscreen mode

Add the following content:

insmod keylayouts
keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

Alternatively, depending on the environment, the following specification may also work:

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

Personally, I prefer to explicitly specify it so that it is clear which file is being used:

keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

This makes it easier to troubleshoot.


Regenerate grub.cfg

After changing the settings, regenerate the GRUB configuration.

On newer RHEL-based systems such as Rocky Linux 9:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Enter fullscreen mode Exit fullscreen mode

Reboot:

sudo reboot
Enter fullscreen mode Exit fullscreen mode

In the GRUB menu, press c to open the command line and check the symbol keys.

For example:

@
:
"
\
_
Enter fullscreen mode Exit fullscreen mode

If these can be entered as they are printed on the keycaps, the JIS layout has been applied.


Notes for UEFI Environments

Older articles mention examples of running grub2-mkconfig directly on the following file in UEFI environments:

/boot/efi/EFI/redhat/grub.cfg
Enter fullscreen mode Exit fullscreen mode

In Rocky Linux, it may be:

/boot/efi/EFI/rocky/grub.cfg
Enter fullscreen mode Exit fullscreen mode

However, in recent RHEL-based systems, the UEFI grub.cfg is configured to be used as a stub to load the main /boot/grub2/grub.cfg.

Therefore, in Rocky Linux 9 and later, it is generally recommended to use:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Enter fullscreen mode Exit fullscreen mode

It is safer not to directly overwrite the UEFI grub.cfg unnecessarily.


Is terminal_input at_keyboard Necessary?

In GRUB keyboard configuration examples, you may see settings like:

insmod at_keyboard
terminal_input at_keyboard
Enter fullscreen mode Exit fullscreen mode

However, on USB keyboards and UEFI environments, explicitly switching the input path to at_keyboard may prevent keyboard input.

If you only want to change to the JIS layout, it is safest to first try:

insmod keylayouts
keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

Unless there is a specific reason, it is not necessary to change the input terminal itself.


Temporary Change vs. Permanent Change

To summarize, the following applies:

Method Setting After Reboot
GRUB command line insmod keylayoutskeymap jp Reverts
Add to 40_custom keymap ${prefix}/layouts/jp.gkb Persists
Linux localectl Changes only the Linux console Does not affect GRUB

It is safe to test with a temporary change first, and then make it permanent if there are no problems.


Can I Change to the JIS Layout in GRUB Without Creating a .gkb File?

Basically, no.

The keymap command in GRUB is designed to load pre-created GRUB keyboard layout files.

Therefore, during Linux startup, you need to:

grub2-kbdcomp
Enter fullscreen mode Exit fullscreen mode

to create:

jp.gkb
Enter fullscreen mode Exit fullscreen mode

in advance.

At a minimum, you need to perform the following preparations on the Linux side:

sudo mkdir -p /boot/grub2/layouts
sudo grub2-kbdcomp -o /boot/grub2/layouts/jp.gkb jp
Enter fullscreen mode Exit fullscreen mode

After that, you can always run:

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

in the GRUB command line to temporarily switch to the JIS layout.


Summary

When setting GRUB to a Japanese JIS keyboard layout on Rocky Linux or RHEL, the key points are:

  1. First, create a GRUB keymap:

    sudo dnf install grub2-tools-extra kbd
    sudo mkdir -p /boot/grub2/layouts
    sudo grub2-kbdcomp -o /boot/grub2/layouts/jp.gkb jp
    
  2. If you want to try it temporarily in GRUB:

    insmod keylayouts
    keymap jp
    
  3. To make it permanent, add to /etc/grub.d/40_custom:

    insmod keylayouts
    keymap ${prefix}/layouts/jp.gkb
    

    and run:

    sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    

GRUB runs before Linux boots, so the localectl set-keymap jp and other Linux settings are completely separate.

If you frequently edit kernel parameters or perform rescue operations in GRUB, setting up the JIS keyboard will make it much easier to operate.

GRUBで日本語キーボード(JIS配列)を使う方法:Rocky Linux / RHEL系対応

GRUBのメニューやコマンドラインを操作していると、キーボードがUS配列として扱われて困ることがあります。

特に日本語キーボードでは、

  • @
  • :
  • "
  • \
  • _

などの記号位置がUS配列とJIS配列で異なるため、GRUB上でカーネルパラメータを編集したり、コマンドを入力したりする際にかなり不便です。

この記事では、Rocky Linux、AlmaLinux、RHEL、CentOS StreamなどのRHEL系Linuxで、GRUBのキーボードレイアウトを日本語JIS配列に変更する方法をまとめます。

なお、ここでいう「日本語キーボード」は日本語入力IMEのことではありません。GRUB上で日本語文字を入力できるようにするものではなく、物理的なJISキーボードのキー配置に合わせるための設定です。


Linuxのキーボード設定とGRUBのキーボード設定は別物

通常、Linux側のコンソールキーマップは次のようなコマンドで変更できます。

localectl set-keymap jp
Enter fullscreen mode Exit fullscreen mode

あるいはカーネルパラメータや設定ファイルで、

vconsole.keymap=jp
Enter fullscreen mode Exit fullscreen mode

のように指定することもあります。

しかし、これらはLinuxカーネルが起動した後の設定です。

GRUBはLinuxより前に動作しているため、Linux側のキーボード設定はGRUBには反映されません。

つまり、

GRUB
  ↓
Linux kernel
  ↓
systemd / console
Enter fullscreen mode Exit fullscreen mode

という起動順序において、GRUB用のキーボードレイアウトはGRUB自身に設定する必要があります。


Rocky Linux / RHEL系で必要なパッケージ

Rocky LinuxやRHEL系では、GRUB関連コマンドの名前に grub2- が付いています。

必要なパッケージをインストールします。

sudo dnf install grub2-tools-extra kbd
Enter fullscreen mode Exit fullscreen mode

主に使用するのは次のコマンドです。

grub2-kbdcomp
grub2-mkconfig
Enter fullscreen mode Exit fullscreen mode

grub2-kbdcomp を使って、GRUBが読み込めるキーボードレイアウトファイルを作成します。


GRUB用の日本語キーマップを作成する

まず保存先を作ります。

sudo mkdir -p /boot/grub2/layouts
Enter fullscreen mode Exit fullscreen mode

続いて、日本語JIS配列のキーマップを作成します。

sudo grub2-kbdcomp -o /boot/grub2/layouts/jp.gkb jp
Enter fullscreen mode Exit fullscreen mode

生成できたか確認します。

ls -l /boot/grub2/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

例えば次のようにファイルが存在していれば準備完了です。

/boot/grub2/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

GRUBでは、この .gkb ファイルを読み込むことでキーボード配列を変更します。


まずはGRUB起動中だけ一時的にJIS配列へ変更する

いきなり永続設定を変更するより、最初はGRUBのコマンドラインから一時的に読み込んで動作確認するのがおすすめです。

GRUBメニューが表示されたら、

c
Enter fullscreen mode Exit fullscreen mode

を押してGRUBコマンドラインに入ります。

次のコマンドを実行します。

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

これで、そのGRUBセッション中だけ日本語JIS配列になります。

再起動すれば元に戻るため、設定テストにも向いています。


keymap jp で読み込めない場合

GRUBが使用している prefix を確認します。

echo $prefix
Enter fullscreen mode Exit fullscreen mode

例えば、

(hd0,gpt2)/grub2
Enter fullscreen mode Exit fullscreen mode

のように表示されることがあります。

次に、キーマップが見えるか確認します。

ls $prefix/layouts/
Enter fullscreen mode Exit fullscreen mode

ここで、

jp.gkb
Enter fullscreen mode Exit fullscreen mode

が表示されれば、GRUBからファイルにアクセスできています。

その場合は通常、

keymap jp
Enter fullscreen mode Exit fullscreen mode

で読み込めます。

環境によっては明示的にファイルを指定して確認してもよいでしょう。

keymap $prefix/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

永続的にJIS配列へ変更する

動作確認できたら、GRUBの設定に組み込みます。

RHEL系では /etc/grub.d/40_custom に追記する方法が分かりやすいです。

sudo vi /etc/grub.d/40_custom
Enter fullscreen mode Exit fullscreen mode

次の内容を追加します。

insmod keylayouts
keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

あるいは、環境によっては次のような指定でも動作します。

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

個人的には、どのファイルを使用しているか分かりやすいので、

keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

のように明示しておく方がトラブルシュートしやすいでしょう。


grub.cfgを再生成する

設定を変更したら、GRUB設定を再生成します。

Rocky Linux 9などの比較的新しいRHEL系では、

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Enter fullscreen mode Exit fullscreen mode

を使用します。

再起動します。

sudo reboot
Enter fullscreen mode Exit fullscreen mode

GRUBメニューで c を押してコマンドラインを開き、記号キーを確認します。

例えば、

@
:
"
\
_
Enter fullscreen mode Exit fullscreen mode

などがキートップの刻印どおり入力できれば、JIS配列が適用されています。


UEFI環境での注意点

古い記事を見ると、UEFI環境では次のようなファイルへ直接 grub2-mkconfig を実行する例があります。

/boot/efi/EFI/redhat/grub.cfg
Enter fullscreen mode Exit fullscreen mode

Rocky Linuxでは、

/boot/efi/EFI/rocky/grub.cfg
Enter fullscreen mode Exit fullscreen mode

となっている場合があります。

ただし、最近のRHEL系ではUEFI側の grub.cfg は、本体となる /boot/grub2/grub.cfg を読み込むためのスタブとして使用される構成があります。

そのため、Rocky Linux 9などでは基本的に、

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Enter fullscreen mode Exit fullscreen mode

を使用します。

UEFI側の grub.cfg を不用意に直接上書きしない方が安全です。


terminal_input at_keyboard は必要か

GRUBのキーボード設定例では、

insmod at_keyboard
terminal_input at_keyboard
Enter fullscreen mode Exit fullscreen mode

といった設定を見かけることがあります。

ただし、USBキーボードやUEFI環境では、入力経路を明示的に at_keyboard へ切り替えることで、逆にキーボード入力ができなくなる場合があります。

JIS配列へ変更するだけであれば、まずは、

insmod keylayouts
keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

だけで試すのが無難です。

特別な理由がない限り、入力端末そのものを変更する必要はありません。


一時変更と永続変更の違い

整理すると、次のようになります。

方法 設定 再起動後
GRUBコマンドライン insmod keylayoutskeymap jp 元に戻る
40_customへ追加 keymap ${prefix}/layouts/jp.gkb 維持される
Linuxの localectl Linuxコンソールのみ変更 GRUBには影響しない

まず一時変更でテストし、問題がなければ永続設定にするのが安全です。


.gkbを作らずGRUBだけでJIS配列へ変更できるか

基本的にはできません。

GRUB上の keymap コマンドは、あらかじめ作成されたGRUB用キーマップファイルを読み込む仕組みです。

そのため、Linux起動中に、

grub2-kbdcomp
Enter fullscreen mode Exit fullscreen mode

を使って、

jp.gkb
Enter fullscreen mode Exit fullscreen mode

を事前に作っておく必要があります。

最低限、次の準備だけはLinux側で行います。

sudo mkdir -p /boot/grub2/layouts
sudo grub2-kbdcomp -o /boot/grub2/layouts/jp.gkb jp
Enter fullscreen mode Exit fullscreen mode

その後であれば、GRUB起動中にいつでも、

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

と実行して、一時的にJIS配列へ切り替えられます。


まとめ

Rocky LinuxやRHEL系でGRUBを日本語JISキーボード配列にする場合、ポイントは次の3つです。

まずGRUB用のキーマップを作ります。

sudo dnf install grub2-tools-extra kbd
sudo mkdir -p /boot/grub2/layouts
sudo grub2-kbdcomp -o /boot/grub2/layouts/jp.gkb jp
Enter fullscreen mode Exit fullscreen mode

GRUB上で一時的に試す場合は、

insmod keylayouts
keymap jp
Enter fullscreen mode Exit fullscreen mode

とします。

永続化する場合は /etc/grub.d/40_custom に、

insmod keylayouts
keymap ${prefix}/layouts/jp.gkb
Enter fullscreen mode Exit fullscreen mode

を追加し、

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Enter fullscreen mode Exit fullscreen mode

を実行します。

GRUBはLinux起動前に動作するため、localectl set-keymap jp などのLinux側設定とは完全に別です。

GRUB上で頻繁にカーネルパラメータを編集したり、レスキュー操作をしたりする環境では、JISキーボードの設定を入れておくとかなり操作しやすくなります。

Top comments (0)