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实现远程免密登录:
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: #另一台主机的密码
and check to make sure that only the key(s) you wanted were added.
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
authorized_keys known_hosts
实现免密登录之后就能远程免密拷贝:
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的主要内容,如果未能解决你的问题,请参考以下文章