ssh option to enable tty

SSH option to enable TTY allocation for interactive sessions

Clarified the context by specifying 'TTY allocation' and 'interactive sessions' to improve the relevance of search results related to SSH configuration.

To enable TTY (teletypewriter) allocation in an SSH (Secure Shell) session, you have a couple of options that cater to different scenarios involving interactive sessions on remote servers. Understanding how to manage TTY allocation is essential for executing commands that require user interaction, such as running sudo.

Understanding TTY Allocation in SSH

TTY allocation essentially allows your SSH client to request a pseudo-terminal (PTY) from the remote server. This is particularly important when running interactive applications or scripts that depend on terminal capabilities.

TTY Allocation Scenarios

  1. Interactive Shell Access: When you log into a remote system via SSH, TTY allocation allows you to interact with that session as if you were using a local terminal.
  2. Executing Commands Needing Interactive Features: Certain commands, especially those that use sudo, require a TTY to function correctly.

SSH Options for TTY Allocation

Using the -t Option

The most common way to enable TTY allocation is by using the -t flag:

ssh -t user@remote_host

This command forces SSH to allocate a pseudo-terminal for the session.

Forcing TTY Allocation with -tt

In some situations, particularly when your local terminal setup doesn't provide a TTY (for example, running SSH from a script), you might need to use the -tt option, which forces TTY allocation even if no TTY is present locally:

ssh -tt user@remote_host

Using -tt is particularly useful in automation or scripting environments where "stdin" is not associated with a terminal, allowing you to execute commands that require a terminal interface.

Global Configuration with RequestTTY

If you often connect to a server and require TTY allocation, you can configure this in your SSH client configuration file (typically located at ~/.ssh/config). Here is how you can enforce TTY allocation for specific hosts or globally:

Host remote_host
    RequestTTY force

This configuration ensures that every SSH session initiated with this host will request a TTY automatically.

Practical Examples

Running a Command with Sudo

When you need to run a command that requires sudo privileges, TTY is often needed:

ssh -t user@remote_host "sudo apt update"

Scripting with SSH

If running a script that interacts with the remote server in a loop, you might need to force TTY allocation:

ssh -tt user@remote_host "bash -s" < local_script.sh

Conclusion

Using the SSH options -t or -tt to enable TTY allocation is a straightforward yet essential practice when working with interactive remote sessions or commands requiring terminal-like interaction. Properly configuring TTY settings can facilitate smoother operations, especially in scripts and automated tasks. For regular usage, consider setting defaults in your SSH config file to eliminate the need to specify these flags repeatedly.

For more detailed instructions, you can refer to the Linux man pages which elucidate on SSH command-line options and their implications.

Sources

10
1
tty - What is the benefit of not allocating a terminal in ssh?
Serverfault

Multiple -t options force tty allocation, even if ssh has no local tty. This allows you to get a "shell" of sorts to the remote server.

2
Pseudo-terminal will not be allocated because stdin is ...
Stack Overflow

Try ssh -t -t (or ssh -tt for short) to force pseudo-tty allocation even if stdin isn't a terminal. See also: Terminating SSH session ...

3
Why does SCP fail when "RequestTTY force" option is ...
Unix

It aborts the SCP process. Use ssh -t to force TTY for interactive sessions, instead of forcing it globally using RequestTTY.

4
allow parameter to ask for pseudo tty allocation for SSH ...
GitHub

Pseudo-TTYs are a must to run sudo in a remote interactive shell. The workaround of using <password> | sudo -s <command> violates a commonly ...

5
Super User
Superuser

For an interactive session, the remote SSH server will allocate a PTY (pseudo-TTY). Then it will invoke the user's shell with the PTY as the ...

6
ssh(1) - Linux manual page
Man7

If an interactive session is requested, ssh by default will only request a pseudo-terminal (pty) for interactive sessions when the client has one. The flags -T ...

7
Building interactive SSH applications
Drewdevault

... enables TTY allocation, so that we can do things like curses. This will work if you want to explicitly authorize specific people, one at a ...

8
no tty present and no askpass program specified" error
Simplified

Allocate a pseudo-terminal for the SSH session to provide the necessary tty. · Force sudo to read the password from standard input by using --stdin or -S switch.

9
Running an SSH shell script using an npm command
Community

... interactive sessions. Use the -tt flag to force TTY allocation, enabling interactive operations inside your executed command. This setup ...

10
How to solve Pseudo terminal issue OpenSSH
Perlmonks

You can read ssh for the details: -t Force pseudo-tty allocation. This can be used to exe +cute arbi- trary screen-based programs on a remote ...