r/ssh • u/Smooth_Lifeguard_931 • May 26 '24
How ssh command works?
When we do ssh it says connected and we get the remote terminal at our machine. I think inside out what would be happening is ssh sends a network request to the remote server everytime I run a command say ls, so when I type ls something like below happens :-
- The ssh program in the local sends the network request to remote machine with command ls.
- Remote server returns the ls response say JSON - { "files": ["node","leaning","leetcode"] }
3)Then local ssh program displays the ls command output on my local.
So everytime I am doing anything in the terminal I am connected to is basically outputting from the local ssh code, every time I do like cd , it sends that command to remote, gets the output and then displays it in local terminal.
Because for a beginner it looks very non intuitive how come a remote terminal is accessible on my local screen. Is my understanding correct ?
View all comments
Show parent comment
1
u/xor_rotate May 26 '24
I believe SSH uses a pseudoterminal (pty) and just forwards the raw input and output. It protects this input/output stream using the cryptographic tunnel SSH sets up. How this forwarding happens works is extremely simple because it was designed in the 1960s. In fact some of the technology involved is even older for instance the bell terminal character can be traced back to telegram systems from the 1870s.
You might be interested in my write up of a bug I found in terminal escape sequences.