25

Is there any way to specify, in .ssh/config, a command like:

ssh -t remote-host "screen -dR screen_name"

so I can easily access the remote screen session with ssh remote-host?

Right now I solved this problem using a custom script:

$ cat ~/bin/sssh 
#!/bin/sh

/usr/bin/ssh -t $1 "screen -dR ab"

~/bin stays first at $PATH, but it is unflexible and ugly.

Also I would like to find way to implement host autocompletion for my custom script.

A B
  • 419
  • I would replace $1 with $@. Then you can add more options, not just the hostname. For completion, try executing . /usr/share/bash-completion/completions/ssh , followed by complete -F _ssh sssh – Alex Stragies Jul 07 '16 at 17:59

4 Answers4

31

To allocate the tty as you would with the -t option, put RequestTTY force as an entry in the ssh config file (usually in ~/.ssh/config). To always execute a command on the server after you've connected, put RemoteCommand COMMAND in the entry, where COMMAND is the command you'd like run upon connection. For other options, check man ssh_config.

Host remote-host
    Hostname my.server.com
    User imauser
    IdentityFile /path/to/identity_file
    RequestTTY force
    RemoteCommand screen -dR screen_name
10

Use the force, Luke!

RequestTTY force in your ~/.ssh/config for the desired host.

1

I'd argue that this is the most flexible way. A script allows you to set up parameters, add conditionals, change the command as needed, all based on the inputs.

There are ways to "force" the command in authorized_keys. But I don't know of a way to force tty allocation except from the command-line or to force a command on the remote server from the local side config files.

Myself, I have a script that scans the config file, checks against DNS with various domains and sets the terminal emulator's title string. I call it from screen on my local system (Ctrl-a g).

Arcege
  • 22,536
1

Note: RemoteCommand starts working with OpenSSH 7.6. In earlier version (ie current debian 9.9) the ssh client will stop working properly (at least with git pull).

pico_prob
  • 131