本文を読み飛ばす

The ssh error: failed remote commands by stty in login shell

I used the stty command in .bashrc, it cause ssh errors with no-console.

# nothing for the output

$ ssh host echo 1234

# you can check the warning with `-v` option

$ ssh -vv host echo 1234

Pseudo-terminal will not be allocated because stdin is not a terminal.

The stty command is used to change terminal settings, so it requires a TTY to function.

When it runs in a non-interactive shell without a TTY, it fails and can cause the entire script or SSH session to exit unexpectedly.

To fix this, I commented out the stty commands in my bash::

if false; then
    stty ...
fi

Please check your login script files:

  • .profile
  • .bash_login
  • .bashrc or etc

Finally, I add the follow line before stty command to avoid the this problem.

[[ $- != *i* ]] && return
stty ...

According to man bash:

PS1 is set and $- includes i if bash is interactive,
allowing a shell script or a startup file to
test this state.

Title: Fixing SSH Errors Caused by stty in Your .bashrc

Using the stty command in your .bashrc can cause unexpected ssh errors during non-interactive sessions.

You might notice that a simple remote command produces no output:

Shell

Returns nothing

$ ssh host echo 1234 The clue lies in the verbose output (-v), which mentions that a TTY was not allocated:

Shell

$ ssh -vv host echo 1234 ... Pseudo-terminal will not be allocated because stdin is not a terminal. ...

The Cause: Interactive vs. Non-interactive Shells

The stty command is used to change terminal settings, so it requires a TTY to function. When it runs in a non-interactive shell (which has no TTY), the command fails. This failure can cause the entire startup script or SSH session to exit unexpectedly.

The Solution

A quick fix is to simply disable the stty command in your shell startup file (.bashrc, .profile, etc.):

Shell

Temporarily disable the command

if false; then stty ... fi However, the best practice is to add the following line before the stty command. This ensures it only runs in an interactive session.

Shell

Exit if this is not an interactive shell

[[ $- != i ]] && return

This will now run safely

stty ... This works because, as the bash man page explains, the $- variable contains an i when the shell is interactive:

PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

コメント

Comments powered by Disqus
宣伝: