wenn SSH mit einem Server + Befehlsargument ausgeführt wird, welches Startprofil wird ggf. ausgeführt?

wenn SSH mit einem Server + Befehlsargument ausgeführt wird, welches Startprofil wird ggf. ausgeführt?

Ich kenne die Startskripte /etc/profileund sie gehören zu den Startskripten, die verwendet werden, wenn zur Anmeldung bei einem Server verwendet wird – also mit Serverargument. Das Szenario, in dem ich nur mit einem Server+Befehlsargument verwende, scheint jedoch nicht dieselben Startskripte zu verwenden.~/.profilesshsshssh

Angenommen, ich habe Bash als Standard-Shell. Wenn .bashrces existiert, weiß ich, dass es in diesem Szenario ausgewählt wird. Aber wenn .bashrces nicht existiert und keine anderen persönlichen Profile vorhanden sind, was wird dann sonst ausgeführt?

Gibt es ein anderes globales Skript, das die Umgebungsvariablen bereitstellt?

Antwort1

Auf dieser Seite erfahren Sie mehr darüber, als Sie wahrscheinlich wissen möchten, aber es handelt sich um eine großartige, prägnante Informationsquelle:

Wenn Sie per SSH auf einen Computer zugreifen, führen Sie grundsätzlich keine „Login“-Shell aus, daher werden andere Dateien als Quelle verwendet. Kurz gesagt, wenn Ihre Standard-Shell /bin/bash ist (Standard unter Ubuntu), wird eine Quelle ~/.bashrcin Ihrem Home-Verzeichnis verwendet. Weitere Dateien, die eine Rolle spielen, finden Sie im FILESAbschnitt der Bash-Manpage ( man bash) am Ende.

Achten Sie außerdem darauf, dass Sie in Ihrem nach Zeilen Ausschau halten, .bashrcdie beendet werden, wenn Sie keine „interaktive“ Shell verwenden. Eine gute Möglichkeit, dies herauszufinden, besteht darin, Ausdrucke in Ihrem einzugeben .bashrcund weiter zu experimentieren, bis Sie es herausgefunden haben.

So was:

# ~/.bashrc:

echo "HERE I WAS!"

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

echo "HERE I AM!"

Antwort2

Ohne Befehl führt SSH eine Login-Shell aus. Für bash, das beinhaltet die Quellenangabe .profile(die unter Ubuntu Quellen von .bashrc) (und /etc/profile, die Quellen von /etc/bash.bashrc). Es gibt andere Dateien, die stattdessen als Quelle verwendet werden könnten, wie .bash_profile, aber ein Standard-Ubuntu-Setup hat nur .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

Wenn SSH mit einem Befehl ausgeführt wird, führt es keine Login-Shell aus.man bash(Abschnitt 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.

Mit einem Befehl wird jedoch bashnicht interaktiv ausgeführt. Warum wird dann .bashrceine Quelle verwendet? Nochmals, von 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.

Andere Dateien können per SSH gelesen werden (vonman ssh, Abschnitt 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.

Für Umgebungsvariablen (aus man ssh, Abschnitt 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).

Das pam_envModul ist für SSH aktiviert:

$ 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

Daher werden auch die Variablen in /etc/environmentund ~/.pam_environmentgesetzt (und /etc/default/locale, da envfilegesetzt ist). Diese Dateien werden jedoch nicht wie .profilein verwendet, daher können Sie hier keine Shell-Befehle verwenden.

verwandte Informationen