侧边栏壁纸
博主头像
蔚然小站博主等级

未来会有的,不要辜负了梦想

  • 累计撰写 36 篇文章
  • 累计创建 14 个标签
  • 累计收到 9 条评论

目 录CONTENT

文章目录
kvm

虚拟化网络学习

皮蛋熊
2023-08-24 / 0 评论 / 0 点赞 / 63 阅读 / 3780 字

网桥

配置网桥是为了让虚拟机都能获取到一个路由器分配的 IP 地址,使得可以通过 IP 直接连接,而非转发。

[!Warning] 此方法不推荐

[!note] 此方法推荐

  1. 建立网桥 vim /etc/netplan/01-network-manager-all.yaml 填入如下信息 (确保 MAC 地址和网络接口选对,148 上 enp5s0,88 上是 enp4s0):
network:
  ethernets:
    enp5s0:
      dhcp4: false
      dhcp6: false
  # add configuration for bridge interface
  bridges:
    br0:
      interfaces: [enp5s0]
      dhcp4: true
      macaddress: 58:11:22:46:df:5e
      dhcp6: false
  version: 2
  renderer: NetworkManager

netplan apply 即可生效 验证网桥 br0: ip addr show

  1. 建立 tap(每一个虚拟机用一个)
sudo apt install uml-utilities
tunctl -t tap0 -u jet 
brctl addif br0 tap0 
ifconfig tap0 0.0.0.0 promisc up

缺点是每次重启都需要创建一次,可以使用下面的方法建立一个系统服务,使得每次系统启动后自动创建: 参考:【Ubuntu】添加虚拟网卡的三种方式_ubuntu 虚拟网卡_remo0x的博客-CSDN博客

$ cat /usr/local/bin/config_tap.sh

#!/bin/bash
#
# config_tap          Start up the tun/tap virtual nic
#
# chkconfig: 2345 55 25

USER="root"
BRIDGE_DEVICE="br0"
TAP_DEV_NUM=0
DESC="TAP config"

do_start() {
  if [ ! -x /usr/bin/tunctl ]; then
    echo "/usr/bin/tunctl was NOT found!"
    exit 1
  fi
  tunctl -t tap$TAP_DEV_NUM -u $USER
  brctl addif $BRIDGE_DEVICE tap$TAP_DEV_NUM
  ifconfig tap$TAP_DEV_NUM ${TAP_NETWORK} 0.0.0.0 promisc up
}

do_stop() {
  brctl delif $BRIDGE_DEVICE tap$TAP_DEV_NUM
  ip link set tap$TAP_DEV_NUM nomaster
  ifconfig tap$TAP_DEV_NUM down 
}
do_restart() {
  do_stop
  do_start
}
check_status() {
  ifconfig tap$TAP_DEV_NUM 
}

case $1 in 
  start)    do_start;;
  stop)     do_stop;;
  restart)  do_restart;;
  status)
            echo "Status of $DESC: "
            check_status
            exit "$?"
            ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1 
esac

将上述脚本加入到系统服务中 (ubuntu 20.04 使用的是 systemd):

$ cat /etc/systemd/system/config_tap.service

[Unit]
Description=config tap device
After=network.target auditd.service sshd.service

[Service]
ExecStart=/usr/local/bin/config_tap.sh start
ExecReload=/usr/local/bin/config_tap.sh restart
ExecStop=/usr/local/bin/config_tap.sh stop
Restart=on-failure
RestartPreventExitStatus=255
Type=simple

[Install]
WantedBy=multi-user.target
Alias=config_tap.service
0
kvm
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区