阅读 314

网络系列之 - Ubuntu网络配置

1.修改主机名

# 永久修改
root@nginx:~# hostnamectl set-hostname ubuntu
root@ubuntu:~# 

# 查看主机名(用hostname命令确认)
hong@ubuntu:~$ hostname
ubuntu
hong@ubuntu:~$ hostname -I
10.0.0.4
# 查看主机名(通过配置文件确认)
hong@ubuntu:~$ cat /etc/hostname 
ubuntu
# 查看主机名(通过环境变量确认)
hong@ubuntu:~$ echo $HOSTNAME
ubuntu

2.修改网卡名

  1. 修改网卡名称为传统命令方式:

范例:通过grub配置文件修改网卡名为eth

root@ubuntu:~# cat /etc/default/grub 
GRUB_CMDLINE_LINUX="net.ifnames=0"				# 网卡名修改项

范例:通过sed编辑grub配置文件

root@ubuntu:~# sed -i.bak ‘/^GRUB_CMDLINE_LINUX=/s#"net.ifnames=0"#‘ /etc/default/grub
  1. 让新的grub.cfg文件生效

范例:用命令生效

# 方式一:
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.cfg
# 方式二:
root@ubuntu:~# update-grub
root@ubuntu:~# grep net.ifnames  /boot/grub/grub.cfg

范例:重启服务器生效

root@ubuntu:~# reboot

3.网卡配置

官文:

3.1配置自动获取IP

  1. 配置网卡文件为DHCP自动获取IP
root@ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml 
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth0:
     dhcp4: yes
  1. 执行命令使其生效

root@ubuntu:~# netplan apply

3.2配置静态IP

  1. 修改配置文件
    root@ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml 
    # Let NetworkManager manage all devices on this system
    network:
      version: 2
      renderer: NetworkManager
      ethernets:
        eth0:
          dhcp4: no
          addresses: [10.0.0.51/24]
          gateway4: 10.0.0.2
          nameservers:
             addresses: [8.8.8.8,8.8.4.4]   
  1. 查看IP和Gateway
    root@ubuntu:~# ip addr  # 查看IP 或者简写为 ip a
    root@ubuntu:~# 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    100    0        0 eth0
    10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
  1. 查看DNS
    root@ubuntu:~# ls -l /etc/resolv.conf 
    lrwxrwxrwx 1 root root 39 3月  11 13:47 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
    root@ubuntu:~# systemd-resolve --status

3.3配置多网卡静态IP和静态路由

实例:

    # 配置多网卡静态IP和静态路由
    root@ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml 
    # Let NetworkManager manage all devices on this system
    network:
      version: 2
      renderer: NetworkManager
      ethernets:
        eth0:
          dhcp4: no
          addresses: [10.0.0.51/24]
          gateway4: 10.0.0.2
          nameservers:
             addresses: [8.8.8.8,8.8.4.4]
        eth1:
          dhcp4: no
          addresses: [192.168.40.245/24]
          nameservers:
             addresses: [223.6.6.6]
          routes:
          - to: 192.168.40.0/24
            via: 192.168.40.1
    # 使得配置立即生效
    root@ubuntu:~# netplan apply
    # 查看路由
    root@ubuntu:~# route -n

范例:给每个网卡配置单独配置文件

    root@ubuntu:~# cat /etc/netplan/01-eth0.yaml 
    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0: 
          dhcp4: yes
    root@ubuntu:~# cat /etc/netplan/02-eth1.yaml 
    network:
      version: 2
      renderer: networkd
      ethernets:
        eth1:
          addresses:
          - 10.20.0.51/16
          - 192.168.40.253/24
          gateway4: 10.0.0.2
          nameservers:
            addresses: [233.6.6.6] 
          dhcp4: no

3.4单网卡桥接

    root@ubuntu:~# apt install -y bridge-utils  # apt相当于yum
    root@ubuntu:~# dpkg -L bridge-utils         # dpkg相当于rpm	

3.5多网卡桥接

3.6双网卡绑定

3.7双网卡绑定+桥接

3.8网卡的多组绑定

3.9网卡多组绑定+多组桥接

原文:https://www.cnblogs.com/hony625/p/15090014.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