搬砖小抄

ubuntu网络配置

字数统计: 595阅读时长: 3 min
2018/07/11 Share

ubuntu 16 是通过修改/etc/network/interfaces来配置网络接口信息,ubuntu 18 网络配置工具换成了netplan,对应的配置文件也换成了/etc/netplan/*.yaml

ubuntu 16

修改/etc/network/interfaces,按照模版改就可以了:

1
2
3
4
5
6
7
8
9
10
11
# config interfaces for : lo
auto lo
iface lo inet loopback

# config interfaces for : eth0
auto eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx(enter your ip here)
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx(enter gateway ip here,usually the address of the router)
dns-nameservers xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx

改完了重启对应的网络接口就行了

1
sudo ifdown eth0 && sudo ifup eth0

ubuntu 18

ubuntu 18 网络配置工具换成了netplan,配置文件是yaml格式。也是照着模版改就行了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# This file describes the network interfaces available on your system
# For more information, see netplan(5).

network:
version: 2
ethernets:
ens160: # 此接口DHCP
addresses: []
dhcp4: true
optional: true
ens192: # 此接口静态IP
addresses:
- 192.168.0.8/24
gateway4: 192.168.0.1
nameservers:
addresses:
- 192.168.0.1
- 192.168.0.250
search: []
optional: true

改完了执行以下命令刷新配置

1
sudo netplan apply

除此之外,原来的ifconfig也不是默认安装了(需要可以手动安装sudo apt install net-tools),它的替代者是ip命令,比如看ip地址信息可以使用ip address,输出如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:b9:04:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.10.135/24 brd 192.168.10.255 scope global dynamic ens160
valid_lft 418191sec preferred_lft 418191sec
inet6 fe80::20c:29ff:feb9:4bc/64 scope link
valid_lft forever preferred_lft forever
3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:b9:04:c6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.228/24 brd 192.168.0.255 scope global ens192
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feb9:4c6/64 scope link
valid_lft forever preferred_lft forever

ip命令的其他参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
ip [ -force ] -batch filename
where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |
vrf | sr }
OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
-h[uman-readable] | -iec |
-f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
-4 | -6 | -I | -D | -B | -0 |
-l[oops] { maximum-addr-flush-attempts } | -br[ief] |
-o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
-rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}
CATALOG
  1. 1. ubuntu 16
  2. 2. ubuntu 18