1、修改主机名
vim /etc/hostname
2、把主机名与ip做映射
vim /etc/hosts
最后要记得重启主机reboot
。
3、修改IP地址
先有ifconfig
命令查看网卡名字
知道网卡是ens33后,修改配置文件
vim /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto ens33
#iface ens33 inet dhcp
auto ens33
iface ens33 inet static
#设置这里的静态ip
address 192.168.37.103
#子网掩码
netmask 255.255.255.0
#网关
gateway 192.168.37.2
#DNS
dns-nameservers 8.8.8.8
特别注意,如果你用的是虚拟机,gateway需要看你虚拟机虚拟网络的设置
我这里用的是NAT模式,点击NAT设置
gateway的网关IP和子网掩码就在上面
修改完端口文件后还要修改DNS(可选,因为我改了后一重启文件又恢复原样)
vim /etc/resolv.conf
添加或修改如下内容
#Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#DO NOT EDIT THIS FILE BY HAND – YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
#上面的内容是原来就有的,下面是新加的内容。
nameserver 114.114.114.114
nameserver 8.8.8.8
然后记得要重启ssh服务
- 重启网络服务
sudo service networking restart
- 重启DNS服务
sudo service resolvconf restart
最后要记得reboot
重启。
参考链接
【Ubuntu】 Ubuntu16.04修改主机名和查看主机名的方法
ubuntu修改配置IP地址和DNS的方法总结(4种)