You can use
tmux for this (
apt install tmux). First find a tutorial you like to get the hang of how it works generally (it is pretty popular amongst linux users and has been around for more than a decade, so there's plenty of stuff online).1
One of the first things you will learn is how to create, detach, and join sessions.
tmux new-session -s foo
Starts a session called
foo (this can be anything). If you do this directly on the Pi, obviously enough this will be foreground on the console (starting it in the foreground, ie., on the local display, remotely, is possible, but that's another question...).
You can then SSH in and:
tmux attach-session -t foo
Notice the switch is here is
-s (the primary documentation can be found in
man tmux). Anyway, whatever you do in one, will happen simultaneously in the other. You can try this out just using two local terminals.
If you're familiar with tmux this may at a glance look like I split one session into two panes (this is part of its functionality) but such is not the case: that's the same single pane session in two terminal windows.
You don't see the invocation (
tmux new-session and
tmux attach-session) because when it starts it takes the whole terminal view. The green bar along the bottom is part of tmux, set that way in
~/.tmux.conf (you'll find examples of such online).
- Including this, by one of our members, from our now abandoned blog. The previous part of that is about how to install tmux from source, which I strongly recommend against. The package in current Raspbian is just fine.