How to quickly run TUI programs via SSH
Hello, and welcome to another blog post! I hope everyone had a lovely and restful Easter.
Very often, I want to run a command on a remote machine via SSH and leave it in a terminal in 1 corner of my screen whilst I work in another terminal on that same machine.
Up until now, I've always SSHed into the machine in question and then run the command manually:
user@local:~$ ssh firstname.lastname@example.org # ..... email@example.com:~$ sudo htop
This is fine, but it takes a moment to connect & setup the terminal on the remote end. What if there was a way to specify the command to run remotely?
Well, it turns out there is. SSH lets you specify the command to run on the remote server instead of the default shell:
ssh firstname.lastname@example.org apt search beanstalk
Sadly, this doesn't always yield the results expected. Colour disappears from the output, and sometimes things like
ssh email@example.com htop) and
ssh firstname.lastname@example.org sudo apt update) break altogether:
Error opening terminal: unknown.
I can't remember how I figured it out, but I discovered that the issue is that when you specify the command instead of letting the default shell initialise, it treats it as some sort of 'script-mode', and doesn't allocate a pseudo-terminal on the remote machine.
Thankfully, there's a way to force it to allocate a pseudo-terminal. This is done with the
ssh -t email@example.com sudo htop
This then enables interactive commands to work as intended, and causes colour to be displayed again :D
Found this useful? Got another great SSH tip? Comment below!