keep ssh connection alive and persistant while switching network interface connections

21,867

Solution 1

I suspect that this solution will not work for SSHFS, etc, but you could take a look at Mosh which provides roaming support for at least the shell itself.

Solution 2

I'm afraid you cannot do this, by definition. An SSH session runs over a TCP connection, which is defined by the four-tuple (source address, source port, destination address, destination port). You cannot shift the existing connection to a different address on the client (aside from the fact that the OS will tear down the connection when the interface goes down).

NAT may complicate this picture, but not in any way that will help you.

Solution 3

Old thread, I know, but for sake of completeness since I was looking for the same thing...

In Windows 7 or newer you should simply be able to select both your wifi adapter and Ethernet adapter and choose "Bridge Connection". This will give you a single IP address for both, and you will now be able to disconnect and reconnect Ethernet at will (given continuous wifi coverage).

Solution 4

I manage this in two ways:

Start the session from a terminal server in your datacenter (RDP/Windows/etc)

OR

Install GNU or and start them after login.

I prefer tmux because screen is pretty old school, but if you are only allowed to install supported packages, screen is in the RHEL repo.

OR

Do both.

Solution 5

You can bond the Ethernat and Wifi connections.

This is how I do it on RHEL:

# Run as sudo:
$ sudo -i

$ modprobe bonding

# To enable after reboot, add " bonding " to:
$ vi /etc/modules-load.d/bonding.conf

# Get your network interfaces IDs
$ nmcli device status | grep -E "wifi|eth"
  # enp0s31f6   ethernet  connected  
  # wlp58s0     wifi      connected  

# Add new connections
$ nmcli connection add type bond ifname bond0 con-name My-Connections-Bond
$ nmcli connection add type ethernet ifname enp0s31f6 master My-Connections-Bond con-name My-Eth-Connection
$ nmcli connection add type wifi ifname wlp58s0 master My-Connections-Bond ssid My-SSID con-name My-Wifi-Connection
  
# Add your Wifi credentials
$ nmcli connection modify My-Wifi-Connection wifi-sec.key-mgmt wpa-psk

$ nmcli connection edit My-Wifi-Connection

    nmcli> set wifi-sec.psk YOUR-PASSWORD  
    nmcli> save
    nmcli> quit

# Enable the connections
$ nmcli connection up My-Connections-Bond
$ nmcli connection up My-Eth-Connection
$ nmcli connection up My-Wifi-Connection
  

This should allow you to roam between your Wifi and Ethernet connections, and keep your current SSH, VPN, etc. sessions connected.

Credits to: https://fedoramagazine.org/bond-wifi-and-ethernet-for-easier-networking-mobility/

Share:
21,867

Related videos on Youtube

Sean
Author by

Sean

Updated on September 18, 2022

Comments

  • Sean
    Sean over 1 year

    Scenario:

    1. At my desk with laptop plugged in to ethernet and connected to remote server over SSH
    2. Want to move to other side of office with laptop and change to WiFi without interrupting SSH connection

    I've tried: connecting to WiFi first then disconnecting ethernet; and also disconnecting ethernet first then connecting to WiFi. Neither approach works. Also tried when using Ubuntu and OS X servers along with both OS options as well for the client. No luck.

    It seems like I need some way of telling my SSH connection that it should begin using the newly connected network interface instead of the old disconnected interface. Any ideas?

    I realize that I could just stay on WiFi the whole day, but I don't want to do that. I also realize that I can just work from within a screen session on the remote server and then re-connect to that screen session after changing interfaces, but I don't want to do that either. For example, I might be piping some big command like a database dump over SSH or I might have files open through SSHFS, or I might just want to avoid the nuisance of re-connecting

    • Alan Curry
      Alan Curry almost 12 years
      If you get the same IP address both ways, then this should Just Work. If not, it's pretty much impossible.
    • kasperd
      kasperd about 8 years
      @AlanCurry If both client and server support MPTCP, then it is possible. But MPTCP support is still very limited, so that's unlikely to be the case.
  • Doug
    Doug about 8 years
    Yes, I agree. The multiple window function is great.
  • Ajay Brahmakshatriya
    Ajay Brahmakshatriya over 5 years
    Yes, but the application can reconnect once it knows that the socket has closed. Does the SSH protocol have a provision to resume an old session?
  • user000001
    user000001 over 5 years
    This looks like a very good solution, but unfortunately it needs to be installed on the server too, which is not always possible due to permissions.
  • Ferrybig
    Ferrybig almost 5 years
    @AjayBrahmakshatriya SSH has no tools to resume sessions
  • Ferrybig
    Ferrybig almost 5 years
    If doing this, don't cannot to both networks at once, depending on the setup, it kicks everyone in area of the network (source: this happened to me)
  • Rouben
    Rouben almost 4 years
    From mosh.org/#usage mosh --server=/tmp/mosh-server remote_host The user can specify an alternate path for the mosh-server on the remote host. The server binary can even be installed in the user's home directory.