ssh远程执行找不到命令(交互式和非交互式shell)—— 筑梦之路

2023-12-13 12:49:54

背景说明

ssh远程某台主机去执行命令时提示找不到命令,而手动登陆主机可以正常执行命令。

问题原因

  • An interactive shell reads user input typed on a keyboard. Commands run in the current shell and the system waits for further instructions from the user. A Bash shell is an interactive shell.

  • A non-interactive shell executes commands read from a file in a new subshell. The shell executes commands from the file and exits. Init, startup, and executed shell scripts start non-interactive shells.

使用xshell这样的工具登陆服务器后就是交互式shell,如果在当前shell中再启动一个shell,这个子shell就是非交互式shell

如何区别交互式和非交互式shell

$ ssh test@127.0.0.1 'echo $0'
bash

$ echo $0
-bash

-bash的输出就是交互式shell,反之就是非交互式shell
  • .bashrc defines the settings for a user when running a subshell. Add custom configurations to this file to make parameters available in subshells for a specific user.

  • .bash_profile defines the settings for a user when running a login shell. Add custom configurations to this file to make parameters available to a specific user when running a login shell.

?交互式shell会执行 .bash_profile(其中包含了.bashrc),而非交互式shell只会执行.bashrc

简单地来说,交互式shell和非交互式shell使用的环境变量文件不同,因此远程执行命令的时候会出现找不到命令的情况。

文章来源:https://blog.csdn.net/qq_34777982/article/details/134968197
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。