Ubuntu 网络配置方法

修改网卡名称的传统命名方式:

将网卡的默认命名方式修改为ethX这种形式的名称

#1. 编辑配置文件:/etc/default/grub --->GRUB_CMDLINE_LINUX="net.ifnames=0"

#2. 生成配置文件:grub-mkconfig -o /boot/grub/grub.cfg或update-grub  #centos里面是grub2

#3. 重启生效: reboot
#修改配置文件的值
tom@ubuntu1604:~$ cat /etc/default/grub |grep -v "^#"

GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=lsb_release -i -s 2> /dev/null || echo Debian
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="net.ifnames=0"

#生成配置文件
tom@ubuntu1604:~$ sudo grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...

Found linux image: /boot/vmlinuz-4.4.0-142-generic
Found initrd image: /boot/initrd.img-4.4.0-142-generic
done

Ubuntu网卡配置

  • ubuntu1604网卡配置
  • ubuntu1804网卡配置

Ubuntu1604网卡配置

ubuntu1604 给网卡设置动态ip

编辑 /etc/network/interfaces这个文件配置。

auto 网卡名称
iface 网卡名称 inet dhcp
tom@ubuntu1604:~$ cat /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 eth0
iface eth0 inet dhcp

注意:NetworkManager工具是Ubuntu桌面版的GUI设置工具。

ubuntu1604 给网卡设置静态ip
tom@ubuntu1604:~$ cat /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 eth0
#iface eth0 inet dhcp

auto eth0   #auto 表示系统启动(boot)时 自动加载eth0接口
iface eth0 inet static  #接口etho 用作 静态ip地址
address 10.0.0.16 #ip地址
netmask 255.255.255.0 #子网掩码
gateway 10.0.0.2 #默认网关
dns-nameserver 180.76.76.76 #dns服务器地址

修改网络配置文件后生效:重启networking服务

tom@ubuntu1604:~$ sudo systemctl restart networking.service
或者
tom@ubuntu1604:~$ service networking restart
或者
sudo /etc/init.d/networking restart
tom@ubuntu1604:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:20:9c:f0
          inet addr:10.0.0.16  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe20:9cf0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:261 errors:0 dropped:0 overruns:0 frame:0
          TX packets:129 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:239962 (239.9 KB)  TX bytes:15405 (15.4 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:160 errors:0 dropped:0 overruns:0 frame:0
          TX packets:160 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:11840 (11.8 KB)  TX bytes:11840 (11.8 KB)

Ubuntu1804网卡配置

自 ubuntu 17.10 开始,Ubuntu 已放弃在 /etc/network/interfaces 里设置静态 IP 的办法了,即使配置也不会生效,而是改成 netplan 方式 。

配置写在 /etc/netplan/01-network-manager-all.yaml 或者类似名称的 yaml 文件里(此处请注意 yaml 的语法格式)
  • 配置文件路径:/etc/netplan/XXX.yaml #配置文件的名字可以随意起
可以一个配置文件对应一个网卡,也可以一个配置文件对应多个网卡。

网卡配置文件的路径:/etc/netplan/xx.yaml

  • 里面创建的文件必须以yaml结尾的文件。
  • 一般来说,不同级别的缩进是两个空格,可以有任何空格。
    [En]

    generally, different levels of indentation are two spaces, and you can have any space.*

网卡配置文件的编写格式:

配置自动获取IP方法

network:              #固定格式  version: 2          #版本号  renderer: networkd  #固定格式  ethernets:          #网络类型为以太网    eth0:             #网卡名      dhcp4: yes      #ipv4的dhcp自动获取                      #键值对冒号后面要有空格 !#yaml格式的文件缩进严格,同级别要保持同样的缩进(一般缩进两个空格,当然也可以是多个空格),错误的格式会导致网络配置文件不起作用。

配置静态ip

范例:

#vim /etc/netplan/01-netcfg.yamlnetwork:                          #固定格式  version: 2                      #版本号  renderer: networkd  ethernets:    eth0:                         #网卡名      addresses:                  #配置多个地址就写多个ip地址      - 192.168.8.10/24           #需要配置的ip地址      - 10.0.0.10/8               #这里给这个网卡配置了两个ip地址      gateway4: 10.0.0.2          #ipv4的网关配置      nameservers:                #如果要配置DNS就需要加这个        search: [xxx.com, fdsfa.org]  #搜索域名,当访问地址写的不完整时,它会自动补全后缀(不需要就不写)        addresses:                #添加的DNS服务器地址,最多可以添加三个        - 180.76.76.76        - 8.8.8.8        - 1.1.1.1#addresses: [192.168.8.10/24,10.0.0.10/8] 这种写法也是可以的#列表(python中的语法):中括号里面有多个元素,用逗号隔开#列表的两种格式:    #格式一:所有元素卸载一个中括号里面以逗号隔开    #格式二:用横线开头,一个元素放一行。

范例

#配置192.168.26.128作为静态地址tom@ubuntu1804-1:~$ cat /etc/netplan/01-netcfg.yaml #01-netcfg这个名称可以随便起,为了方便管理可以命名为网卡的名称This file describes the network interfaces available on your systemFor more information, see netplan(5).network:  version: 2  renderer: networkd  ethernets:    ens33: #网卡名      addresses:      - 192.168.26.128/24 #ip地址      gateway4: 192.168.26.2 #网关      nameservers:        addresses: #配置的DNS服务器地址 最多可以配三个        - 192.168.26.129

修改网络配置文件后生效:netplan apply

Original: https://www.cnblogs.com/heyongshen/p/16462178.html
Author: 背对背依靠
Title: Ubuntu 网络配置方法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523147/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球