Skip to content

Ubuntu を日本語環境にする

(準備) vi をインストールする

  • vi くらいないとコンフィグいじるのも大変なので入れておく
bash
sudo apt install -y vim

キーマップを日本語キーボードに変更する

  • SSH クライアントソフト等で接続する分には問題ないが、tty 端末を直接操作する場合はキーボードのマッピングがずれていることがある
  • 以下のコマンドでキーボードの種類と地域を選択することで、正しいマッピングを設定する。
bash
sudo dpkg-reconfigure keyboard-configuration
  • 以下のような感じでリストが表示されるので、数値を入力して選択する

    • [more] が表示されて止まったら Enter で続きを表示する
    • 別途 dialog をインストールすると、リスト表示ではなくテキストベースのダイアログ画面から選択する UI を利用できる
  • 以下を選択

    • Keyboard model ... 72 (Generic 105-key PC)
    Please select the model of the keyboard of this machine.
        : (途中略)
      70. Generic 104-key PC
      71. Generic 104-key PC with L-shaped Enter key
      72. Generic 105-key PC
      73. Generic 86-key PC
      74. Genius Comfy KB-12e
        : (途中略)
    Keyboard model: 72
    • Country of origin for the keyboard ... 56 (Japanese)
    The layout of keyboards varies per country, with some countries having multiple common layouts. Please select the country of origin for the keyboard of this computer.
        : (途中略)
      5. Arabic (Egypt)                              55. Italian
      6. Arabic (Iraq)                               56. Japanese
      7. Arabic (Morocco)                            57. Kazakh
        : (途中略)
    Country of origin for the keyboard: 56
    • Keyboard layout ... 1 (Japanese)
    Please select the layout matching the keyboard for this machine.
    
      1. Japanese                       4. Japanese - Japanese (Kana)
      2. Japanese - Japanese (Dvorak)   5. Japanese - Japanese (Macintosh)
      3. Japanese - Japanese (Kana 86)  6. Japanese - Japanese (OADG 109A)
    Keyboard layout: 1
    • Key to function as AltGr ... 1 (The default for the keyboard layout)
    With some keyboard layouts, AltGr is a modifier key used to input some characters, primarily ones that are unusual for the language of the keyboard layout, such as foreign currency symbols and accented letters. These are often printed as an extra symbol on keys.
    
      1. The default for the keyboard layout  5. Right Logo key  9. Keypad Enter key
      2. No AltGr key                         6. Menu key        10. Both Logo keys
      3. Right Alt (AltGr)                    7. Left Alt        11. Both Alt keys
      4. Right Control                        8. Left Logo key
    Key to function as AltGr: 1
    • Compose key ... 1 (No compose key)
    The Compose key (known also as Multi_key) causes the computer to interpret the next few keystrokes as a combination in order to produce a character not found on the keyboard.
    
    On the text console the Compose key does not work in Unicode mode. If not in Unicode mode, regardless of what you choose here, you can always also use the Control+period combination as a Compose key.
    
      1. No compose key     3. Right Control   5. Menu key       7. Caps Lock
      2. Right Alt (AltGr)  4. Right Logo key  6. Left Logo key
    Compose key: 1
    
    Your console font configuration will be updated the next time your system boots. If you want to update it now, run 'setupcon' from a virtual console.
    update-initramfs: deferring update (trigger activated)
    Processing triggers for initramfs-tools (0.142ubuntu25.5) ...
    update-initramfs: Generating /boot/initrd.img-6.8.0-71-generic
  • 最後に再起動する

    • たぶん何らかのファイルを読ませれば再起動が不要になる (未調査)
    bash
    sudo systemctl reboot

ロケールを日本に変更

  • 現在のロケールを確認する
