LoginSignup
0
0

More than 5 years have passed since last update.

プログラムからSSH接続したときに.bashrcが読み込まれないときの対処

Posted at

NodeJSで書いたプログラムから外部サーバにSSHで接続してコマンドを実行したいと思ったのですが、普通にSSH接続するときに使えるエイリアスが、プログラムからのアクセスだと使えない、ということがありました。
ライブラリのバグかなーと思い、 ssh2 のIssueを漁っていた所、こんなものを発見。
(引用中のカッコ内は私が翻訳したものです)

You may have to check your .bashrc. At least on Ubuntu, .bashrc will exit early if it detects that it's not being run in interactive mode:
(あなたのサーバの.bashrcを読んでみてね。Ubuntuなどでは、.bashrcの最初の方でシェルが対話的に実行されてるかどうかをチェックしてます。)

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

You could try just setting PS1 before sourceing .bashrc:
(PS1という変数にセットしてから.bashrcを再読込してみたらどうかな)

conn.exec("PS1='\u@\h:\w\$ ' source ~/.bashrc; echo Hooray", function(err, stream) {
  // ...
});

ライブラリのバグではなく、.bashrc上でインタラクティブシェルかどうかの判定をしており、それに引っかかっていたということのようです。
ほとんどデフォルトのままで運用していたため気づきませんでした……ちゃんと読まないとだめですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0