ssh与scp

Posted getbird

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh与scp相关的知识,希望对你有一定的参考价值。

ssh命令用于linux 的远程登录,其默认端口为:22,在工作中常常会修改掉这一默认端口的值。

scp命令用于远程的文件相互拷贝。


 

scp远程文件拷贝

[[email protected] a]#ls            #在a目录下有如下文件

1.txt  2.txt  3.txt  4.txt  5.txt
[[email protected] a]#scp 192.168.27.128:/root/a/test.txt .      #将另一台主机的test.txt文件拷贝到当前目录

The authenticity of host ‘192.168.27.128 (192.168.27.128)‘ can‘t be established.
ECDSA key fingerprint is 17:5a:69:db:e5:ab:6c:35:fb:a6:f2:4c:c8:bc:d3:c2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.27.128‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:     #需要输入另一台主机的密码
test.txt                                                                                             100%    0     0.0KB/s   00:00   
[[email protected] a]#ls
1.txt  2.txt  3.txt  4.txt  5.txt  test.txt

如果要拷贝的文件是目录,则需要加上-r的选项,同时也可以从当前主机往另一台主机上载文件或者目录:

[[email protected] a]#mkdir test
[[email protected] a]#ls
1.txt  2.txt  3.txt  4.txt  5.txt  test  test.txt
[[email protected] a]#scp -r test 192.168.27.128:/root/b    #将当前的test目录拷贝到另一台主机的/root/b中
[email protected]‘s password:     #输入另一台主机的密码
[[email protected] a]#

 

在另一台主机上查看:

[email protected] b]#ls
test


 

ssh实现远程免密登录:

[[email protected] b]#ssh-keygen    #生成密钥
Generating public/private rsa key pair.
Enter file in which 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:
35:ab:4f:5f:60:f6:e5:a3:14:56:82:1e:5c:29:87:96 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
|            o..  |
|          .Eoo   |
|          ++o. . |
|         ..o. o  |
|        S ..+o  .|
|         . o.o.o |
|        . .  .o..|
|         o .... .|
|          . ..   |
+-----------------+
[[email protected] b]#ssh-copy-id 192.168.27.129    #发送公钥到另一端
The authenticity of host ‘192.168.27.129 (192.168.27.129)‘ can‘t be established.
ECDSA key fingerprint is c0:d8:9b:d9:05:37:51:4e:42:16:1c:a2:17:6c:5a:b7.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in 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
[email protected]‘s password:     #另一台主机的密码
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh ‘192.168.27.129‘"
and check to make sure that only the key(s) you wanted were added.
 
查看验证:
[[email protected] b]#ssh 192.168.27.129    #完成免密之后无需密码即可登录
Last login: Tue Jul  2 19:48:11 2019 from 192.168.27.1
[[email protected] ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c2:9e:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.27.129/24 brd 192.168.27.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec2:9e7e/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:d9:3d:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:d9:3d:91 brd ff:ff:ff:ff:ff:ff
 
在被免密登录的一方的/root/.ssh中有如下文件:
[[email protected] ~]#ls .ssh
authorized_keys  known_hosts
存放的就是免密登录的主机和公钥的信息
 

实现免密登录之后就能远程免密拷贝:
[[email protected] b]#scp 192.168.27.129:/root/a/*.txt .
1.txt                                                                                                100%    0     0.0KB/s   00:00   
2.txt                                                                                                100%    0     0.0KB/s   00:00   
3.txt                                                                                                100%    0     0.0KB/s   00:00   
4.txt                                                                                                100%    0     0.0KB/s   00:00   
5.txt                                                                                                100%    0     0.0KB/s   00:00   
test.txt                                                                                             100%    0     0.0KB/s   00:00  
无需密码拷贝成功。

 

以上是关于ssh与scp的主要内容,如果未能解决你的问题,请参考以下文章

ssh免密登录与scp

ssh免密登录与scp

scp 通过 ssh 隧道打开

ssh无密登录rsync 服务器与服务器之间同步scp 服务器与服务器之间拷贝

SSH无密码登录及SCP

在带有 ssh、scp 或 sftp 的 jenkins 管道中使用 ssh 凭据