quando o ssh é executado com um argumento de servidor + comando, qual perfil de inicialização é executado, se houver?

quando o ssh é executado com um argumento de servidor + comando, qual perfil de inicialização é executado, se houver?

Eu sei /etc/profilee ~/.profileestou entre os scripts de inicialização usados ​​quando sshé usado para fazer login em um servidor – ou seja, usando ssho argumento do servidor. No entanto, o cenário em que estou usando apenas sshum argumento servidor + comando não parece pegar os mesmos scripts de inicialização.

Suponha que eu tenha o bash como shell padrão. Se .bashrcexistir, sei que é captado neste cenário. Mas se .bashrcnão existe e não existem outros perfis pessoais, o que mais funciona?

Existe outro script global que fornece as variáveis ​​de ambiente?

Responder1

Esta página tem mais do que você provavelmente gostaria de saber, mas é um ótimo recurso conciso:

Basicamente, quando você faz ssh para um computador, você não está executando um shell de 'login', portanto, arquivos diferentes são originados. Resumindo, se o seu shell padrão for /bin/bash (padrão no Ubuntu), você obterá um ~/.bashrcem seu diretório inicial. Verifique a FILESseção da página de manual do bash ( man bash), perto do final, para outros arquivos que entram em jogo.

Além disso, certifique-se de estar atento .bashrcàs linhas que saem se não estiver usando um shell 'interativo'. Uma boa maneira de descobrir essas coisas é colocar impressões no seu .bashrce continuar experimentando até descobrir.

Assim:

# ~/.bashrc:

echo "HERE I WAS!"

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

echo "HERE I AM!"

Responder2

Sem um comando, o SSH executa um shell de login. Para isso bash, isso envolve sourcing .profile(que, no Ubuntu, fontes .bashrc) (e /etc/profile, quais fontes /etc/bash.bashrc). Existem outros arquivos que podem ser obtidos, como .bash_profile, mas uma configuração padrão do Ubuntu possui apenas .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

Quando executado com um comando, o SSH não executa um shell de login, portanto, de acordo comman bash(seção 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.

Porém, com um comando, bashnão está sendo executado de forma interativa. Então por que é .bashrcoriginado? Novamente, 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.

Outros arquivos podem ser lidos por SSH (deman ssh, seção 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 variáveis ​​de ambiente, (de man ssh, seção 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).

O 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

Portanto, variáveis ​​em /etc/environmente ~/.pam_environmenttambém são definidas (e /etc/default/locale, desde envfileé definido). No entanto, esses arquivos não são originados da maneira .profileque são, portanto você não pode usar comandos shell aqui.

informação relacionada