現在システムにログインしているユーザー名を表示するには、whoami コマンドを使用します。場合によっては、システムにログインして別のユーザーに切り替える必要があり、現在ログインしているユーザーがわからないことがあります。そのような場合は、whoami コマンドを使用して現在のユーザー名を確認できます。 /P>
whoami コマンドを使用して、現在のユーザーに関する情報をすばやく表示できます。 whoami コマンドは、現在のログイン セッションの所有者を表示します:
# whoami root
–help オプションを渡すことで、whoami コマンドの使用可能なオプションを表示できます。
# whoami --help Usage: whoami [OPTION]... Print the user name associated with the current effective user ID. Same as id -un. --help display this help and exit --version output version information and exit GNU coreutils online help: [http://www.gnu.org/software/coreutils/] Full documentation at: [http://www.gnu.org/software/coreutils/whoami] or available locally via: info '(coreutils) whoami invocation'
whoami コマンドの実行中に以下のエラーが発生した場合:
whoami: command not found
ディストリビューションの選択に応じて、以下に示すように coreutils パッケージをインストールしてみてください。
分布 | コマンド |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
アルパイン | apk add coreutils |
Arch Linux | pacman -S coreutils |
カリ Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
whoami コマンドの例
1. 現在ログインしているユーザー名を表示:
# whoami
2. ユーザー ID の変更後にユーザー名を表示します:
$ sudo whoami
結論
whoami コマンドは、現在有効なユーザーの名前を出力します。 sudo コマンドを使用した場合、これはログイン名 (logname の出力) とは異なる場合があります。