I have a linux box in the office I want to ssh into from my Macbook at home. I can’t reach the box directly on port 22, but I have a port tunnel nailed up between localhost:23001 and port 22 on the remote linux box. This works for a regular ssh session:
$ ssh -t -2 -A -p 23001 [email protected]
The next step is, I want to automatically run my terminal session inside a screen session. It almost works to do:
$ ssh -t -2 -A -p 23001 [email protected] screen -R
If I don’t have a current screen session, it creates one. If there’s already a detached screen, it connects to it. So far, so good. But, the problem is, I never get a login shell, so my .profile never gets run and my environment doesn’t get set up. So, I went one step further, and tried:
$ ssh -t -2 -A -p 23001 [email protected] bash -l -c screen -R
Now, I get a real login shell which runs .profile, but screen fails to re-attach to a detached session. I run the above command on my Macbook, and on the remote machine, I’ve got an attached screen:
$ screen -list
There is a screen on:
21117.pts-21.roysmith01 (01/19/2015 01:50:59 PM) (Attached)
1 Socket in /var/run/screen/S-roysmith.
If I close the window where I ran the above ssh line, the session gets detached, as expected:
$ screen -list
There is a screen on:
21117.pts-21.roysmith01 (01/19/2015 01:50:59 PM) (Detached)
1 Socket in /var/run/screen/S-roysmith.
Now, if I open another local terminal window and run that ssh command again, screen fails to find the detached session and creates a new one:
$ screen -list
There are screens on:
21304.pts-21.roysmith01 (01/19/2015 01:52:40 PM) (Attached)
21117.pts-21.roysmith01 (01/19/2015 01:50:59 PM) (Detached)
2 Sockets in /var/run/screen/S-roysmith.
Any idea what’s going on here? Why does inserting a login shell keep screen from locating existing sessions?