ssh命令用於linux 的遠程登錄,其默認端口為:22,在工作中常常會修改掉這一默認端口的值。
scp命令用於遠程的文件相互拷貝。
scp遠程文件拷貝:
[root@jerry a]#ls #在a目錄下有如下文件
1.txt 2.txt 3.txt 4.txt 5.txt
[root@jerry 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.
root@192.168.27.128's password: #需要輸入另一台主機的密碼
test.txt 100% 0 0.0KB/s 00:00
[root@jerry a]#ls
1.txt 2.txt 3.txt 4.txt 5.txt test.txt
如果要拷貝的文件是目錄,則需要加上-r的選項,同時也可以從當前主機往另一台主機上載文件或者目錄:
[root@jerry a]#mkdir test
[root@jerry a]#ls
1.txt 2.txt 3.txt 4.txt 5.txt test test.txt
[root@jerry a]#scp -r test 192.168.27.128:/root/b #將當前的test目錄拷貝到另一台主機的/root/b中
root@192.168.27.128's password: #輸入另一台主機的密碼
[root@jerry a]#
在另一台主機上查看:
root@jerry 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 root@jerry
The key's randomart image is:
+--[ RSA 2048]----+
| o.. |
| .Eoo |
| ++o. . |
| ..o. o |
| S ..+o .|
| . o.o.o |
| . . .o..|
| o .... .|
| . .. |
+-----------------+
[root@jerry 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
root@192.168.27.129'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
[root@jerry ~]#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
