domingo, 4 de junho de 2023

KVM: Criando uma rede em ponte com o NetPlan no Ubuntu 22.04

0 comentários

 Fonte: KVM: Creating a bridged network with NetPlan on Ubuntu 22.04 | Fabian Lee : Software Engineer


In order to expose KVM virtual machines on the same network as your bare-metal Host, you need to enable bridged networking.

In this article, I’ll show how to implement KVM bridged networking on Ubuntu 22.04 using Netplan.  This bridged network will expose the KVM Guest OS as a peer on the upstream network, with no limits on ingress/egress.

Prerequisites for Netplan

First, you need to have your system under Netplan control.  If the yaml file in your “/etc/netplan” directory has a renderer of ‘networkd’, then your system is under Netplan control and you can continue.

# you want Netplan renderer of 'networkd' (not 'NetworkManager')
sudo grep -sr 'renderer' /etc/netplan

If your system is using NetworkManager, then see the notes at the bottom of this article for switching over to Netplan.

Prerequisites for libvirt/KVM

If you haven’t installed KVM on Ubuntu, you can follow the instructions in my article here.

Although we don’t directly require it for NetPlan, install the network bridge utilities package for debugging.

sudo apt install bridge-utils -y

Create host bridge using NetPlan

By creating a bridged network, you can have guest VMs share the network interface of the Host machine.  In other words, the Host machine as well as the Guest virtual machine hosts can each have an IP address on the same subnet.

You need to use the tools provided at the OS level to create a network bridge on the Host.  On older versions of Ubuntu, you would use ‘brctl‘, but on 18.04+ you can use Netplan.

Look at existing NetPlan

Go into the “/etc/netplan” directory and you should see a file named “01-netcfg.yaml” or “50-cloud-init.yaml” that looks something like:

network:
  version: 2
  renderer: networkd

  ethernets:
    enp1s0:
      dhcp4: false
      dhcp6: false 
      addresses: [192.168.1.239/24]
      # gateway4 is deprecated, use routes instead
      routes:
      - to: default
        via: 192.168.1.1
        metric: 100
        on-link: true
      mtu: 1500
      nameservers:
        addresses: [8.8.8.8]

If instead you have a file named something like ’01-network-manager-all.yaml’ and the renderer is ‘NetworkManager’, rename it with an extension of ‘.old’ so it is not picked up anymore.

The definition above is for a host with a single physical NIC named ‘enp1s0’ (yours might be ‘eth0’ or ‘enp2s0’), static IP of 192.168.1.239 on the 192.168.1.1 network and using Google DNS servers at 8.8.8.8.

Created a bridge with Netplan

To create a bridged network, you need to disable the specific settings on the physical network, and instead apply them to the bridge.  Make a backup of your old file before modifying.

cd /etc/netplan

# make backup
sudo cp 50-cloud-init.yaml 50-cloud-init.yaml.orig

# modify, add bridge
sudo vi 50-cloud-init.yaml

Below is an example showing how we take the physical network example above (physical interfaced named ‘enp1s0’, might be ‘eth0’ for your system), and created a bridge named ‘br0’ that has the same properties.

network:
  version: 2
  renderer: networkd

  ethernets:
    enp1s0:
      dhcp4: false 
      dhcp6: false 

  bridges:
    br0:
      interfaces: [enp1s0]
      addresses: [192.168.1.239/24]
      # gateway4 is deprecated, use routes instead
      routes:
      - to: default
        via: 192.168.1.1
        metric: 100
        on-link: true
      mtu: 1500
      nameservers:
        addresses: [8.8.8.8]
      parameters:
        stp: true
        forward-delay: 4
      dhcp4: no
      dhcp6: no

Then apply the new netplan with bridged network with the commands below.  But be sure you have physical access to the host in case network connectivity needs to be investigated.

sudo netplan generate
sudo netplan --debug apply

You may temporarily lost network connectivity if you are connected over ssh after running the apply command.  So be sure you have physical access to the console.

You can see the network entities at the OS level by using these commands:

# show compact list of interfaces 'br0' should now exist
sudo networkctl -a
sudo networkctl status br0

# bridge control, will now show new 'br0'
brctl show

# ip list
ip a show br0
# show host routes, default will be 'br0' to gateway
ip route

# show arp table (IP to MAC)
arp -n

Configure libvirt network to use existing bridge

Now with the network bridge created at the OS level, you can configure a libvirt network to use this bridge.  Create a file named “host-bridge.xml” containing the following:

<network>
  <name>host-bridge</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>

Then create the network using these commands:

# current list of libvirt networks
virsh net-list

# create libvirt network using existing host bridge
virsh net-define host-bridge.xml
virsh net-start host-bridge
virsh net-autostart host-bridge

# state should be active, autostart, and persistent
virsh net-list --all

VM assigned to this network will be treated just like any other peer host.  For example, if your upstream gateway router is DHCP enabled, new VM will be assigned an IP from that pool if the guest OS is configured to use DHCP.  Likewise, the guest OS can be configured to use a static IP from the same subnet as the host bridge.

KVM Guest OS

The last step is to configure a KVM Guest to use this network bridge.

From virt-manager, select the network source ‘Virtual network host-bridge: Bridge network’ as shown above.  Or if you ‘virsh edit’ the KVM guest, then set the <interface type=’network’> and <source network=’host-bridge’/> using similar values to below.

