在远程登录到Linux服务器时绕过〜/ .profile


32

~/.profile当使用ssh或putty登录到远程Linux服务器时,是否可以绕过或阻止执行?


你为什么想做这个?为什么不只是编辑文件中的值?(毕竟,这是您的个人资料。)
Telemachus

7
例如,如果有人搞砸了.profile文件而您没有直接访问系统的权限,这将非常有用:)
monkey_p 2009年

这正是我在monkey_p中遇到的情况
Andrew Hampton 2009年

Answers:


30

对于bash:

$ ssh hostname "bash --noprofile"

6
-t我认为您还需要选择。
grawity

2
ssh -t主机名“ bash --noprofile”。如果-t不存在,我将收到错误消息“ standard in must be tty”。
尼汀斯2013年

14

如果您正在寻找禁用所有登录脚本,您可以使用--noprofile标志来禁止/etc/profile~/.profile等,--norc以禁用~/.bashrc,像这样:

$ ssh 127.0.0.1 "bash --noprofile --norc"

请记住,如果有可用的外壳,您也可以启动其他外壳。在弄乱以下内容后,我不得不使用它chsh

$ ssh 127.0.0.1 sh

这很可能会使您进入空白外壳(无提示),因此请给它一个ls以确保其正常工作。


4

如果您的目标计算机在bash shell中:

user@host:/$ ssh hostname "bash --noprofile"

另外,如果您想使用其他个人资料

user@host:/$ ssh hostname "bash --noprofile; source ~/.other_profile"

1

另外,尝试使用WinSCP等FTP程序删除错误的登录文件。这将丢弃它,但是至少您应该能够登录到默认外壳


或者只是将其重命名。
jezmck

1

就像其他人提到的那样,在启动连接时运行带有标志的--noprofile将会起作用,尽管如果您使用的是其他Shell,则可能会或可能不会。

一种替代方法是让配置文件脚本本身检测SSH连接并相应地执行操作。由于SSH连接通常会设置许多环境变量,因此可以轻松地进行检查。在个人资料的开头添加以下几行就足够了:

if [ "$SSH_CONNECTION" != "" ]; then
  echo Logging in with ssh
  return
else
  echo Logging in with something that is not ssh
fi

# rest of your profile goes here

return如果$SSH_CONNECTION设置了环境变量,它将跳过脚本的其余部分,通常在启动SSH连接时会创建该变量。否则,配置文件将正常运行。

请注意,这只会跳过受影响的配置文件脚本。/etc/profile除非您进行类似的修改,否则所有其他配置文件脚本(例如:)仍将被处理。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.