0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

suとsu -の違いについて

Posted at

環境

Ubuntu20.4

はじめに

susu ‐ の違いについて知りたければ、ググると沢山の記事がでてきます。迷ったときはググればいいだけなのですが、本当に、記事内容の通りの動きになっているのか、自分で手を動かして確かめてみることは重要です。記事の内容を鵜呑みにすることなく、自分で手を動かして確認することで、本当の理解へとつながるものです。

違いについては、一言でいえばこの通りです。

$su - testuser   # 環境変数を引き継ぎません
$su testuser     # 環境変数を引き継ぎます

やったこと

適当なユーザーを新規に作成して、パスワードも設定します。

$sudo useradd testuser
$sudo passwd testuser

TESTという環境変数が登録されていないことを確認します。

$printenv | grep TEST

TESTという環境変数を作成して、01という値をセットします。

$export TEST=01
$echo $TEST

testuserユーザーに切り替えます。

$su - testuser

TEST環境変数は引き継がれていません。

$echo $TEST
(空白)

再度、testuserユーザーに切り替えます。

$su testuser

TEST環境変数が表示されます。環境変数が引き継がれているということです。

$echo $TEST
01

:smile:はい!よくわかりました。

後始末

テストのために作成したユーザーは削除します。TEST環境変数は、シャットダウンすると消えるのでそのままにしておいてOKです。

$sudo userdel testuser
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?