<interface type='network'>
    <source network='host-bridge'/>
    <model type='virtio'/>
    <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

Note that this only assigns the interface at the firmware level.  You still have to ensure proper configuration at the Guest OS level in order to use DHCP or static IP.

Read my article on cloning Ubuntu guest VMs for tips, configuring older Ubuntu versions is well understood at this point but newer Ubuntu versions that use systemd for networking do not use the MAC address as the DHCP id which can lead to issues when cloning.

Continue reading →
sábado, 28 de janeiro de 2023

Montar Windows no Linux

0 comentários

 

sudo apt install cifs-utils

sudo mount -t cifs -o credentials=/etc/win-credentials,uid=1000,gid=1000,dir_mode=0777,file_mode=0777 //192.168.31.161/Nova

Continue reading →
domingo, 1 de janeiro de 2023

Como instalar a última versão do qBittorrent no Ubuntu

0 comentários

 Fonte: qBittorrent no Ubuntu - veja como instalar a última versão (edivaldobrito.com.br)


Instalando o qBittorrent no Ubuntu server

Você pode instalar o daemon qBittorrent Ubuntu server e gerenciá-lo através da interface Web do qBittorrent. Para isso, faça o seguinte:

Passo 1. Abra um terminal (use as teclas CTRL + ALT + T);
Passo 2. Se ainda não tiver, adicione o repositório do programa com este comando;

sudo add-apt-repository ppa:qbittorrent-team/qbittorrent-stable

Passo 3. Atualize o APT com o comando:

sudo apt-get update

Passo 4. Agora instale o programa com o comando:

sudo apt-get install qbittorrent-nox

Passo 5. Uma vez instalado, crie um usuário para o qBittorrent;

sudo adduser qbtuser

Passo 6. Você será solicitado para definir a senha para este usuário. Em seguida, adicione este usuário ao grupo sudo.

sudo gpasswd -a qbtuser sudo

Passo 7. Em seguida, crie um arquivo de serviço systemd qbittorrent-nox, usando este comando;

echo -e '[Unit]\n Description=qBittorrent Daemon Service\n After=network.target\n \n [Service]\n Type=forking\n User=qbtuser\n ExecStart=/usr/bin/qbittorrent-nox -d\n \n [Install]\n WantedBy=multi-user.target'| sudo tee /etc/systemd/system/qbittorrent-nox.service

Passo 8. Agora, inicie o qBittorrent-nox:

sudo systemctl start qbittorrent-nox

Passo 9. Você também pode querer ativar a inicialização automática do serviço quando o Ubuntu iniciar;

sudo systemctl enable qbittorrent-nox

Passo 10. Se quiser verificar o status do serviço, use este comando:

systemctl status qbittorrent-nox

Passo 11. Com o serviço ativo, você já pode acessar a interface Web, digitando na barra de endereços do navegador, o endereço ip do servidor seguido de :8080, como no exemplo abaixo;

ip-do-servidor:8080

Como instalar a última versão do qBittorrent no Ubuntu

Continue reading →
segunda-feira, 3 de outubro de 2022

Monitoring Pi-hole with Prometheus

0 comentários

 Monitoring Pi-hole with Prometheus (observability.blog)


eko/pihole-exporter
A Prometheus exporter for PI-Hole’s Raspberry PI ad blocker - eko/pihole-exporter

pihole-exporter is a Go application that exports Pi-hole metrics.

The first step is downloading the correct release. In my case I run Pi-hole on a Debian VM so I downloaded the amd64 release. If you run Pi-hole on a Raspberry Pi you will need the arm release.
https://github.com/eko/pihole-exporter/releases

# amd64 ver. 0.0.8
wget https://github.com/eko/pihole-exporter/releases/download/0.0.8/pihole_exporter-linux-amd64

# arm ver. 0.0.8
wget https://github.com/eko/pihole-exporter/releases/download/0.0.8/pihole_exporter-linux-arm

Once you have the the executable downloaded lets create a space for it

mkdir /opt/pihole_exporter

Create a system user to run the exporter

useradd -r pihole_exporter

Move the executable to the folder we created

mv pihole_exporter-linux-amd64 /opt/pihole_exporter

Make sure to set the correct permissions

chgrp pihole_exporter /opt/pihole_exporter
chgrp pihole_exporter /opt/pihole_exporter/pihole_exporter-linux-amd64
chmod +x /opt/pihole_exporter/pihole_exporter-linux-amd64
# NOTE: if you downloaded the arm release make sure you change the file name

Create a service file

nano /lib/systemd/system/pihole_exporter.service
systemctl daemon-reload
service pihole_exporter start
systemctl enable pihole_exporter
[Unit]
Description=pihole_exporter

[Service]
ExecStart=/opt/pihole_exporter/pihole_exporter-linux-amd64
WorkingDirectory=/opt/pihole_exporter
Restart=always
User=pihole_exporter

[Install]
WantedBy=multi-user.target
Service File

The exporter should now be running on port 9617

curl 127.0.0.1:9617

All thats left is to setup the Prometheus job

- job_name: pihole
  static_configs:
  - targets:
    - 10.0.4.9:9617

The Github repo also provides a Grafana Dashboard.
https://github.com/eko/pihole-exporter/blob/master/grafana/dashboard.json

Continue reading →