You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aney.co.uk/guides/setup-kvm-bridge.html

118 lines
5.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="This guide covers how to set a KVM bridge network for use with Virtual Machines">
<meta name="keywords" content="Blog, articles, template">
<meta name="author" content="Nathan (Aney) Steel">
<meta name="theme-color" content="white">
<meta name="theme-color" content="black">
<link rel="stylesheet" type="text/css" href="/main.css">
<link rel="icon" type="image/png" href="/images/favicon.svg">
<title>Setup a bridge network for KVM</title>
</head>
<body>
<header>
<a href="#main" class="vh">Jump directly to main content</a>
<h1>Setup a bridge network for KVM</h1>
<input id="burger-toggle" type="checkbox"/>
<label class="burger-container" for="burger-toggle"><div class="burger"></div><span class="sr">Burger menu</span></label>
<hr/>
<nav>
<a href="/">home</a>
<a href="/about.html">about</a>
<a href="/projects.html">projects</a>
<a href="/blog/">blog</a>
<a href="/sitemap.html">misc</a>
<a href="/support.html">support</a>
</nav>
<hr/>
</header>
<main id="main">
<section>
<p class="intro">A bridge network is a means to connect/bridge different networks together to act as a single network. In this case, it allows any virtual connections to the bridge network to get their own internal IPs, as if plugged into the network directly.</p>
<h2>Install bridge-utils</h2>
<pre><code>sudo apt install bridge-utils</code></pre>
<h2>Find the network device to bridge</h2>
<p>First we need to find the network device we want to bridge to the VMs.</p>
<pre><code>ip link</code></pre>
<p>You'll likely have a device called something similar to <strong>enp1s0</strong>, or <strong>eth0</strong>.</p>
<h2>Create the bridge network</h2>
<p>This will be familiar to those who have <a href="/guides/set-static-ip.html">set a static ip</a> on linux, as it's essentially the same, with a few additional lines related to bridging</p>
<h3>Remove interface from interfaces file</h3>
<p>First step is to remove any references to your network device from <strong>/etc/network/interfaces</strong>, if this file is untouched there will likely be two lines at the bottom.</p>
<pre><code>sudo vim /etc/network/interfaces</code></pre>
<h3>Create a new bridge interface</h3>
<p>Now create a file in the <strong>/etc/network/interfaces.d/</strong> directory, with the name of your bridge (I like <strong>br0</strong>).</p>
<h4>Static IP</h4>
<pre><code>auto br0
iface br0 inet static # Name the same as your file (br0)
address 192.168.0.100
broadcast 192.168.0.255
netmask 255.255.255.0
gateway 192.168.0.1 # Normally your router's IP
dns-nameservers 192.168.0.1 8.8.8.8 8.8.4.4 # Don't set if resolveconf is installed, comment if internet borked
bridge_ports enp1s0 # Your device name
bridge_stp off # Disable Spanning Tree Protocol
bridge_waitport 0 # No delay before a port becomes available
bridge_fd 0 # No forwarding delay (Connects to network immediately)</code></pre>
<h4>Dynamic IP</h4>
<pre><code>iface br0 inet dhcp
bridge_ports enp2s0</code></pre>
<h2>(Optional) Create Virtual Network</h2>
<p>To make it easier to manage with VMs, this new bridge can also be made into a Virtual Network (Basically so you can select it from a dropdown).</p>
<h3>Create XML file</h3>
<p>Open up a text-editor, and create a file called <strong>br0.xml</strong>, named after the bridge itself.</p>
<pre><code>vim br0.xml</code></pre>
<p>Enter the following, then save</p>
<pre><code>&lt;network&gt;
&lt;name&gt;br0&lt;/name&gt;
&lt;forward mode="bridge"/&gt;
&lt;bridge name="br0"/&gt;
&lt;/network&gt;</code></pre>
<h3>Create the virtual network</h3>
<pre><code>sudo virsh net-define br0.xml</code></pre>
<p>Activate, and auto-start (on boot) the new network.</p>
<pre><code>sudo virsh net-start br0</code></pre>
<pre><code>sudo virsh net-autostart br0</code></pre>
<p>You can now remove the br0.xml file.</p>
<h2>Reload the network</h2>
<pre><code>sudo systemctl restart networking</code></pre>
<p>This may claim to have failed, but if checking with <strong>ip link</strong> shows the bridge, reboot. Essentially the bridge has been brought up, and it's trying to bring it up again (and can't), then throws an error.</p>
<pre><code>sudo reboot</code></pre>
<h3>Check it's there</h3>
<p>Run some checks and you should now see br0, with an IP4 address.</p>
<pre><code>ip a</code></pre>
<pre><code>ping google.co.uk</code></pre>
</section>
<section>
<h2>Useful Links</h2>
<ul>
<li><a href="https://ostechnix.com/how-to-find-available-network-interfaces-on-linux/">OSTechNix - Find available network interfaces</a></li>
<li><a href="https://www.cyberciti.biz/faq/how-to-configuring-bridging-in-debian-linux/">CyberCiti - Configure bridging in debian</a></li>
</ul>
</section>
</main>
<footer>
<hr/>
<p>Written by <a href="https://aney.co.uk" target="_blank" rel="noopener">@aney</a> with <a href="https://danluu.com/web-bloat/" target="_blank" rel="noopener">web bloat</a> in mind | <a href="https://github.com/Aney/website" target="_blank" rel="noopener">Source Code</a></p>
</footer>
</body>
</html>