linux网络相关命令汇总(6)

2019-09-23 09:14:56王冬梅

这里对方的80端口是开启并允许通信的。当对端端口没有开启时:

[root@centos7 ~]# telnet 10.0.1.251 81
Trying 10.0.1.251...
telnet: connect to address 10.0.1.251: No route to host

当对端拒绝连接时:

[root@centos7 ~]# telnet 10.0.1.251 8085
Trying 10.0.1.251...
telnet: connect to address 10.0.1.251: Connection refused

10、ssh 远程登录程序

ssh [OPTIONS]... [user@]hostname [command]

ssh的全称是Secure Shell,在不安全的网络主机间提供安全加密的通信,旨在代替其他远程登录协议。

[root@centos7 ~]# ssh 10.0.1.253
The authenticity of host '10.0.1.253 (10.0.1.253)' can't be established.
ECDSA key fingerprint is 96:bd:a3:a7:87:09:1b:53:44:4c:9b:b9:5f:b2:97:89.
Are you sure you want to continue connecting (yes/no)? yes #这里输入yes
Warning: Permanently added '10.0.1.253' (ECDSA) to the list of known hosts.
root@10.0.1.253's password:   #这里输入密码
Last login: Fri Nov 11 09:04:01 2016 from 192.168.78.137
[root@idc-v-71253 ~]#     #已登录

当命令ssh后直接跟主机IP时表示使用默认用户root登录,如果是首次登录,需要确认添加该主机的认证key,当输入yes后,即会在本机/root/.ssh/known_hosts中增加一条该主机的记录,下一次登录时就不用再次确认了。然后需要输入用户密码,通过验证之后,我们就获得了目的主机的一个shell,我们就可以在这个shell中执行命令了。
在新shell中输入exit即可退回到原shell。
如果需要频繁登录某主机,但不想每次都输入密码,可以设置免密码登录:

[root@centos7 ~]# ssh-keygen -t rsa
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:
be:c3:d0:02:50:35:35:fe:60:d6:2f:26:96:f0:e1:e6 root@centos7
The key's randomart image is:
+--[ RSA 2048]----+
| ...o.o  |
| . o o  |
| . . * .  |
| . * = .  |
|  . .S + . |
|  o=.o .  |
|  +E  |
|  o.  |
|  ..  |
+-----------------+
[root@centos7 ~]#
[root@centos7 ~]# ssh-copy-id 10.0.1.253
/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@10.0.1.253's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '10.0.1.253'"
and check to make sure that only the key(s) you wanted were added.
[root@centos7 ~]#

其中命令ssh-keygen用来生成公钥私钥,选项-t指明密钥类型。之后使用命令ssh-copy-id将公钥发送至目标主机,这里需要输入目标主机用户密码。然后就可以免密码登录了: