Using VSCode with UW CS Linux Servers

Installing "Remote - SSH" extension

Watch the VS Code Extensions video and install the "Remote - SSH" extension by Microsoft.

Setting up the "Remote - SSH" extension

Click the green VSCode Remote Connect Icon icon at bottom left of VSCode, and choose "Connect to Host". Then click "Configure SSH Hosts" and choose the first option, which will vary depending on if your computer is Mac (~/.ssh/config) or Windows (C:\Users\username\.ssh\config). A sample config file is below (replace userid with your UW username, up to 8 characters):

Host *.student.cs.uwaterloo.ca
  # Replace userid with your UW username
  User userid
  # Optional: If you have an SSH key generated on your computer,
  # you can specify its location by uncommenting the following line:
  # (by default, it looks in your .ssh folder for an id_rsa file)
  # IdentityFile ~/.ssh/id_rsa

Host ubuntu2204-002.student.cs.uwaterloo.ca

Host ubuntu2204-004.student.cs.uwaterloo.ca

Host ubuntu2204-006.student.cs.uwaterloo.ca

Host ubuntu2204-010.student.cs.uwaterloo.ca

Host ubuntu2204-012.student.cs.uwaterloo.ca

Host ubuntu2204-014.student.cs.uwaterloo.ca

This sample SSH config file will make VSCode let you choose which Linux server to use when you click "Connect to Host". An up-to-date list of available servers is on the CSCF teaching hosts web page.

If the list of servers on that web page change, you will have to update the SSH config on your computer yourself.

If you have SSH keys generated, you can use the IdentityFile setting to tell VSCode where your id_rsa file is. Alternatively, you can type in your password each time, but be aware VSCode can ask you for your password a lot. It will be even more annoying from off-campus without VPN, because you'll have to accept the 2FA prompt each time. For your own convenience, you may want to set up SSH keys.

The following are instructions for generating your SSH keys:

# Navigate to your ~/.ssh folder on your own computer
# WINDOWS:  cd \Users\your_windows_username\.ssh
# MAC:      cd ~/.ssh

# Generate your keys
ssh-keygen
(then press [enter] [enter] [enter] if you do not wish to have a passphrase)

# copy your id_rsa.pub file to the school
scp id_rsa.pub username@linux.student.cs.uwaterloo.ca:.ssh/my_pub

# Now, log into the school
ssh username@linux.student.cs.uwaterloo.ca

# append your my_pub file to your authorized_keys file (it may or may not exist)
cd .ssh
cat my_pub >> authorized_keys

Which server should I use? linux.student.cs or ubuntu2204-NNN and what NNN?

You should be using ubuntu2204-NNN with VSCode. The Ubuntu 22.04 Linux servers currently available are listed on the CSCF teaching hosts web page.

You can pick whichever one of the Linux servers you want, for example you might pick ubuntu2204-006.student.cs.uwaterloo.ca. But once you pick a server, try to keep using it each time you connect in VSCode. This will help avoid creating extra VSCode instances on other servers, which takes up extra computing resources (CPU, RAM/memory). When you close VSCode on your computer and re-open it later, VSCode automatically connects to the same server you were using before, so you don't need to remember it yourself.

If the server that you picked is not working, then you can use another server. To do this, save your work then close your VSCode remote connection. Then connect to another server. With the sample SSH config above, after you click "Connect to Host" in VSCode, you will see a drop-down menu of the servers in the config. You can pick a different one from your usual server. Any of the Linux servers may go down for various reasons, such as crash or hardware failure, but I.T. staff will try to fix it as quickly as possible. With multiple servers, even if 1 or 2 is down, you should still be able to connect to the other ones while the broken servers are being repaired.

Having multiple VSCode windows open and connected remotely will use more resources, so avoid that if possible.

Explanation of avoiding "linux.student.cs.uwaterloo.ca" with VSCode

Using "linux.student.cs.uwaterloo.ca" will randomly redirect you to one of the Linux servers, and the server you randomly get can change often. VSCode does not always shut itself down properly on the server. Using VSCode with "linux.student.cs.uwaterloo.ca" can take up computing resources on multiple servers, even though you really only need to be using just one server. As an analogy, it'd be like taking up multiple seats in the classroom when you only need one seat.

If you are connecting with simpler SSH programs like PuTTY or SSH command in Mac Terminal, then you can use "linux.student.cs.uwaterloo.ca" because these programs use fewer computing resources than VSCode, and they shut themselves down faster on the server when you close them.

Extensions and C/C++ IntelliSense

Please avoid installing VSCode extensions while connected remotely, unless the course tells you to. For CS136(L), the only extensions you should have installed remotely are the Marmoset VSCode extension and C/C++ extension from Microsoft (see CS136L for more info on this extension). The reason for this rule is because some VSCode extensions use a ridiculous amount of CPU and memory, and they make the server unusable for other students. If you install these kind of extensions, and the I.T. staff detect that your extension is causing problems on the server, the extension will be terminated without warning.

The C/C++ IntelliSense extension sometimes use a large amount of memory (several gigabytes). To stop it from doing this, open the VSCode settings (gear icon at bottom left). Then search for "memory" and click the "Remote [SSH...]" tab. Several settings related to memory will appear. Set the "C_Cpp: Max Memory" and "C_Cpp: Intelli Sense Memory Limit" to the minimum of 256.

Configuring VSCode C/Cpp IntelliSense Memory Limit

Closing VSCode Remote Connection

First, remember to save all your work. Then when you're done using VSCode, or if you want to switch servers because the server you're on is broken, go to File => Close Remote Connection. Then close the VSCode window. If you want to reconnect, click the remote connection icon VSCode Remote Connect Icon.

Connecting Remotely after updating VSCode

Microsoft releases VSCode updates at least once a month. After you update VSCode, the next time you connect to the Linux servers will take a while. The reason is that VSCode tries to delete a large folder in your Linux account, and this deletion can take a few minutes. Installing the new VSCode files into your account can take some time, too. You may have to retry connecting a couple times after a VSCode update.

Other questions