服务器环境需要有静态的 IP 用于SSH登陆,本文记录 ubuntu 系统下设置静态IP的方法。
ifconfig
命令可以查看当前网络配置$ ifconfig
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.4 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::7285:c2ff:fe80:45d8 prefixlen 64 scopeid 0x20<link>
inet6 2409:8a1e:8fc7:3bc0:7285:c2ff:fe80:45d8 prefixlen 64 scopeid 0x0<global>
ether 70:85:c2:80:45:d8 txqueuelen 1000 (以太网)
RX packets 8721 bytes 6897773 (6.8 MB)
RX errors 0 dropped 1047 overruns 0 frame 0
TX packets 6367 bytes 1362777 (1.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (本地环回)
RX packets 961 bytes 94772 (94.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 961 bytes 94772 (94.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp1s0
为网卡的 logical name
sudo lshw -C network
命令$ sudo lshw -C network
*-network
description: Ethernet interface
product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vendor: Realtek Semiconductor Co., Ltd.
physical id: 0
bus info: pci@0000:01:00.0
logical name: enp1s0
version: 11
serial: 70:85:c2:80:45:d8
size: 1Gbit/s
capacity: 1Gbit/s
width: 64 bits
clock: 33MHz
capabilities: pm msi pciexpress msix vpd bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
configuration: autonegotiation=on broadcast=yes driver=r8169 driverversion=5.11.0-40-generic duplex=full firmware=rtl8168g-2_0.0.1 02/06/13 ip=192.168.1.222 latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s
resources: irq:22 ioport:e000(size=256) memory:91304000-91304fff memory:91300000-91303fff
*-network DISABLED
description: Ethernet interface
physical id: 1
logical name: virbr0-nic
serial: 52:54:00:5a:47:3b
size: 10Mbit/s
capabilities: ethernet physical
configuration: autonegotiation=off broadcast=yes driver=tun driverversion=1.6 duplex=full link=no multicast=yes port=twisted pair speed=10Mbit/s
logical name
为网卡名这里我的网卡为
enp1s0
/etc/netplan/**.yaml
,不同电脑配置文件名不同,我的是01-network-manager-all.yaml
enp1s0
添加配置内容
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
enp1s0:
addresses:
- 192.168.1.222/24
gateway4: 192.168.1.1
nameservers:
addresses: [114.114.114.114]
意思是在以太网接口
enp1s0
下,添加ipv4地址192.168.1.222,网关192.168.1.1,dsn服务器114.114.114.114。dsn服务器是必须要填的,但是每一行的缩进不是严格要求的.
sudo netplan apply
有时 netplan 文件夹下没有文件,可以采用如下方法
/etc/network/interfaces
(配置IP和网关)/etc/resolv.conf
(配置DNS服务器)/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 enp1s0
allow-hotplug enp1s0
# iface enp1s0 inet dhcp
iface enp1s0 inet static
address 192.168.1.111
netmask 255.255.255.0
gateway 192.168.1.1
broadcast 192.168.1.255
# This is an autoconfigured IPv6 interface
iface enp1s0 inet6 auto
配置 | 含义 |
---|---|
auto enp1s0 | 开机自动连接网络 |
iface enp1s0 inet static | 设置静态IP |
iface enp1s0 inet dhcp | dhcp 自动获取IP |
address 192.168.1.111 | 静态 IPv4地址 |
netmask 255.255.255.0 | 子网掩码 |
gateway 192.168.1.1 | 网关 |
broadcast 192.168.1.255 | 广播地址(也可以不写) |
/etc/resolv.conf
文件,设置 dnsnameserver 192.168.1.1
nameserver 114.114.114.114
service networking restart
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 70:85:c2:80:45:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.222/24 brd 192.168.1.255 scope global noprefixroute enp1s0
valid_lft forever preferred_lft forever
inet6 2409:8a1e:8fc7:3bc0:7285:c2ff:fe80:45d8/64 scope global dynamic mngtmpaddr
valid_lft 240888sec preferred_lft 154488sec
inet6 fe80::7285:c2ff:fe80:45d8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
inet 192.168.1.222/24
表明修改静态IP成功