Working on a remote linux server

Table of Contents

This is a getting-started guide to working on a remote server.

1 Basics

1.0.1 Logging in from your local system

You should know the username and ip address (e.g., 192.168.10.10) of the server. Open a terminal (if you are using Linux/Mac OS in the local machine) or cmd/powershell (if you use Windows). You are now in your local machine/system. Type

ssh username@ipaddress

or

ssh -X username@ipaddress

The second option also allows you to use some graphical applications. However, you will need a good internet connection for this.

You will be asked for a password, and if that is successfully authenticated, then you are in the server!

There are various software with GUIs for doing many of these (logging in, copying files, etc.), but it is always good to know how to do it via the terminal.

1.0.2 Navigating within the server

Since you only have a text-based interface, you need to know how to navigate, and manipulate files on the server. Learn commands such as cd, pwd, less, mkdir, touch, etc. There are plenty of online tutorials.

Learn the basics of vi/vim/emacs. Also learn to monitor resources using commands like top or htop, sensors, ps. In many cases, you can stop running code using Ctrl-c on the terminal. In other cases, you may need to use the kill command.

1.0.3 Installing basic software

Generally, C/C++ compilers are already installed. You generally will not have superuser/sudo permissions, so you will not be able to install software using apt/apt-get/etc. You can only download scripts and install software locally for your user. You may want to install python. It is better to install anaconda, which is an entire package for python. Go to the Linux installation guide. Get the link to the installer, and download it to some location using wget. Then run the installer. Choose the default options, and say yes when it asks you if you want to add anaconda to the default path.

Once this is done, if you want to install additional libraries, use conda install library_name, e.g., conda install matplotlib. If this does not work try pip install library_name. It is good to learn about virtual environments.

1.0.4 Copying files to and from the local system

If you want to copy a file from the local system to some location on the server, you can open the terminal/powershell and do something like

scp location_of_local_file username@ipaddress:location_of_destination

Example:

scp ~/Documents/some_file username@192.168.10.10:Downloads/

This will copy some_file from the Documents folder of your local machine to the Downloads folder of your server. Make sure that the destination is a valid location.

Similarly, if you want to copy a file from the server to the local machine,

scp username@ipaddress:location_of_source location_of_destination

Example:

scp username@192.168.10.10:Downloads/some_file ~/Documents

Search for scp tutorials online to know more.

1.0.5 Useful workflow

I generally prefer to use a text editor on my local machine to work with files on the server. A very useful approach is to mount a folder which resides on the server onto my local machine. You can do this using sshfs. Search online to know more. The standard approach would be

sshfs username@ipaddress:folder_on_server folder_on_local_machine

For example, I create a folder called ssh_mount within Documents/ on my local desktop. If I run

sshfs username@ipaddress:Downloads ~/Documents/sshfs-mount/

then all the files in Downloads/ on the server are now accessible from Documents/sshfs-mount/, which is neat!

After you are done, you should unmount the folder using

fusermount -u ~/Documents/sshfs-mount/

You can now use vscode/emacs/gedit/geany or any other locally installed software to edit/view/interact with files on the server.

Note: Avoid mounting folders that contain very large files, particularly if these are going to be modified when running code. These can potentially cause a system freeze on your local machine. Always create a separate directory for datasets, and one for only code, and only mount the folder containing your code.

Another very useful software is tmux. This lets you split the terminal into panes, and more importantly create sessions that do not close in case your connection drops or you end the ssh session. Very useful if you want to run some code on the server for several hours!

To create a new tmux session, use

tmux new -s session_name

Ctrl-b % and Ctrl-b " let you vertically/horizontally split panes. Ctrl-b o lets you navigate panes, and Ctrl-b x closes the current pane.

1.0.6 Other resources

There are plenty of resources online, including video tutorials. This is a great place to learn some useful tools.

If you have any other tips for working on remote servers, please let me know!



Author: Shashank

Created: 2022-01-31 Mon 10:53