克拉拉最可爱了

导航

Linux ubuntu网络配置(学习笔记)

1.网卡名称修改

#修改配置文件为下面形式
root@ubuntu1804:~#vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
#或者sed修改
root@ubuntu1804:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub
root@maple-u18:~# grub-mkconfig -o /boot/grub/grub.cfg ;reboot

2. 配置静态IP

ubuntu在18.04版本中的网络配置已经由
/etc/network/interfaces 换成了/etc/netplan/*.yaml配置文件了。
网卡配置文件采用YAML格式,必须以 /etc/netplan/XXX.yaml 文件命名方式存放
yml后缀不可以。
可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里

root@ubuntu1804:~# vim /etc/network/interfaces
# ifupdown has been replaced by netplan(5) on this system. See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
# sudo apt install ifupdown

范例

vim /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [10.0.0.151/24,1.1.1.1/32] #和下面方式不能混合用
- 10.0.0.151/24
- 1.1.1.1/32
nameservers:
search: [maple.com,maple.org]
addresses:
- 114.114.114.114
- 8.8.8.8
gateway4: 10.0.0.2

生效:

netplan apply #加载网卡配置文件
ip a #查看地址是否生效
route -n #查看网关是否生效
cat /etc/resolv.conf #仅有search加到配置文件中了,dns没看到。
ll /etc/resolv.conf #可以看到在ubuntu中resolv文件由systemd统一管理了。
systemd-resolv --status #可以看到配置的DNS,
# resolvectl status Ubuntu 20.04新命令

search的作用:

vim /etc/host
127.0.0.1 localhost www.maple.com

root@ubuntu1804:/etc/netplan# ping www
PING www.maple.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.009 ms

3. 配置自动获取IP

vim /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes

4. 配置多⽹卡静态IP和静态路由

mv eth0.yaml netcfg.yaml
vim netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses:
- 10.0.0.151/24
- 1.1.1.1/32
nameservers:
search: [maple.com,maple.org]
addresses:
- 114.114.114.114
- 8.8.8.8
gateway4: 10.0.0.2
eth1:
dhcp4: no
dhcp6: no
addresses: [10.20.0.100/24]
nameservers:
addresses: [223.6.6.6]
routes:
- to: 10.30.0.0/24 #去往该网段的路由
via: 10.20.0.1 #下一跳为10.20.0.1
- to: 10.40.0.0/24
via: 10.20.0.1
- to: 10.50.0.0/24
via: 10.20.0.1
- to: 10.60.0.0/24
via: 10.20.0.1

73 history
root@ubuntu1804:/etc/netplan# !70
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:ad:3d:7d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.151/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 1.1.1.1/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fead:3d7d/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:ad:3d:87 brd ff:ff:ff:ff:ff:ff
inet 10.20.0.100/24 brd 10.20.0.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fead:3d87/64 scope link
valid_lft forever preferred_lft forever
root@ubuntu1804:/etc/netplan# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.20.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.30.0.0 10.20.0.1 255.255.255.0 UG 0 0 0 eth1
10.40.0.0 10.20.0.1 255.255.255.0 UG 0 0 0 eth1
10.50.0.0 10.20.0.1 255.255.255.0 UG 0 0 0 eth1
10.60.0.0 10.20.0.1 255.255.255.0 UG 0 0 0 eth1

5. 配置单网桥连接:

三个网卡在一个配置文件里

network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
dhcp6: no
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [10.0.0.161/24]
nameservers:
addresses: [114.114.114.114,8.8.8.8]
gateway4: 10.0.0.2
interfaces:
- eth1
- eth2

也可以一个网卡一个配置文件,网桥配置单独抽离出配置文件:

 

netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes

 

br0.yaml
network:
version: 2
renderer: networkd
ethernets:
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
dhcp6: no
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [10.0.0.161/24]
nameservers:
addresses: [114.114.114.114,8.8.8.8]
gateway4: 10.0.0.2
interfaces:
- eth1
- eth2

 

root@ubuntu1804:/etc/netplan# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:ad:3d:7d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.151/24 brd 10.0.0.255 scope global dynamic eth0
valid_lft 1634sec preferred_lft 1634sec
inet6 fe80::20c:29ff:fead:3d7d/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000
link/ether 00:0c:29:ad:3d:87 brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000
link/ether 00:0c:29:ad:3d:91 brd ff:ff:ff:ff:ff:ff
5: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 82:0c:13:fb:65:26 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.161/24 brd 10.0.0.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::800c:13ff:fefb:6526/64 scope link
valid_lft forever preferred_lft forever

br0将eth1和eth2连接到自身,和eth1和eth2网络在同一网段的能够相互通信,
我这里的IP分配的稍微有问题,eth0的dhcp地址池分配的ip也是10网段的了,导致路由表里面有10网段的路由有两条记录
并且我宿主机10.0.0.1ssh连不上 10.0.0.151eth0接口了。
在主机里面加一条主机路由到宿主机即可ssh
route add -host 10.0.0.1/32 dev eth0

配置多网桥同样配置,在bridges下加个br1即可。

 

posted on 2023-11-29 15:02  克拉拉最可爱了  阅读(421)  评论(0编辑  收藏  举报