Today, I will introduce several applications of ssh to you. Common applications include using ssh to connect to remote servers, using sftp to transfer files, and using ssh to perform remote backup.
Connect to remote host
Connect to a remote server This is our most commonly used function, connect to a remote server, and then manage the server.
If your client is windows, then you need to install terminal tools, such as xshell, Terminator, Tmux, etc., and then use these terminal tools to connect to the remote server. If the client is Linux, you can use the ssh command directly.
Common command usage is given below
-
Connect to remote host ssh [account@]IP [- p specified port]
-
Do not log in, directly send a command to the remote server to execute the ssh -f [account@]IP [-p specified port] command
Let’s demonstrate the remote connection server
# ssh 121.196.12.64 The authenticity of host '121.196.12.64 (121.196.12.64)' can't be established. ECDSA key fingerprint is SHA256:wx0RHE8fcCoad6YKw0Ex4NE+QjwRiTYxC2s2g/DqPUU. ECDSA key fingerprint is MD5:43:2c:7a:12:24:1d:86:3a:b0:a0:b7:95:c2:cf:7b:ab. Are you sure you want to continue connecting (yes/no)?
When connecting for the first time, you will be asked if you want to continue connecting, enter yes here. After entering yes, you will be asked to enter a password
[email protected]'s password: Welcome to Alibaba Cloud Elastic Compute Service ! Activate the web console with: systemctl enable --now cockpit.socket Last login: Thu Nov 19 16:25:42 2020 from 114.103.36.247
When the password is entered correctly, the remote server is successfully connected.
When the public key of the remote host is accepted, it will be saved in the file $HOME/.ssh/known_hosts. Next time you connect to this host, the system will recognize that its public key has been saved locally, skip the warning part, and directly prompt for the password.
Each SSH user has its own known_hosts file, and the system also has such a file, usually /etc/ssh/ssh_known_hosts, which stores the public keys of some remote hosts that are trusted by all users.
If you want to log out, type exit to wait for the login
# exit log out Connection to 121.196.12.64 closed.
Simulated ftp file transfer method: SFTP
Using SSH is used to control the remote host. If you just want to download from the resources of the remote server, or upload local files to the server, then use sftp or scp. These two commands are also ported over ssh.
Login via sftp
# sftp [email protected] [email protected]'s password: Connected to 121.196.12.64. sftp> ? <== input? You can view the help information of interactive commands
There are many commands for the interactive mode of sftp. Below are some common interactive commands, from the perspective of three categories.
-
Commands for remote servers: such as ls, pwd, mkdir, etc.
-
Commands for this machine: lcd, lls, etc.
-
Commands for uploading and downloading: put (upload files), get (download files).
The following demonstrates the upload and download operations.
# Download a file from the remote server to the local sftp> ls 1.txt install.sh sftp> get 1.txt Fetching /root/1.txt to 1.txt /root/1.txt 100% 6 0.0KB/s 00:00 sftp> lls 1.txt install.sh job1.php job2.php job3.php learnshell logrotate_learn.log logrotate_learn.log.1.gz # Upload local files to the server sftp> put job1.php Uploading job1.php to /root/job1.php job1.php 100% 34 0.3KB/s 00:00 sftp> ls 1.txt install.sh job1.php
File remote transfer: SCP
SCP command can be used for remote backup. The simplest usage of SCP is as follows
# Upload files scp [-pr] [-l rate] file [account@] host: directory name # download file scp [-pr] [-l rate] [account@] host: file directory name
Options and parameters:
-
-p preserve file attributes
-
-r recursive operation
-
-l limit rate, followed by value ; as 1024 means 1024k bytes/s
For the backup of important files, follow a principle “never put all your eggs in one basket”. In addition to local backups, we should also perform off-site backups. Often use the scp command plus the scheduled task of the system to perform remote backup, such as:
* 2 1 * * scp -rp root@101.*. *.185:/backup \ > /root/backup/scp_$(date +$Y%m%d) 1>/dev/null 2>&1
For more related technical articles, please visit linux tutorial column!
The above are the functions of the ssh service – the details of logging in to the remote host, sftp, and file backup in different places. For more information, please pay attention to other related articles on 1024programmer.com!