ブログ

Armadillo X1/G3/G3L/640: シェルプロンプトを変更する方法

at_shigehisa.y…
2019年9月1日 15時15分

■ シェルについて

Linuxで最も一般的なシェルは、bash(Bourne-Again Shell)です。 Armadillo X1/G3/G3L/640に採用されているDebian GNU/Linuxでも、標準のシェルとしてbashが採用されています。

現在使われているシェルは、以下のコマンドで分かります。

root@armadillo:~# echo $SHELL
/bin/bash
root@armadillo:~#

■ シェルプロンプトの環境変数

シェルプロンプトは、PS1環境変数で設定されています。

root@armadillo:~# echo "$PS1"
${debian_chroot:+($debian_chroot)}\u@\h:\w\$
root@armadillo:~#

PS1環境変数は、/etc/bash.bashrc中に記載されています。

root@armadillo:~# cat /etc/bash.bashrc
# System-wide .bashrc file for interactive bash(1) shells.
 
# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.
 
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
 
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
 
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi
 
# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 
# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
#    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
#    ;;
#*)
#    ;;
#esac
 
# enable bash completion in interactive shells
#if ! shopt -oq posix; then
#  if [ -f /usr/share/bash-completion/bash_completion ]; then
#    . /usr/share/bash-completion/bash_completion
#  elif [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion
#  fi
#fi
 
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
        function command_not_found_handle {
                # check because c-n-f could've been removed in the meantime
                if [ -x /usr/lib/command-not-found ]; then
                   /usr/lib/command-not-found -- "$1"
                   return $?
                elif [ -x /usr/share/command-not-found/command-not-found ]; then
                   /usr/share/command-not-found/command-not-found -- "$1"
                   return $?
                else
                   printf "%s: command not found\n" "$1" >&2
                   return 127
                fi
        }
fi

■ PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 'の意味

' ' で囲まれた\u@\h:\w\$の部分が、シェルプロンプトとして表示されます。
\u ユーザ名を表示します。
\h ホスト名を表示します。
\w ホームディレクトリを基準にカレントディレクトリを表示します。
\$ \$: rootユーザの場合は#、それ以外のユーザの場合$を表示します。

※ debian_chrootについて

debian_chrootは、chrootコマンドでルートディレクトリを変更した環境の場合に、どの環境を使用中かプロンプトから区別するために使用します。 このためchrootを利用しない場合、debian_chrootは必要ありません。

詳細は、以下を参照してください。

 https://askubuntu.com/questions/372849/what-does-debian-chrootdebian-chroot-do-in-my-terminal-prompt

■ ホスト名の変更

方法1

シェルプロンプトを変更する例として、ここではホスト名を変更してみます。 現在のホスト名は、以下のコマンドで確認できます。

root@armadillo:~# hostname
armadillo

ここでは現在のホスト名がarmadilloであることが分かります。 ホスト名は/etc/hostnameに記述されています。

root@armadillo:~# cat /etc/hostname
armadillo

また、ドメイン名は/etc/hostsに記述されています。

root@armadillo:~# cat /etc/hosts
127.0.0.1       localhost
127.0.0.1       armadillo
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

ホスト名を変更する場合は、/etc/hostnameと/etc/hostsの両方を変更します。 ホスト名をarmadilloからarmadillo-g3に変更した場合の例を以下に示します。

root@armadillo:~# cat /etc/hostname
armadillo-g3
root@armadillo:~# cat /etc/hosts
127.0.0.1       localhost
127.0.0.1       armadillo-g3
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

変更したホスト名は、Armadilloを再起動すると有効になります。

root@armadillo:~# reboot
         Unmounting /opt/license...
[  OK  ] Stopped target Graphical Interface.
         Stopping User Manager for UID 0...
         Stopping Authorization Manager...
         Stopping Session c1 of user root.
[  OK  ] Stopped target Timers.
 
・・・・・・・・・・
 
Debian GNU/Linux 9 armadillo-g3 ttymxc4
 
armadillo-g3 login: root
Password:

また、以下のコマンドを実行することで、変更したホスト名を一時的に有効にできます。

root@armadillo:~# hostname -F /etc/hostname
root@armadillo:~# hostname -f
armadillo-g3

方法2

ホスト名は、hostnamectlでも変更できます。

root@armadillo-g3:~# hostnamectl set-hostname armadillo
IPVS: Creating netns size=912 id=3
root@armadillo-g3:~# cat /etc/hostname
armadillo

■ その他のシェルプロンプトの変更について

bashシェルのプロンプトは、設定ファイル(.bashrc)でも変更できます。

root@armadillo-g3:~# cat .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
 
# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 
# umask 022
 
# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'

例えば、シェルプロンプトに日付を表示させたい場合は、PS1環境変数のコメントを外し、\dを追加します。

root@armadillo-g3:~# cat .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
 
# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w \d\$ '
 
# umask 022
 
# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'

上記編集が完了したら、以下のコマンドで.bashrcを実行します。

root@armadillo:~# source .bashrc
root@armadillo:~ Sun Sep 01#

bashシェルのプロンプトに設定できる変数は、以下を参照して下さい。

Bash Reference Manual (6.9 Controlling the Prompt)

 https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Controlling-the-Prompt