跳到主要内容

Linux 设置静态 IP

Debian

vi /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
allow-hotplug eno1

iface eno1 inet static
address 10.8.40.130
netmask 255.255.255.0
gateway 10.8.40.1
dns-nameservers 114.114.114.114 223.5.5.5

Ubuntu

Ubuntu 22

编辑 /etc/netplan/下的yaml文件

这里文件名是 00-installer-config.yaml

sudo vi /etc/netplan/00-installer-config.yaml

network:
version: 2
ethernets:
eno1:
dhcp4: no
addresses:
- 10.8.40.125/24
routes:
- to: default
via: 10.8.40.1
nameservers:
addresses:
- 114.114.114.114
- 223.5.5.5
search: [localdomain]
optional: true
eno2:
dhcp4: true
eno3:
dhcp4: true
eno4:
dhcp4: true

sudo netplan apply

Ubuntu 18.04 +

原来设置 /etc/network/interfaces 的方法还可以用,只是设置的dns没有用 新的配置使用 netplan 管理

修改配置文件

编辑 /etc/netplan/下的yaml文件

这里文件名是 01-network-manager-all.yaml

sudo vi /etc/netplan/01-network-manager-all.yaml

# 注释掉 renderer:NetworkManager

network:
version: 2
ethernets:
# 网络名
enp0s3:
# 一个ip数组,用 ‘,’ 隔开
addresses: [10.8.40.119/24]
# 使用dhcp 动态获取ip: true/no
dhcp4: no
# ipv4 网关
gateway4: 10.8.40.1
# dns
nameservers:
addresses: [114.114.114.114]
search: [localdomain]
optional: true

# 立即生效
sudo netplan apply

Ubuntu 16

修改 /etc/network/interfaces

sudo vi  /etc/network/interfaces
auto eno4
iface eno4 inet static
address 10.8.30.176
netmask 255.255.255.0
gateway 10.8.30.1
dns-nameserver 114.114.114.114

配置 /etc/resolv.conf

以上配置完成就可以了 如果是desktop 版本可能设置的dns不能使 /etc/resolv.conf生效. 重启又恢复到默认

安装 resolvconf 服务

sudo apt-get update
sudo apt-get install resolvconf

配置 resolvconf

修改 /etc/resolvconf/resolv.conf.d/head

vi /etc/resolvconf/resolv.conf.d/head
# 增加
nameserver 223.5.5.5
nameserver 223.6.6.6

保存退出,执行

resolvconf -u

查看 /etc/resolv.conf, 重启再看

cat /etc/resolv.conf

CentOS

配置文件在 /etc/sysconfig/network-scripts

这里测试机文件名为:ifcfg-enp0s3

修改 ifcfg-enp0s3 文件

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

默认配置

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=64a31304-95d8-4ab6-961e-8d5db92f8cc0
DEVICE=enp0s3
ONBOOT=no

修改为

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
#BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=64a31304-95d8-4ab6-961e-8d5db92f8cc0
DEVICE=enp0s3
#ONBOOT=no

#static assignment
NM_CONTROLLED=no
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.116
NETMASK=255.255.255.0
GATEWAY=192.168.0.1

修改/etc/sysconfig/network

默认为空

# Created by anaconda
NETWORKING=yes
GATEWAY=192.168.0.1
DNS1=114.114.114.114

重启服务

service network restart

补充

查看网关

# 查看网关
netstat -rn
# 或
route -n

设置默认网关

route add default gw 10.8.30.1

重启网关

/etc/init.d/networking restart