bash
locale
  • ロケールの確認結果

    • 初期状態はすべて C.UTF-8 (C ロケールをデフォルトロケール、POSIX ロケールという)
    LANG=C.UTF-8
    LANGUAGE=
    LC_CTYPE="C.UTF-8"
    LC_NUMERIC="C.UTF-8"
    LC_TIME="C.UTF-8"
    LC_COLLATE="C.UTF-8"
    LC_MONETARY="C.UTF-8"
    LC_MESSAGES="C.UTF-8"
    LC_PAPER="C.UTF-8"
    LC_NAME="C.UTF-8"
    LC_ADDRESS="C.UTF-8"
    LC_TELEPHONE="C.UTF-8"
    LC_MEASUREMENT="C.UTF-8"
    LC_IDENTIFICATION="C.UTF-8"
    LC_ALL=
  • ロケール一覧をインストール

bash
sudo apt install -y locales
  • ロケールを日本に設定
    • ロケールの設定を、あえて古い dpkg-reconfigure で実施する
    • 最小構成インストールしているせいか、 localectl set-locale がうまく動きませんでした
bash
sudo dpkg-reconfigure locales
  • 以下のような感じでリストが表示されるので、数値を入力して選択する

    • [more] が表示されて止まったら Enter で続きを表示する
    • 別途 dialog をインストールすると、リスト表示ではなくテキストベースのダイアログ画面から選択する UI を利用できる
  • 以下を選択

    • Locales ... 169 (ja_JP.UTF-8 UTF-8)
    Locales are a framework to switch between multiple languages and allow users to use their language, country, characters, collation order, etc.
    
    Please choose which locales to generate. UTF-8 locales should be chosen by default, particularly for new installations. Other character sets may be useful for backwards compatibility with older systems and software.
        : (途中略)
      59. chr_US UTF-8          168. iu_CA UTF-8             277. sv_SE.UTF-8 UTF-8
      60. ckb_IQ UTF-8          169. ja_JP.UTF-8 UTF-8       278. sw_KE UTF-8
      61. cmn_TW UTF-8          170. ka_GE.UTF-8 UTF-8       279. sw_TZ UTF-8
        : (途中略)
    (Enter the items or ranges you want to select, separated by spaces.)
    
    Locales to be generated: 169
    • Default locale for the system environment ... 3 (ja_JP.UTF-8)
      • root を含め全ユーザが日本語環境になる
      • tty 端末ではデフォルト状態だと日本語が文字化けするので注意が必要
    Many packages in Debian use locales to display text in the correct language for the user. You can choose a default locale for the system from the
    generated locales.
    
    This will select the default language for the entire system. If this system is a multi-user system where not all users are able to speak the default
    language, they will experience difficulties.
    
        1. None  2. C.UTF-8  3. ja_JP.UTF-8
    
    Default locale for the system environment: 3
    
    Generating locales (this might take a while)...
    ja_JP.UTF-8... done
    Generation complete.
  • ログインしなおす

bash
exit

fbterm で tty 端末でも日本語表示する

  • こちらのサイトが詳しいです。

  • 通常の tty 端末で使用するシステムフォントは日本語に対応しておらず、ロケールを日本に設定した後に表示メッセージ等が文字化けすることがある

    • システムフォントは GUI 環境で通常利用しているベクタ形式の .ttf (TrueType Font) や .otf (OpenType Font) といったものではなく、ビットマップ形式の .psf (PC Screen Font) というファイルになる
  • 以下の手順で tty 端末上でフレームバッファ端末を導入し、.ttf や .otf 形式のフォントで日本語を表示できるようにする

  • 日本語フォントと fbterm および gpm をインストールする

bash
sudo apt install -y fonts-noto-cjk fbterm gpm
  • 一般ユーザで fbterm を起動できるよう、実行ファイルに SetUID を付与する
bash
sudo chmod u+s /usr/bin/fbterm
  • fbterm はフレームバッファの物理メモリを読み書きするため、利用するユーザに video グループを追加する
bash
sudo usermod -aG video (ユーザ名)
  • ログアウトしてログインしなおす
bash
exit
  • 一度 fbterm を実行してみる
    • これにより、実行ユーザのホーム配下に .fbtermrc が作成される
bash
fbterm
exit
  • fbterm の設定を変更する
bash
vi ~/.fbtermrc
  • 使用するフォント名などを変更できる

    • 例として、フォントサイズが小さかったため 16 に変更
    font-size=16
  • 以上で、fbterm を実行中は日本語表示ができるようになる