timerring

SSH Remote Access and File Transfer

February 12, 2022 · 4 min read
Tutorial
SSH | VPS
If you have any questions, feel free to comment below. Click the block can copy the code.
And if you think it's helpful to you, just click on the ads which can support this site. Thanks!

Log In to the Server #

ssh user@hostname
  • user: username
  • hostname: IP address or domain name

The first time you log in, you will see this prompt:

The authenticity of host '123.57.47.211 (123.57.47.211)' can't be established.
ECDSA key fingerprint is SHA256:iy237yysfCe013/l+
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Enter yes and press Enter. This records the server’s information in ~/.ssh/known_hosts.

Then enter the password to log in to the remote server. The default login port is 22. To log in through a specific port:

ssh user@hostname -p 22

Configuration File #

Create the file ~/.ssh/config, then enter the following in it:

Host myserver1
    HostName IP address or domain name
    User username

Host myserver2
    HostName IP address or domain name
    User username

After that, when using the servers, you can use the aliases myserver1 and myserver2 directly. For example, ssh myserver1.

Key-Based Login #

Create a key:

ssh-keygen

Then keep pressing Enter.

When the command finishes, there will be two new files in ~/.ssh/:

  • id_rsa: private key
  • id_rsa.pub: public key

To log in to a server without a password, send the public key to that server.

For example, to log in to the myserver server without a password, copy the contents of the public key into ~/.ssh/authorized_keys on myserver (create the file if it does not exist).

You can also add the public key with the following single command (convenient):

ssh-copy-id myserver

Execute Commands #

Command format:

ssh user@hostname command

For example:

ssh user@hostname ls -a

Or:

# $i inside single quotes can be evaluated
# Locally, '' does not expand $i, so the string is sent intact to the remote server for evaluation
# The remote server expands $i
ssh myserver 'for ((i = 0; i < 10; i ++ )) do echo $i; done'

Or:

# $i inside double quotes cannot be evaluated as intended
# The command is evaluated once locally and then again on the remote server
# First, the local machine sees "" and expands $i, but there is no local $i variable, so echo receives an empty value before the command is sent to the server
# The server evaluates it again and ultimately outputs several empty values
ssh myserver "for ((i = 0; i < 10; i ++ )) do echo $i; done"

Execute Multiple Commands Remotely with ssh #

ssh server "cd homework ; ls"

This can basically handle common remote-node management tasks. A few points to note:

  1. Without double quotes, the second ls command runs locally.
  2. Use a semicolon to separate the two commands.

Quoting the Entire ssh Command #

a=1
ssh myserver echo $a # correct
ssh myserver "echo $a" # correct
ssh myserver 'echo $a' # incorrect

Double quotes are evaluated locally, so the command sent is not echo $a but echo 1. Single quotes are evaluated on the server; what is sent is `echo $a, and since the server does not know the value of $a, it expands to an empty value.

# Double quotes are evaluated locally; the local machine does not know the value of `$i`, so it expands to an empty value
ssh myserver "for ((i = 0; i < 10; i ++ )) do echo $i; done" # incorrect
# Single quotes are evaluated on the server; the value of `$i` changes with the loop on the server
ssh myserver 'for ((i = 0; i < 10; i ++ )) do echo $i; done' # correct

Spaces in Shell Command Variables (When Executed with ssh) #

ssh ser mkdir homework/lesson_4/homework_4/\"$1\" # correct: escaping prevents the string from being evaluated locally
ssh ser mkdir homework/lesson_4/homework_4/"'$1'" # correct: after upload, "" is removed, while '' still keeps the argument together
ssh ser 'mkdir homework/lesson_4/homework_4/\"$1\"' # incorrect: an argument still needs to be received here; hard-coding it prevents that
ssh ser mkdir homework/lesson_4/homework_4/'"$1"' # incorrect: it is sent exactly as written and cannot be evaluated
  1. If a shell command (executed with ssh) contains spaces, enclose the variable in double quotes.
  2. With double quotes on the outside and single quotes inside, special characters such as $ can still be recognized.
  3. With single quotes on the outside and double quotes inside, special characters such as $ cannot be recognized.
  4. mkdir “my dir” -> mkdir my dir -> creates the my and dir folders
  5. mkdir"'my dir'" ->mkdir 'my dir'-> creates the my dir folder

scp File Transfer #

Basic Usage #

Command format:

scp source destination

Copy the file at the source path to destination.

Copy multiple files at once:

scp source1 source2 destination

Copy a folder:

scp -r ~/tmp myserver:/home/acs/

Copy the tmp folder in the local home directory to /home/acs/ on the myserver server.

scp -r ~/tmp myserver:homework/

Copy the tmp folder in the local home directory to ~/homework/ on the myserver server.

scp -r myserver:homework .

Copy the ~/homework/ folder on the myserver server to the current local directory.

Specify the server’s port number:

scp -P 22 source1 source2 destination

Note: Put scp options such as -r -P before source and destination whenever possible. Also note that the P here is uppercase.

Use scp to Configure vim and tmux on Other Servers #

scp ~/.vimrc ~/.tmux.conf myserver:

<< prev | Introduction to... Continue strolling Image... | next >>

If you want to follow my updates, or have a coffee chat with me, feel free to connect with me: