A glance of SSH

2 minute read

Published:

The recent lab (an alias of homework in computer science) required us to set up the environment on Tencent Cloud. The deployment includes the usage of SSH, a way of remote log-in, say, log in your best friend’s computer to debug for him. This article will introduce the basic information of SSH and how to use SSH.

Basic usage

In this lab, I rented 4 vitual machines on Tencent Cloud with about 100 dollers for 2 months. The vendor provided no GUI interface for constumers so I have to use SSH to log in the machines remotely and enter their terminals to get control of them.

In your own machine, enter the following command in the terminal:

ssh -q -l ubuntu -p 22 212.64.89.105

A simple explanation for this command:

  • ‘ubuntu’: the user name in the virtual machine.
  • ‘22’: the 22 port of each computer is for SSH connection.
  • 212.64.89.105: the public IP of the virtual machine

Then you’re asked to enter the password of the remote machine. After that the terminal will appear to be the remote machine’s.

Change the hostname

If you fail to remember the public ip or you don’t wanna brother type the ip every time you use SSH, you can readily add a host name in /etc/hostname, say app.

Then the command is simplified as:

ssh ubuntu@app

Here, ubuntu and app are the default user name and the hostname of the remote machine respectively.

Change the user name

A machine can have more than one users which means one can SSH to different users in a virtual machine.

Modify ~/.ssh/config such that you can change the user name.

A instance is shown as below.

Host app
	HostName 212.64.89.105
	User cse
	Port 22

Host name
	HostName 126.125.125.125
	User cse
	Port 22

Host data1
	HostName 127.125.125.125
	User cse
	Port 22

Host data2
	HostName 128.125.125.125
	User cse
	Port 22

After modification, you can SHH to any user like:

SSH app

By the way, you may need to switch users in the remote machine and at many times modification of some files needs super user privilige. I conclude some common operations.

  • Add a user
    • sudo adduser cse
  • Switch a user
    • su newuser
  • Change priority
    • sudo visudo (sometimes fail, try su and then visudo)
    • add newuser ALL=(ALL:ALL) NOPASSWD: ALL

Passwordless SSH

Human is lazy. Convience overweighs security. To avoid entering a long and ugly password every time, SHH provides a passwordless way.

Execute ssh-keygen (ssh-keygen command on Linux or PuTTYgen on Windows) on your machine, it will generate a key pair in ~/.ssh, public key is ~/.ssh/id_rsa.pub, private key is ~/.ssh/id_rsa.

The public key looks like:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQ5e7DARGEvgfWco0ri2/zZa13abPoOgFoDf5hVDyTMb56gbD7NXcTmkQgA36KQFvaxWlE8/3eLQO8900zQGwuZoHht1VevmzP1tdN7DNKWFtD4U9NJCVKPe21YspQ1zBsztvCbikWSW4pKNJLlgZPmOF3HGJjWP/BbhY7h04n3A5iL4nozrfMt2K6fq0dm/JbuqK+6+rWjA+7id8m2ggEvUx7B504QQEe+KsbjTO9LZ0Nh6WMBog2IwpOPqB1XWyN4JDRLn3pzo9KIZ1ZYiutoGTajjDzRLXUwImtk0ETTuJPs4ISQ4NT6e4VVBYboo5H0fqUQXWinFnH19DjIhkB cse@app

Put the public key in ~/.ssh/authorized_keys of the remote machine.

SSH app

Now it needs no password.