ssh がサーバー + コマンド引数で実行される場合、どの起動プロファイルが実行されますか?

ssh がサーバー + コマンド引数で実行される場合、どの起動プロファイルが実行されますか?

私は、 がサーバーにログインするために使用されるとき、つまりサーバー引数を使用するときに使用される起動スクリプトの中にあることを/etc/profile知っています。ただし、サーバー + コマンド引数のみを使用するシナリオでは、同じ起動スクリプトが選択されないようです。~/.profilesshsshssh

デフォルトのシェルとして bash があると仮定します。.bashrc存在する場合、このシナリオでそれが選択されることがわかります。しかし、.bashrc存在せず、他の個人プロファイルも存在しない場合、他に何が実行されますか?

環境変数を提供する別のグローバル スクリプトはありますか?

答え1

このページには、おそらくあなたが知りたい以上の情報が記載されていますが、簡潔で優れたリソースです。

基本的に、コンピュータに ssh するときは、「ログイン」シェルを実行していないため、別のファイルがソースとして使用されます。つまり、デフォルトのシェルが /bin/bash (Ubuntu のデフォルト) の場合、ホーム ディレクトリの がソースとして使用されます。関係するその他のファイルについては、 bash のマニュアル ページ ( ) の最後のほうにあるセクション~/.bashrcを確認してください。FILESman bash

また、「対話型」シェルを使用していない場合は、 で終了する行に注意してください.bashrc。これを理解する良い方法は、 に prints を入れて.bashrc、理解できるまで実験を続けることです。

このような:

# ~/.bashrc:

echo "HERE I WAS!"

# Exit here unless an interactive session.
[ -z "$PS1" ] && return

echo "HERE I AM!"

答え2

コマンドがない場合、SSH はログイン シェルを実行します。 の場合bash、 のソース指定.profile(Ubuntu では が のソース指定.bashrc) (および が/etc/profileのソース指定/etc/bash.bashrc) が必要になります。 など、代わりにソース指定できるファイルは他にもあります.bash_profileが、Ubuntu のデフォルトのセットアップには しかありません.profile

$ grep bashrc /etc/profile .profile
/etc/profile:    # The file bash.bashrc already sets the default PS1.
/etc/profile:    if [ -f /etc/bash.bashrc ]; then
/etc/profile:      . /etc/bash.bashrc
.profile:    # include .bashrc if it exists
.profile:    if [ -f "$HOME/.bashrc" ]; then
.profile:   . "$HOME/.bashrc

コマンドで実行する場合、SSHはログインシェルを実行しないので、man bash(セクションINVOCATION):

When an interactive shell that is not a login shell  is  started,  bash
reads  and  executes  commands  from /etc/bash.bashrc and ~/.bashrc, if
these files exist.  This may be inhibited by using the  --norc  option.
The  --rcfile  file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.

しかし、コマンドでは、bash対話的に実行されません。では、なぜ.bashrcソース化されるのでしょうか? 繰り返しますが、from man bash:

Bash attempts to determine when it is being run with its standard input
connected to a network connection, as when executed by the remote shell
daemon, usually rshd,  or  the  secure  shell  daemon  sshd.   If  bash
determines  it  is  being  run  in  this fashion, it reads and executes
commands from ~/.bashrc and ~/.bashrc, if these  files  exist  and  are
readable.  It will not do this if invoked as sh.  The --norc option may
be used to inhibit this behavior, and the --rcfile option may  be  used
to  force  another file to be read, but neither rshd nor sshd generally
invoke the shell with those options or allow them to be specified.

その他のファイルはSSHで読み取ることができます(man ssh、 セクションFILES):

~/.ssh/rc
     Commands in this file are executed by ssh when the user logs in,
     just before the user's shell (or command) is started.  See the
     sshd(8) manual page for more information.
/etc/ssh/sshrc
     Commands in this file are executed by ssh when the user logs in,
     just before the user's shell (or command) is started.  See the
     sshd(8) manual page for more information.

環境変数の場合(man ssh、セクションからENVIRONMENT):

Additionally, ssh reads ~/.ssh/environment, and adds lines of the format
“VARNAME=value” to the environment if the file exists and users are
allowed to change their environment.  For more information, see the
PermitUserEnvironment option in sshd_config(5).

モジュールはpam_envSSH に対して有効になっています:

$ grep pam_env /etc/pam.d/sshd 
# /etc/security/pam_env.conf.
session    required     pam_env.so # [1]
session    required     pam_env.so user_readenv=1 envfile=/etc/default/locale

/etc/environmentしたがって、およびの変数も~/.pam_environment設定されます ( が設定されている/etc/default/localeため、もenvfile設定されます)。ただし、これらのファイルは のようにソース化されていない.profileため、ここでシェル コマンドを使用することはできません。

関連情報