我知道/etc/profile
,在用于登录服务器~/.profile
时使用的启动脚本中,即使用服务器参数。但是,在我只使用服务器+命令参数的情况下,似乎没有选择相同的启动脚本。ssh
ssh
ssh
假设我使用 bash 作为默认 shell。如果.bashrc
存在,我知道在这种情况下它会被选中。但如果.bashrc
不存在并且没有其他个人配置文件,还会运行什么?
是否有其他提供环境变量的全局脚本?
答案1
这个页面包含的信息可能比你想了解的还要多,但它是一个很棒的简明资源:
基本上,当您通过 ssh 连接到计算机时,您并没有运行“登录”shell,因此会获取不同的文件。简而言之,如果您的默认 shell 是 /bin/bash(Ubuntu 上的默认 shell),那么您将~/.bashrc
在主目录中获取。查看FILES
bash 手册页 ( man bash
) 中靠近末尾的部分,了解其他会用到的文件。
此外,如果您不使用“交互式”shell,请确保在您的命令行中查找.bashrc
退出的行。解决此问题的一个好方法是在您的命令行中输入打印.bashrc
,然后继续尝试,直到您弄清楚为止。
像这样:
# ~/.bashrc:
echo "HERE I WAS!"
# Exit here unless an interactive session.
[ -z "$PS1" ] && return
echo "HERE I AM!"
答案2
SSH 无需命令即可运行登录 shell。对于bash
,这涉及获取.profile
(在 Ubuntu 上,获取.bashrc
)(和/etc/profile
,获取/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 不会运行登录 shell,因此,根据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
无法以交互方式运行。那么为什么要.bashrc
引用呢?再次引用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).
该pam_env
模块已为 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
/etc/environment
因此,和中的变量~/.pam_environment
也将被设置(并且/etc/default/locale
,因为envfile
已设置)。但是,这些文件不是以 的方式获取.profile
的,因此您不能在此处使用 shell 命令。