LoginSignup
3
6

More than 5 years have passed since last update.

sshによるリモートコマンド実行時の環境変数について

Posted at

sshによるリモートコマンド実行時にsshでログインした際には適用される環境変数が適用されてなくて困ったので、適用されない理由を調べた。あってるかは不明なので間違ってたらこっそり教えて下さい。

まずsshのmanには以下の一文が記載されている。

If command is specified, it is executed on the remote host instead of a login shell.

sshでコマンドを指定して実行すると ログインシェルの代わり に実行されると書かれている。

How do I set $PATH such that ssh user@host command works?

ログインシェルと非ログインシェル及び対話的かどうかで初期化ファイルの読み込みに差異が出てくる。
下記にその違いを簡単に記載する(bashを例に)。

ログイン

  • システムログイン時に実行されるシェル
  • 必要な設定やログイン時に実行したい環境・設定を構築するのに使用される
  • bashの場合実行時に/etc/profile及び~/.bash_profileを読み込む

非ログイン

  • ログイン以外のシェルやログオンユーザに紐づいてないプロセスから起動されているシェル

対話シェル

  • 実行するコマンドはキーボードなどのユーザ入力を受け取ることが可能
  • ユーザへ入力を促すことが出来る
  • bashの場合実行時に~/.bashrcを読み込む

非対話シェル

  • プロセスから実行されているため、ユーザからの入力や出力をユーザに見せるといったことができない

Interactive: As the term implies: Interactive means that the commands are run with user-interaction from keyboard. E.g. the shell can prompt the user to enter input.

Non-interactive: the shell is probably run from an automated process so it can't assume if can request input or that someone will see the output. E.g Maybe it is best to write output to a log-file.

Login: Means that the shell is run as part of the login of the user to the system. Typically used to do any configuration that a user needs/wants to establish his work-environment.

Non-login: Any other shell run by the user after logging on, or which is run by any automated process which is not coupled to a logged in user.

What is the difference between interactive shells, login shells, non-login shell and their use cases? [duplicate]

sshのリモートコマンド実行の場合、ログインシェルが実行されないためシェルがログイン時に実行する各種初期化が動かない(非ログイン・非対話のシェル)ため、環境変数がログイン時とはことなる状態になっている(のだと思う).

3
6
0

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
3
6