cuando se ejecuta ssh con un argumento servidor + comando, ¿qué perfil de inicio se ejecuta, si corresponde?

cuando se ejecuta ssh con un argumento servidor + comando, ¿qué perfil de inicio se ejecuta, si corresponde?

Lo sé /etc/profiley ~/.profileestoy entre los scripts de inicio que sshse utilizan para iniciar sesión en un servidor, es decir, sshcon el argumento del servidor. Sin embargo, el escenario en el que solo estoy usando sshun argumento de servidor+comando no parece recoger los mismos scripts de inicio.

Supongamos que tengo bash como shell predeterminado. Si .bashrcexiste, sé que se recoge en este escenario. Pero si .bashrcno existe y no existen otros perfiles personales, ¿qué más funciona?

¿Existe otro script global que proporcione las variables de entorno?

Respuesta1

Esta página tiene más de lo que probablemente quieras saber al respecto, pero es un gran recurso conciso:

Básicamente, cuando accedes por ssh a una computadora, no estás ejecutando un shell de 'inicio de sesión', por lo que se obtienen archivos diferentes. En resumen, si su shell predeterminado es /bin/bash (predeterminado en Ubuntu), obtendrá un ~/.bashrcen su directorio de inicio. Consulte la FILESsección de la página de manual de bash ( man bash), cerca del final, para ver otros archivos que entran en juego.

Además, asegúrese de estar atento .bashrca las líneas que salen si no utiliza un shell "interactivo". Una buena manera de resolver esto es poner impresiones en tu .bashrcy seguir experimentando hasta que lo resuelvas.

Como esto:

# ~/.bashrc:

echo "HERE I WAS!"

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

echo "HERE I AM!"

Respuesta2

Sin un comando, SSH ejecuta un shell de inicio de sesión. Para bash, eso implica abastecimiento .profile(que, en Ubuntu, obtiene .bashrc) (y /etc/profile, que obtiene /etc/bash.bashrc). Hay otros archivos que podrían obtenerse en su lugar, como .bash_profile, pero una configuración predeterminada de Ubuntu solo tiene .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

Cuando se ejecuta con un comando, SSH no ejecuta un shell de inicio de sesión, por lo que, segúnman bash(sección 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.

Sin embargo, con un comando, bashno se ejecuta de forma interactiva. Entonces, ¿por qué se .bashrcobtiene? De nuevo, de 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.

Se pueden leer otros archivos mediante SSH (desdeman ssh, sección 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.

Para variables de entorno, (de man ssh, sección 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).

El pam_envmódulo está habilitado para SSH:

$ 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

Por lo tanto, las variables en /etc/environmenty ~/.pam_environmenttambién se establecen (y /etc/default/locale, desde envfilese establecen). Sin embargo, estos archivos no se obtienen de la forma .profileen que están, por lo que no puede utilizar comandos de shell aquí.

información relacionada