TL; DR

  • Logging In to the Server locallyssh user@remote -p portPort 22ssh user@remote, log outexit
  • Local password-free loginssh-copy-id user@remote -p portThis is especially useful when writing script server controls
  • Configure an alias for the servercode ~/.ssh/configJust append a few thingsssh selfnameLogin, no longer need to remember the server IP address
  • Execute the commandssh selfname "cd ~; ls", will execute the command in quotes on the server, after which it will automatically shut down the remote service
  • Copy computer a files (folder) to which folder on computer B:SCP username@a PC IP address: file path username@b PC IP address: folder path, plus if it’s a folder-r, you can also use an alias, usually omitted in native languageThe username @ a computer IP:
  • The basic plus advanced use of SSH is referenced in large chunks

basis

SSH is a network protocol used for encrypted login between computers. Principle:

  • The remote host receives the login request from the user and sends its public key to the user.
  • The user uses the public key to encrypt the login password and send it back.
  • Using its own private key, the remote host decrypts the login password and, if the password is correct, allows the user to log in.

When the public key of the remote host is accepted, it is stored in the local file ~/.ssh/known_hosts. In addition, the system also has such a file, usually /etc/ssh/ssh_known_hosts, which holds the public key of the remote host that can be trusted by all users.

Basic commands:

ssh user@remote -p port

# user is your user name on the remote machine, which defaults to the current user if not specified
# remote is the address of the remote machine, which can be an IP address, domain name, or an alias mentioned later
# port is the port on which the SSH Server listens. If not specified, the default value is 22
# SSH [email protected] for example
Copy the code

SSH: connect to host remote port 22: SSH: connect to host remote port 22: Connect to host remote port 22: Sudo apt-get install openssh-server sudo apt-get install openssh-server sudo apt-get install openssh-server

Password-free login

Is it annoying to have to enter a password every time you SSH? The opposite of password authentication is public key authentication. In other words, to achieve password-free login, you need to set an SSH key first. That is, the local computer generates a public key private key, and then put the public key on the remote server! The principle is simple: users store their public keys on a remote host. At login, the remote host sends the user a random string, which the user encrypts with his private key, and sends back. The remote host decrypts the shell using the public key stored in advance. If successful, the user is proved to be trusted and is allowed to log in to the shell without requiring a password.

#Check if there are any
ls ~/.ssh
#Not generating
ssh-keygen
#In the ~/. SSH/directory, two new files id_rsa.pub and id_rsa are generated. The former is your public key and the latter is your private key.
#Put the public key on the remote server
ssh-copy-id user@remote -p port
#For example, my ssh-copy-id [email protected]
Copy the code

Brew install ssh-copy-id on MAC. SSH user@remote -p port ‘mkdir -p SSH && cat >>.ssh/authorized_keys’ < ~/.ssh/id_rsa.pub, SSH /id_rsa.pub (public key) to.ssh/authorized_keys. Of course, if you don’t use this command, you can manually copy the public key, log in to the remote machine, and paste it into.ssh/authorized_keys.

Configure an alias

SSH user@remote -p port SSH user@remote -p port SSH user@remote -p port Configuring aliases allows us to be lazy even further. Let’s say I want to replace the string above with SSH lab

<
Because it is the front end, edit it directly with vscode
code ~/.ssh/config
# Append the following and save
Host lab
    HostName remote
    User user
    Port port

# Log in
ssh lab

# Like mine
# Host han
    # the HostName 120.79.52.223
    # User zhm
    # Port 22
# ssh han
Copy the code

Command line performs login and executes commands on the target server

Command line performs login and executes commands on the target server:

# Single or double quotation marks enclose commands and separate them with semicolons
ssh user@remoteNode "cd /home ; ls"
Copy the code

If you have a lot of commands, you need to build a script.

# create a script file called test.sh, write this in it, and execute sh test.sh
#! /bin/bash
SSH user@remoteNode >/dev/null 2>&1 << remotessh
ssh user@remoteNode << remotessh
ls
exit
remotessh
Copy the code

Ah, scripting is easy, isn’t it?!

Transfer files

SCP can be used for file transfer between two machines. The address format of SCP is basically the same as that of SSH, but the user name and port can be omitted. The slight difference is that -p is uppercase instead of lowercase for the specified port. However, this does not matter if you have configured an alias, as SCP also supports direct aliases

The default remote current folder is Home directory (~). Copy files from COMPUTER A to computer B
# Note: If A is A file and B is the path of the file, the contents of file A will be written to file B regardless of whether the file exists.
If A folder exists on computer A, it will generate files and contents with the same name on computer A. If so, it will overwrite the files with the same name
# if A folder, add - r, if B computer file path complains, if B computer folder path and folder does not exist, it will establish the contents of this folder and put in A folder inside (equivalent to A folder to move over and then rename), folder exists, in the folder below to generate A and A folder with the same, It's the same thingSCP A PC: file path B PC: file path/path/to/local/file to/ path/to/remote/file
scp -P port /path/to/local/file user@remote:/path/to/remote/file

You can also use an alias
scp /path/to/local/file lab:/path/to/remote/file

/path/to/remote/file to/ path/to/local/file
scp lab:/path/to/remote/file /path/to/local/file

The default path for remote is the home directory
/ /dir/file
scp file lab:dir/file

# Add the -r command to transfer folders
The following command can transfer the current directory dir folder to the remote home directory
scp -r dir lab:

Don't forget. Can be used to refer to the current directory
The following command can be used to download the remote ~/dir directory to the current directory
scp -r lab:dir/ .
Copy the code

If you don’t feel comfortable transferring files from the command line, you can also use SFTP. Any CLIENT that supports SFTP can use your SSH account information to log in and manage files, such as FileZilla, the open source FTP client with a graphical interface. Don’t forget that with these clients, you can also specify your private key (~/.ssh/id_rsa) and log in without a password.

Keep the program running in the background

The following commands are executed on the server

nohup

Make the program run in the background like nohup node index.js &Nohup has been executing orders &# Look at the task node
ps
# Terminate if necessary
kill 21455
Copy the code

tmux

Tmux is more capable of executing complex programs, tMUx can also manage multiple Windows, window splicing, copy and paste, etc., it is more convenient for MAC users to use TMUx, new session TMUx-CC, tMUx-CC attach when recovery

Install TMUX on the server
sudo apt-get install tmux

# Run tMUx and enter the session. Anything running at this point will not be killed by exiting SSH
tmux

CTRL + B then press D

# restore session
# tmux attach

The # tmux command must run on the server
Copy the code

SSH Introduction to basic and advanced SSH Usage Shell in Linux Use SSH to automatically log in to the remote server, run commands, and automatically SSH forward