Set up SSH remote access for Windows Subsystem for Linux (WSL)

To access lab computers remotely, you could use TeamViewer in the Windows environment. However, it gets stuck sometimes and the experience is just not perfect. Since we usually run our code in the Linux environment, it would make our life much easier if we could have clean access to the Linux OS on the lab computer. To do this, we could set up ssh access to the lab computers.

Here are the steps:

  1. Please make sure the Windows Subsystem for Linux (WSL) and Gurobi are properly set up on the lab computer. If you haven’t done so, read this topic for the instructions: Setting up Gurobi with C++ in the Linux environment

  2. Let’s set up the ssh server. To get the ssh server working properly, it must be uninstalled and then reinstalled using the following command:
    sudo apt remove openssh-server
    sudo apt install openssh-server

  3. Edit the sshd_config file by running the command sudo nano /etc/ssh/sshd_config, and do the following changes in the sshd_config file:

    • Change PasswordAuthentication to yes
    • Add your login user to the bottom of the file by using this command: AllowUsers yourusername. Don’t forget to replace “ yourusername ” with your actually username. If you want to add multiple users, you could separate each user with a space: AllowUsers user1 user2
    • Use CTRL+X to save and exit
  4. Check the status of the ssh service using the command service ssh status:

    • If you see sshd is not running, then run this command: sudo service ssh start.
    • If you see sshd is running, then run this command: sudo service ssh --full-restart.
  5. Enable Port 22 in Windows Firewall.

    • Open Windows Firewall Advance Settings, Click on New Rule… under Inbound Rules to create a new firewall rule, and select Port under Rule Type. Then click Next to continue.
    • Under Protocol and Ports select Specific local Ports and enter 22. Then click Next to continue.
    • Under Action select Allow the connection. Then click Next to continue.
    • Under Profile make sure to only select Domain and Private. Note, with this setting, you will need the VPN of our university to remotely access the lab computer through ssh. ( Warning: do not select Public unless you absolutely require a direct connection form the outside world. This is not recommend especially for portable device (Laptop, Tablets) that connect to random Wi-fi hotspots.) Then click Next to continue.
    • Under Name, set the Name as SSH Server, and the Description also as SSH Server (you could name it something else if you prefer). At last, click Finish.
  6. Test the ssh remote access.

    • Get the IP address of the lab computer. You could use the command ifconfig to get the IP in the Linux environment, or use the command ipconfig in the Windows Command Window. To access the Windows Command Window, you could press Win+R on the keyboard and type in cmd and press Enter, and then the Windows Command Window will open.
    • Once you get the IP address of the lab computer, turn to your personal computer and open WSL. Make sure your VPN is connected, and then run this command: ssh username@IP_address. Please don’t forget to replace the username with the actual username and the IP_address with the actual IP address.
    • Then it will prompt something asking you some stuff, just type in yes. Then it will ask you to type in your password (note, it is your password to the Linux system, not to the Windows system), and then it will get you connected.
    • Once you are connected, congratulations! Now you have remote access to the WSL on the lab computer. You could run your code on the lab computer just like what you did in front of the computer.

Additional notes:

  1. File transfer from your personal computer to the lab computer.

    • You could use the command scp in the Linux environment to copy the files from your personal computer to the lab computer. An example of copying a file named filename.txt from your local path /mnt/c/local/directory to the lab computer is shown as follows:
      scp username@IP_address:filename.txt /mnt/c/local/directory
    • An easier and more intuitive way to copy the files is to use the software WinSCP. You could download the software here for free: https://winscp.net/eng/index.php
      • After you install it on your personal computer, run it, and click New Site on the left.
      • Then type in the IP address of the lab computer under Host name, keep Port number as 22, then put in your Linux user name and password as well. You could click Save right below User name to save the information you don’t have to type it in each time you login. Then click Login at the bottom.
      • Then you will be connected to the lab computer. The WinSCP interface has two parts, the left is your personal computer and the right side is the lab computer that you connected to. You could choose a path that you like on the lab computer and then upload the files to the path, and you could also see what files are already in the path, which makes it really convenient to manage the files on the remote computer.
  2. Setting up an account for another user. If you don’t need to share the lab computer with anyone else, then you don’t need to worry about this part. But if you do, you need to do the following to set up another account:

    • In the Linux environment, run the command: sudo adduser new_user_name. It will prompt to ask you some information about the user, like the Full Name, but if you don’t want to provide this information, just press Enter to use the default settings.
    • Add the new user to sudoers (this step is optional if all the packages are installed and the new user doesn’t need to install or configure anything): usermod -aG sudo new_user_name
    • Allow the new user to have access to the ssh server. Edit the sshd_config file by running the command sudo nano /etc/ssh/sshd_config, and allow the new user to have access to the ssh server: AllowUsers the_first_user new_user_name
    • The new user should register their own Gurobi license. Each user has to have their own Gurobi license. Even if the first user has already set up the license, the license would only allow the first user to use it. So additional users have to apply for their own Gurobi license and use the command grbgetkey key_serial_number to register their license once they log in as the new user. Make sure to create your own folder to save your license, and don’t replace other user’s licenses.
    • The new user should set up the environment variables for Gurobi. Each user can only set the environment variables for themselves, so the new user has to set up their own environment variables. Once you log in as the new user, you could edit the environment variables using the following command:
      sudo nano ~/.bashrc
      and then add the following 4 lines to the end of the file:
      export GUROBI_HOME="/mnt/c/Linux/gurobi902/linux64" export PATH="${PATH}:${GUROBI_HOME}/bin" export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib" export GRB_LICENSE_FILE="/mnt/c/Linux/gurobi902/gurobi.lic"
      Please make sure the GRB_LICENSE_FILE path is the one where you stored your license file.
    • Log out of the remote computer by run the command exit, and then login again, then the new user should be able to use Gurobi.

Reference:

  1. https://www.illuminiastudios.com/dev-diaries/ssh-on-windows-subsystem-for-linux/
  2. http://www.hypexr.org/linux_scp_help.php