建站优化是什么/东莞网站建设优化
Ubuntu18以上的版本,之前的配置方法失效。在此统一整理在各个发行版Linux上的静态IP配置方法。本文的配置都基于IPv4版本。涉及信息详情:
IP address: 192.168.1.252
Netmask: 255.255.0.0
Hostname: freeOcean.com
Domain name: freeOcean.com
Gateway: 192.168.1.1
DNS Server 1: 8.8.8.8
DNS Server 2: 114.114.114.114
在 RHEL/CentOS/Fedora 中配置静态IP:
首先有两个文件区别需要了解一下:
/etc/sysconfig/network
/etc/sysconfig/network-scripts/ifcfg-eth0
google了一个比较好的答案:
The file /etc/sysconfig/network typically contains general information about networking on the system. Should interfaces even be activated? Which ones? What is the primary interface? What is this system’s name and identity?
The other file you mention, /etc/sysconfig/network-scripts/ifcfg-eth0 is specific to the first ethernet interface, known in the linux world as eth0, and has details about whether to use DHCP or assign an IP address, whether THAT interface should be activated on boot, and so on. You will find one of these files for each interface on the system. Laptops for example often have wlan0 as a wireless ethernet devices, and many systems with multiple network cards will keep going with eth1, eth2 etc.
我的理解是,第一个文件是一个总体的信息,第二个是针对eth0这个网卡的具体配置,一个机器可能有多个网卡。路由策略会决定报文应该路由到哪个网卡。可以通过 ip route查看系统的网卡信息。
实际配置时,我们只需要配置具体的网卡信息(第二个文件)就行。
第一步: 注意网卡名称要与系统的对应可以先通过ip a 查看网卡名称。这里是eth0
vi /etc/sysconfig/network-scripts/ifcfg-eth0
打开编辑:
DEVICE="eth0"
BOOTPROTO="static"
DNS1="8.8.8.8"
DNS2="4.4.4.4"
GATEWAY="192.168.1.1"
HOSTNAME="freeOcean.com"
HWADDR="00:19:99:A4:46:AB"
IPADDR="192.68.1.252"
NETMASK="255.255.0.0"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="8105c095-799b-4f5a-a445-c6d7c3681f07"
上面的信息,只需修改下面几项:DNS可以只保留一个。
BOOTPROTO="static" #静态ip
DNS1="8.8.8.8"
DNS2=114.114.114.114 #dns服务器
IPADDR=192.168.1.252 #本机地址
NETMASK=255.255.0.0 #子网掩码
GATEWAY=192.168.1.1 #默认网关
ONBOOT="yes" #随系统启用
第二步:
vim etc/resolv.conf
编辑其中中的信息为(和上面的配置对应即可):
nameserver 8.8.8.8 # Replace with your nameserver ip
nameserver 114.114.114.114 # Replace with your nameserver ip
第三步:重启网络(以下方式任选一个)
/etc/init.d/network restart [On SysVinit]
systemctl restart network [On SystemD]
在Debian / Ubuntu中设置静态IP
我在Ubuntu18上的配置,发现之前的方式失效。先给出早期版本的配置方式:
1.打开配置文件:
nano /etc/network/interfaces
默认显示的内容如下:
auto eth0
iface eth0 inet dhcp
编辑为自己的信息
auto eth0
iface eth0 inet static
address 192.168.1.252
netmask 255.255.0.0
gateway 192.168.1.1
dns-nameservers 114.114.114.114
dns-nameservers 8.8.8.8
2.编辑/etc/resolv.conf
nano /etc/resolv.conf
nameserver 8.8.8.8 # Replace with your nameserver ip
nameserver 114.114.114.114 # Replace with your nameserver ip
3.重启网络
/etc/init.d/network restart [On SysVinit]
systemctl restart network [On SystemD]
Ubuntu18上的配置:
下面介绍一下在18.04上新采用的netplan命令。网卡信息配置在/etc/netplan/01-network-manager-all.yaml文件,需做如下配置,注意要注释render
#Let NetworkManager manage all devices on this system
network:
version: 2
#renderer: NetworkManager
ethernets:
ens33:
addresses: [192.168.0.111/24]
gateway4: 192.168.0.1
nameservers:
addresses: [192.168.0.1]
然后使用以下命令使配置即时生效,
netplan apply
以上操作均在root用户下进行,如在普通用户,请自行加上sudo。
yaml的语法是采用缩进表示,接触SpringBoot项目的应该不陌生。
参考文档: