Ubuntu18.04 配置IP地址以及连接secureCRT
之前Ubuntu的IP动不动就变了,导致远程终端工具secureCRT
经常连接超时。IP地址是在系统启动时自动配置的,需要配置个静态的IP地址。
配置Ubuntu的IP地址 interfaces
1、备份interfaces配置文件
看自己喜好备份,个人喜欢修改某个文件前会做个备份。
配置文件所在位置:
/etc/network
hadoop@ubuntu:/etc/network$ sudo cp ./interfaces ./interfaces.bak
2、配置文件Interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
# IP
address 192.168.0.102
#网关
gateway 192.168.1.2
3、重启网络服务
hadoop@ubuntu:/etc/network$ sudo /etc/init.d/networking restart
报错:
hadoop@ubuntu:/etc/network$ sudo /etc/init.d/networking restart
[sudo] password for hadoop:
[....] Restarting networking (via systemctl): networking.serviceJob for networking.service failed because the control process exited with error code.
See "systemctl status networking.service" and "journalctl -xe" for details.
failed!
执行命令"systemctl status networking.service" 查看网络服务状态:
hadoop@ubuntu:/etc/network$ systemctl status networking.service
● networking.service - Raise network interfaces
Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor prese
Active: failed (Result: exit-code) since Sat 2024-05-04 20:31:48 +14; 4min 0s
Docs: man:interfaces(5)
Process: 2385 ExecStart=/sbin/ifup -a --read-environment (code=exited, status=
Process: 2382 ExecStartPre=/bin/sh -c [ "$CONFIGURE_INTERFACES" != "no" ] && [
Main PID: 2385 (code=exited, status=1/FAILURE)
May 04 20:31:48 ubuntu systemd[1]: Starting Raise network interfaces...
May 04 20:31:48 ubuntu ifup[2385]: Cannot find device "eth0"
May 04 20:31:48 ubuntu ifup[2385]: Failed to bring up eth0.
May 04 20:31:48 ubuntu systemd[1]: networking.service: Main process exited, code
May 04 20:31:48 ubuntu systemd[1]: networking.service: Failed with result 'exit-
May 04 20:31:48 ubuntu systemd[1]: Failed to start Raise network interfaces.
Cannot find device “eth0”
hadoop@ubuntu:/etc/network$ dmesg | grep eth
[ 4.599721] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:85:64:1b
[ 4.599727] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection
[ 4.762079] e1000 0000:02:01.0 ens33: renamed from eth0
修改配置文件interfaces为:
把eth0
修改为ens33
,如下:
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
# IP
address 192.168.0.102
#网关
gateway 192.168.1.2
重启网络的运行结果:
hadoop@ubuntu:/etc/network$ sudo /etc/init.d/networking restart
[ ok ] Restarting networking (via systemctl): networking.service.
4、使用 ifconfig 命令查看当前 IP,查看是否和刚刚修改和IP对应
修改主机名和 hosts 文件
当前主机名
hadoop@ubuntu:/etc$ cat -n hostname
1 ubuntu
hadoop@ubuntu:/etc$
当前hosts文件内容:
hadoop@ubuntu:/etc$ cat -n hosts
1 127.0.0.1 localhost
2 127.0.1.1 ubuntu
3
4 # The following lines are desirable for IPv6 capable hosts
5 ::1 ip6-localhost ip6-loopback
6 fe00::0 ip6-localnet
7 ff00::0 ip6-mcastprefix
8 ff02::1 ip6-allnodes
9 ff02::2 ip6-allrouters
hadoop@ubuntu:/etc$
1、主机名维持现状
2、配置 Linux 主机名称映射 hosts 文件, 打开/etc/hosts
添加如下内容:
192.168.0.102 ubuntu
3、重启 ubuntu
hadoop@ubuntu:/etc$ reboot
4、修改 windows 的主机映射文件(hosts 文件)
操作系统是 window10,先拷贝出来,修改保存以后,再覆盖即可。
①进入 C:\Windows\System32\drivers\etc 路径
添加如下内容:
# updated in May 2024
192.168.0.102 ubuntu
连接远程终端工具secureCRT
域名解析暂时失败
hadoop@ubuntu:~/Desktop$ ping baidu.com
ping: baidu.com: Temporary failure in name resolution
查看域名解析配置文件,如下:
hadoop@ubuntu:/etc$ less resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0
这个文件是被systemd-resolved
这个服务托管的,不要直接在这个文件编辑,这个服务的配置文件为/etc/systemd/resolved.conf
。
备份文件"resolved.conf":
hadoop@ubuntu:/etc/systemd$ sudo cp ./resolved.conf ./resolved.conf.bak
修改文件"resolved.conf":
[Resolve]
DNS=1.1.1.1 1.0.0.1
LLMNR=no
重启systemd-resolved,如下:
sudo systemctl restart systemd-resolved
ping了下网络,发现ping通了。
secureCRT连接超时
网络ping通之后,发现secureCRT总是连接超时。这个给我整无语了,解决这个问题,之前解决的问题又出现了。
检查ssh
查看ssh版本
hadoop@ubuntu:~$ ssh -V
OpenSSH_7.6p1 Ubuntu-4ubuntu0.5, OpenSSL 1.0.2n 7 Dec 2017
检查网络
网络没问题
hadoop@ubuntu:~$ ping baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=38.3 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=39.3 ms
^C
--- baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 38.359/38.858/39.358/0.536 ms
主机和虚拟机之间是否能ping通?
发现主机和虚拟机互相ping不同,总是显示无法访问目标主机。
最后看到win10的vm8的IP是192.168.1.1,和虚拟机的IP不在同一个网段上,如下:
192.168.0.102:11000000 10101000 00000000 01100110
255.255.255.0:11111111 11111111 11111111 00000000
与结果:11000000 10101000 00000000 00000000
192.168.1.1: 11000000 10101000 00000001 00000001
255.255.255.0:11111111 11111111 11111111 00000000
与结果:11000000 10101000 00000001 00000000
以上IP和子网掩码的两个与运算结果不同,表示子网号不同,不在同一个网段。
参考文章:解决 Secure CRT 连接超时问题
修改虚拟机IP,要和vm8的IP时同一个网段:
1、重新配置文件Interfaces
2、重启网络服务
3、重新配置 Linux 主机名称映射 hosts 文件
4、修改 windows 的主机映射文件(hosts 文件)
最后连接成功,如下图:
![连接secureCRT](https://img-blog.csdnimg.cn/direct/eeb8b2813d4a45f3ad6f11c9dfbe0dcb.png#pic_center)