Egor Demidov

Using a remote linux machine: PART 1

This tutorial is an introduction on how to start using a remote linux machine from the command line of your device. It covers some basic aspects: generating a log in key pair, logging in and navigating the system, and transferring files between the remote system and your device. The approaches discussed here should work on any client system - Windows, macOS, or linux.

Linux, macOS, and recent releases of Windows usually have an ssh client enabled by default. To confirm this, run ssh in the terminal. If the result of this command is not a “command not found” message, you are good to proceed with this tutorial. Otherwise, it is necessary to configure an ssh client on your system before continuing the tutorial.

If you are doing this tutorial on Windows, it is necessary to use PowerShell and not the cmd.exe command line.

Our linux box info
Must be connected to NJIT VPN to access
Hostname: linux.edemidov.com
Port: 22

Generating a login key pair

We will be using a cryptographic key pair to log into the remote linux machine due to the enhanced security of key pairs compared to simple passwords. You will need to generate a key pair on your device. To do that, run the following command in the terminal:

ssh-keygen -t rsa

Leave all the parameters at their default values by pressing enter to skip through the questions that ssh-keygen will ask you. If ssh-keygen warns that a key already exists and asks if you want to overwrite it, decline and do not overwrite the key. After the successful execution of the command, you should find a folder named “.ssh” in your home directory. To view its contents, run:

cd ~/.ssh
ls

In the output, you will see the list of files present in the “.ssh” folder. There should be files “id_rsa” and “id_rsa.pub” among them. These two files contain your private and public keys respectively. These keys are cryptographically related, but it is impossible to reconstruct the private key from the public key. The public key can be used to verify messages signed with the private key. The private key should never be shown to anyone or sent over the internet. The contents of the public key need to be sent to your system administrator so they can configure your account on the remote linux machine. To view the public key, run:

type ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub

The body of the output should contain a long string of letters and numbers. Copy the entire output and send it to the system administrator (our group’s linux box is administered by me). Await confirmation from the administrator before proceeding with the tutorial.

To log in to the remote machine, run the following command:

ssh <username>@<hostname>

<username> should be replaced by the name assigned to you by the system administrator and <hostname> should be replaced by the web address of the remote machine. For example, I would use ssh egor@linux.edemidov.com to log into our group’s linux box. When done with the session, make sure to log out and terminate your session by running:

exit

Once logged in to the remote machine, the command line will be initialized in the home directory. The command line is like a file browser - it lets you create, move, open, and delete files on the computer. To switch between folders, manipulate files, and run programs, text commands need to be typed into the terminal. Here I provide a reference list of some of the most fundamental commands that you will need while working with a linux machine.

ls command

Mnemoniclist
Actionlist files in the current directory
Argumentsnone

pwd command

Mnemonicprint working directory
Actionprint the path of the current directory
Argumentsnone

cd command

Mnemonicchange directory
Actionmove into another directory
Argumentspath or none
NotesIf a directory name or a complex path is provided, opens the specified directory. If used without arguments, returns to the home directory
Example: return to homecd
Example: move to parent directorycd ..
Example: open directory my_directorycd my_directory
Example: open a pathcd /usr/bin

mv command

Mnemonicmove
Actionmove or rename file
Arguments(1) old path, (2) new path
NotesIf you want to move a file from some source directory to the current directory, the second argument should be a dot .
Example: rename file my_file.txtmv my_file.txt readme.txt
Example: move file my_file.txt and rename itmv ~/Downloads/my_file.txt readme.txt
Example: move file my_file.txt without renaming itmv ~/Downloads/my_file.txt .

cp command

Mnemoniccopy
Actioncopy file
Arguments(1) old path, (2) new path
Example: copy file my_file.txt to readme.txtmv my_file.txt readme.txt

rm command

Mnemonicremove
Actiondelete file
Argumentspath
Example: delete file my_file.txtrm my_file.txt
Example: delete directory my_directoryrm -r my_directory

mkdir command

Mnemonicmake directory
Actioncreate directory
Argumentspath
Example: create directory my_directorymkdir my_directory

cat command

Mnemonicconcatenate
Actionprint the contents of the file
Argumentspath
Example: print file my_file.txtcat my_file.txt

wget command

Mnemonicweb get
Actiondownload file from internet
ArgumentsURL
Example: download the source code of soot-demwget https://github.com/egor-demidov/soot-dem/archive/refs/tags/v2.tar.gz

There are many more essential and convenience commands in linux, but these will help you get started. Refer to web resources for other commands. Some useful commands to learn next would be the archive manipulation commands (zip, unzip, tar) or text manipulation commands (vi, nano).

Copying files to/from the linux box

To copy files to/from the remote computer, the scp command needs to be used from the client device. The scp command is similar in syntax to cp, except it can copy files over the internet using the ssh protocol. If the source or destination path is on the remote host, it needs to be prefixed with <username>@<hostname>:.

For example to copy the file my_file.txt from our linux box, I would use:

scp egor@linux.edemidov.com:/home/egor/my_file.txt .

To copy a local file my_file.txt from my device to the linux box I would use:

scp my_file.txt egor@linux.edemidov.com:/home/egor/my_file.txt

Alternatively, Windows users can use MobaXterm, which is a program that combines an ssh-capable terminal for logging into a remote machine and a graphical interface for file transfer between the remote and the client. Thus, you can just use the drag and drop semantics to exchange files with the remote machine. MobaXterm would need to be configured to log in using your private key instead of password-based authentication, which is enabled by default.

To continue learning about linux terminal, check out part 2 of the tutorial.

Copyright © 2024 Egor Demidov. Content on this website is made available under CC BY 4.0 licence unless specified otherwise