关于ssh client pseudo-tty模式(.bashrc 在-t情况下不加载)

October 25th, 2011 | Tags:

今天遇到两例ssh pseudo-tty的使用,都值得记录一下。

一是在使用mindterm的时候,需要借助跳板机, 流程如下:

<laptop(mindterm)> ssh to   <jump box> ssh to <target host>

这种情况下,使用pseudo-tty就是必须的  (-t),否则 <jump box>上的ssh没法进行交互。根据ssh 的manual :

-t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a             remote machine, which can be very useful, e.g. when implementing menu services.  Multiple -t options             force tty allocation, even if ssh has no local tty.

如果需要远程运行基于screen-based的程序,如ssh -t -l root targethost “vim”,则这个 -t 是必不可少的。
第二例是caifeng遇到的诡异问题。他需要远程到target host A执行一些脚本,发现当不使用-t的时候,系统每次都会自动加载.bashrc 中的内容,造成脚本运行异常。如果使用-t参数,ssh 过去的session就是忽略掉.bashrc。 从而脚本正常运行。
首先看bash manual:

–norc     Do not read and execute the personal initialization file ~/.bashrc if  the  shell  is  interactive.

This option is on by default if the shell is invoked as sh.

也就是当bash由sh来调用的,那么默认将不加载.bashrc

而这个问题就取决于bash的调用方式:

  • 1, rshd: remote shell daemon调用bash, ( 不使用pseudo-tty,直接远程执行命令,就是这种方式)
  • 2, sh调用bash。(使用pseudo-tty情况下,将会由sh调用bash)

 

No comments yet.
*