SSH全称为Secure Shell,是一种能够以安全的方式提供远程登录的协议,是目前远程管理 Linux 系统的首选方式。在SSH出现之前一般使用FTP以及Telnet来进行远程登录,但是他们都是以明文的形式在网络中传输账户密码和数据信息,因此非常不安全,这种方式很容易受到黑客发起的中间人攻击,从而篡改数据或截取服务器账号密码。
[root@origin ~]# hostname
origin
[root@origin ~]# ssh 192.168.78.100
The authenticity of host '192.168.78.100 (192.168.78.100)' can't be established.
ECDSA key fingerprint is c1:b8:67:1f:1d:c0:cd:6b:37:90:42:b1:c6:5a:e8:cf.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.78.100' (ECDSA) to the list of known hosts.
root@192.168.78.100's password:
Last login: Sun Jan 5 11:19:40 2020 from 192.168.78.1
[root@heimatengyun ~]# hostname
heimatengyun
[root@heimatengyun ~]#
[root@origin ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file inwhich to save the key (/root/.ssh/id_rsa): 按回车或设置密钥存储路径
Enter passphrase (empty for no passphrase): 按回车或设置密钥的密码
Enter same passphrase again: 按回车或设置密钥的密码
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
bc:94:4e:e1:82:7c:4a:96:ad:a3:38:c5:d6:47:ac:94 root@origin
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| o . |
| .E+oo o |
| . o*o+ S |
| +oo+.= . |
| o +. o |
|.. . . |
|... |
+-----------------+
[root@origin ~]#
[root@origin ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@origin ~]# ssh-copy-id 192.168.78.100
/usr/bin/ssh-copy-id: INFO: attempting to login with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.78.100's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.78.100'"
and check to make sure that only the key(s) you wanted were added.
[root@origin ~]#
[root@origin ~]# echo "local to remote">local.txt
[root@origin ~]# scp local.txt 192.168.78.100:/root/
The authenticity of host '192.168.78.100 (192.168.78.100)' can't be established.
ECDSA key fingerprint is c1:b8:67:1f:1d:c0:cd:6b:37:90:42:b1:c6:5a:e8:cf.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.78.100' (ECDSA) to the list of known hosts.
root@192.168.78.100's password:
local.txt 100% 16 0.0KB/s 00:00
[root@heimatengyun ~]# screen -S test
[root@heimatengyun ~]#tail -f /var/log/messages
此时直接断开或关闭会话窗口,模拟异常断开的情况。
再次远程登录系统,在会话窗口中通过以下命令查看上次的会话,并恢复会话
[root@origin ~]# screen -ls
There is a screen on:
49170.test (Detached)
1 Socket in /var/run/screen/S-root.
[root@origin ~]# screen -r test
[root@origin ~]# tail -f /var/log/messages
Jan 5 19:40:01 origin systemd: Starting Session 77 of user root.
Jan 5 19:40:01 origin systemd: Started Session 77 of user root.
Jan 5 19:42:37 origin systemd-logind: Removed session 76.
Jan 5 19:42:39 origin systemd-logind: New session 78 of user root.
Jan 5 19:42:39 origin systemd: Starting Session 78 of user root.
... 省略部分内容
[root@heimatengyun ~]# ssh 192.168.78.104
root@192.168.78.104's password:
Last login: Sun Jan 5 20:41:31 2020 from 192.168.78.1
[root@origin ~]# screen -S test