ssh가 서버 + 명령 인수로 실행될 때 어떤 시작 프로필이 실행됩니까?

ssh가 서버 + 명령 인수로 실행될 때 어떤 시작 프로필이 실행됩니까?

나는 서버에 로그인하는 데 사용되는 시작 스크립트 중 하나라는 것을 /etc/profile알고 있습니다. 즉, 서버 인수와 함께 사용하는 것입니다. 그러나 서버+명령 인수와 함께 사용하는 시나리오에서는 동일한 시작 스크립트를 선택하지 않는 것 같습니다.~/.profilesshsshssh

기본 쉘로 bash가 있다고 가정합니다. 존재하는 경우 .bashrc이 시나리오에서 선택된다는 것을 알고 있습니다. 하지만 .bashrc존재하지 않고 다른 개인 프로필도 존재하지 않는다면 또 무엇이 실행됩니까?

환경 변수를 제공하는 또 다른 전역 스크립트가 있습니까?

답변1

이 페이지에는 여러분이 알고 싶은 것보다 더 많은 내용이 있지만 매우 간결한 리소스입니다.

기본적으로 컴퓨터에 SSH로 연결하면 '로그인' 셸을 실행하는 것이 아니므로 다른 파일이 소스로 제공됩니다. 간단히 말해서 기본 쉘이 /bin/bash(Ubuntu의 기본값)인 경우 ~/.bashrc홈 디렉토리에서 소스를 사용하게 됩니다. FILESbash 매뉴얼 페이지( man bash)의 끝 부분에서 작동되는 다른 파일을 확인하세요 .

.bashrc또한 '대화형' 쉘을 사용하지 않는 경우 종료되는 행을 주의 깊게 살펴보십시오 . 이 문제를 알아내는 좋은 방법은 인쇄물을 넣고 .bashrc알아낼 때까지 계속 실험하는 것입니다.

이와 같이:

# ~/.bashrc:

echo "HERE I WAS!"

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

echo "HERE I AM!"

답변2

명령이 없으면 SSH는 로그인 셸을 실행합니다. 의 경우 bash소싱 .profile(Ubuntu에서는 source .bashrc)(및 /etc/profilesource /etc/bash.bashrc)이 포함됩니다. 와 같이 대신 소스로 제공될 수 있는 다른 파일이 있지만 .bash_profile기본 Ubuntu 설정에는 .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

명령으로 실행하면 SSH는 로그인 셸을 실행하지 않으므로 다음과 같습니다.man bash(부분 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.

그러나 명령을 사용하면 bash대화식으로 실행되지 않습니다. 그렇다면 왜 .bashrcsourced인가? 다시 한 번 말씀드리자면 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.

다른 파일은 SSH로 읽을 수 있습니다(man ssh, 부분 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.

환경 변수의 경우( man ssh, 섹션 에서 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).

SSH에 대해 모듈 pam_env이 활성화되었습니다.

$ 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

따라서 /etc/environment및 의 변수 ~/.pam_environment도 설정됩니다(및 /etc/default/locale, 이후가 envfile설정됨). 그러나 이러한 파일은 .profile있는 그대로 소스가 제공되지 않으므로 여기서는 쉘 명령을 사용할 수 없습니다.

관련 정보