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 email@example.com # ..... firstname.lastname@example.org:~$ 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 email@example.com apt search beanstalk
Sadly, this doesn't always yield the results expected. Colour disappears from the output, and sometimes things like
ssh firstname.lastname@example.org htop) and
ssh email@example.com 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 firstname.lastname@example.org 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!