简介
使用 ubuntu22.04 ubuntu24.04 这几个版本时候发现,ssh 连接后直接执行命令找不到环境变量
原因在于 用户 .bashrc
最开始有这样一段 bash 脚本
1
2
3
4
5
|
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
|
它的作用是阻止脚本在非交互式环境下执行某些操作
所以 ssh 后直接执行命令 无法执行。
解决办法
注释掉 $HOME/.bashrc
开头的这段代码
1
2
3
4
5
|
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
|
如果额外想要在 ssh 后直接可以读取 /etc/profile.d/*.sh
下的环境变量
可以在 $HOME/.bashrc
添加如下的脚本
1
2
3
4
5
6
7
8
9
|
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
|