왜 SSH 원격 명령이 수동으로 실행될 때 더 적은 환경 변수를 얻습니까? [닫은

StackOverflow https://stackoverflow.com/questions/216202

  •  03-07-2019
  •  | 
  •  

문제

시스템에 SSH를 사용하고 실행하면 정상으로 실행되는 명령이 있지만 원격 SSH 명령을 사용하여 실행하려고 할 때 다음과 같은 명령이 실행됩니다.

ssh user@IP <command>

두 가지 방법을 사용하여 "ENV"의 출력을 비교하면 다른 환경에서 고정됩니다. 기계에 수동으로 로그인하고 ENV를 실행하면 실행할 때 훨씬 더 많은 환경 변수를 얻습니다.

ssh user@IP "env"

이유가 있습니까?

도움이 되었습니까?

해결책

다른 유형의 껍질이 있습니다. SSH 명령 실행 쉘은 비회적 쉘이지만 일반 쉘은 로그인 쉘 또는 대화식 쉘입니다. 설명은 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.

다른 팁

명령을 실행하기 전에 프로필을 소싱하는 것은 어떻습니까?

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

당신은 그것을 바꾸는 것이 가장 좋을 것입니다 ~/.bash_profile, ~/.bashrc, 또는 무엇이든.

(처럼 여기 (linuxquestions.org))

원격 SSH 명령을 실행할 때 쉘 환경이로드되지 않습니다. SSH 환경 파일을 편집 할 수 있습니다.

vi ~/.ssh/environment

형식은 다음과 같습니다.

VAR1=VALUE1
VAR2=VALUE2

또한 CermitUserEnvironment = Yest 옵션에 대한 SSHD 구성을 확인하십시오.

나는 비슷한 문제가 있었지만 결국 ~/.bashrc가 필요한 전부라는 것을 알았습니다.

그러나 우분투에서는 처리를 중지하는 줄에 댓글을 달아야했습니다 ~/.bashrc :

#If not running interactively, don't do anything
[ -z "$PS1" ] && return

이 문제에 대한 쉬운 해결책은 대상 시스템에서 실행하려는 스크립트의 상단에 소스 /etc /프로파일을 추가하는 것이 었습니다. 여기서 시스템에서 Script.sh가 필요한 환경 변수는 로그인 쉘에서 실행되는 것처럼 구성되었습니다.

이전 응답 중 하나에서 ~/.bashr_profile 등을 사용하는 것이 제안되었습니다. 나는 이것에 많은 시간을 소비하지 않았지만, 이것의 문제는 당신이 로그인하는 소스 시스템의 쉘과 다른 대상 시스템의 다른 사용자에게 ssh를 ssh하는 것입니다. ~.

~/.bashrc의 비 중과 쉘에 대한 점검 위에 원하는 환경 변수를 내 보냅니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top