Dlaczego polecenie zdalne SSH pobiera mniej zmiennych środowiskowych, gdy jest uruchamiane ręcznie? [zamknięte]

Mam polecenie, które działa dobrze, jeśli ssh do komputera i uruchomić go, ale nie działa, gdy próbuję uruchomić go za pomocą zdalnego polecenia ssh jak:

ssh user@IP <command>

Porównanie wyjścia " env " przy użyciu obu metod resutls w różnych środowiskach. Kiedy ręcznie loguję się do komputera i uruchamiam env, dostaję znacznie więcej zmiennych środowiskowych niż podczas uruchamiania :

ssh user@IP "env"
Wiesz dlaczego ?
Author: Good Person, 2008-10-19

6 answers

Istnieją różne rodzaje muszli. Powłoka wykonująca polecenia SSH jest powłoką nieinteraktywną, podczas gdy normalna powłoka jest powłoką logowania lub powłoką interaktywną. Opis Pochodzi Z man bash:

       A  login  shell  is  one whose first character of argument
       zero is a -, or one started with the --login option.

       An interactive shell is  one  started  without  non-option
       arguments  and  without the -c option whose standard input
       and error are both connected to terminals  (as  determined
       by  isatty(3)), or one started with the -i option.  PS1 is
       set and $- includes i if bash is interactive,  allowing  a
       shell script or a startup file to test this state.

       The  following  paragraphs  describe how bash executes its
       startup files.  If any of the files exist  but  cannot  be
       read,  bash reports an error.  Tildes are expanded in file
       names as described below  under  Tilde  Expansion  in  the
       EXPANSION section.

       When  bash is invoked as an interactive login shell, or as
       a non-interactive shell with the --login option, it  first
       reads and executes commands from the file /etc/profile, if
       that file exists.  After reading that file, it  looks  for
       ~/.bash_profile,  ~/.bash_login,  and  ~/.profile, in that
       order, and reads and executes commands from the first  one
       that  exists  and is readable.  The --noprofile option may
       be used when the shell is started to inhibit  this  behav­
       ior.

       When a login shell exits, bash reads and executes commands
       from the file ~/.bash_logout, if it exists.

       When an interactive shell that is not  a  login  shell  is
       started,  bash reads and executes commands from ~/.bashrc,
       if that file exists.  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
       ~/.bashrc.

       When  bash  is  started  non-interactively, to run a shell
       script, for example, it looks for the variable BASH_ENV in
       the  environment,  expands  its value if it appears there,
       and uses the expanded value as the name of a file to  read
       and  execute.   Bash  behaves  as if the following command
       were executed:
              if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
       but the value of the PATH variable is not used  to  search
       for the file name.

 144
Author: Vinko Vrsalovic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-19 09:20:46

Co powiesz na pozyskanie profilu przed uruchomieniem polecenia?

ssh user@host "source /etc/profile; /path/script.sh"

Może się okazać, że najlepiej zmienić to na ~/.bash_profile, ~/.bashrc, albo cokolwiek.

(jako tutaj (linuxquestions.org))

 101
Author: Ian Vaughan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-01 08:44:24

Środowisko powłoki nie ładuje się podczas uruchamiania zdalnego polecenia ssh. Możesz edytować plik środowiskowy ssh:

vi ~/.ssh/environment

Jego format to:

VAR1=VALUE1
VAR2=VALUE2

Sprawdź również konfigurację sshd dla opcji PermitUserEnvironment=yes.

 70
Author: dpedro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-03 12:52:24

Miałem podobny problem, ale w końcu dowiedziałem się, że~/.bashrc był wszystkim czego potrzebowałem.

Jednak w Ubuntu musiałem skomentować linię, która przestaje przetwarzać~/.bashrc:

#If not running interactively, don't do anything
[ -z "$PS1" ] && return
 51
Author: tomaszbak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-26 18:56:00

Znalazłem łatwe rozwiązanie tego problemu było dodanie źródło / etc / profile na szczyt script.sh plik, który próbowałem uruchomić na systemie docelowym. Na tutejszych systemach, to spowodowało zmienne środowiskowe, które były potrzebne przez script.sh być skonfigurowane tak, jakby uruchamiało się z powłoki logowania.

W jednej z wcześniejszych odpowiedzi zasugerowano, że~/.bashr_profile itp... być używane. Nie spędziłem wiele czasu na tym, ale problem z tym polega na tym, że jeśli ssh do innego użytkownika na system docelowy niż powłoka na systemie źródłowym, z którego się logujesz, wydawało mi się, że powoduje to użycie nazwy użytkownika systemu źródłowego dla ~.

 4
Author: Chuck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-05 19:13:53

Po prostu wyeksportuj zmienne środowiskowe, które chcesz powyżej sprawdzenia nieinteraktywnej powłoki w~/.bashrc.

 3
Author: Michael MacDonald,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-15 06:27:13