Today I will introduce to you several applications of ssh. Common applications include using ssh to connect to remote servers, using sftp to transfer files, and using ssh to perform off-site backups.
Connect to the remote host
Connect to the remote server This is our most commonly used function, connect to the 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.
Commonly used command usage is given below
Connect to the remote host ssh [account@]IP [-p specified port]
Without logging in, directly send a command to the remote server to execute the ssh -f [account@]IP [-p specified port] command
The following demonstrates remote connection to the 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 you connect for the first time, you will be asked if you want to connect, enter yes here. After entering yes, you will be asked to enter a password
root@121.196.12.64'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 you enter the password correctly, you will successfully connect to the remote server.
When the public key of the remote host is accepted, it will be saved in the file $HOME/.ssh/known_hosts. The 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 you to enter the password.
Each SSH user has its own known_hosts file. In addition, the system also has such a file, usually /etc/ssh/ssh_known_hosts, which saves some public keys of remote hosts that are trustworthy to all users.
If you want to exit the login, enter exit and wait to log in
# exit logout Connection to 121.196.12.64 closed.
Simulate ftp file transfer method: SFTP
Using SSH is used to control the remote host. If you just want to download resources from the remote server or upload local files to the server, then use sftp or scp. These two commands are also through the ssh port.
Login through sftp
# sftp root@121.196.12.64 root@121.196.12.64's password: Connected to 121.196.12.64. sftp> ? <== 输入?可查看交互命令的帮助信息
There are many commands for the interactive mode of sftp. Here are some common interactive commands, viewed from the three categories.
Commands for the remote server: such as ls, pwd, mkdir, etc.
Commands for the local machine: lcd, lls, etc.
Commands for uploading and downloading: put (upload files), get (download files).
The upload and download operations are demonstrated below.
# 从远程服务器下载一个文件到本地 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 # 上传本地文件到服务器上 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
Off-site file transfer: SCP
The SCP command can be used for off-site backup. The simplest usage of SCP is as follows
# 上传文件 scp [-pr] [-l 速率] file [账号@]主机:目录名 # 下载文件 scp [-pr] [-l 速率] [账号@]主机:file 目录名
Options and parameters:
-p Keep file attributes
-r Recursive operation
-l Limit the rate, followed by a value; for example, 1024 means 1024k bytes/s
For the backup of important files, follow one principle "Never put all your eggs in one basket." In addition to local backup, we should also perform off-site backup. Frequently use the scp command plus the system's scheduled tasks to perform off-site 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 the linux tutorial column!
The above is the detailed content of What functions does the ssh service have - logging into remote hosts, sftp, and off-site backup of files?. For more information, please follow other related articles on the PHP Chinese website!