LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-28-2022, 07:30 AM   #1
kgQyJt5dRg
LQ Newbie
 
Registered: Oct 2022
Posts: 5

Rep: Reputation: 0
Kill switch for virtual machines on QEMU


I have my host machine running on Linux. I installed QEMU hypervisor on this machine. Then I created a virtual machine with Linux on it. I want to create a "kill switch" for the host machine so that my host machine AND virtual machine can access internet ONLY when host machine connected to internet via VPN. And if VPN is down then no internet should be neither on host nor on virtual machine.

I tried these settings for ufw:

#!/bin/bash
ufw --force reset
ufw default deny incoming
ufw default deny outgoing
ufw allow in on tun0
ufw allow out on tun0
ufw allow in on enpXXX from 192.168.1.0/24
ufw allow out on enpXXX to 192.168.1.0/24
ufw allow out on enpXXX to xxx.xxx.xxx.xxx port xxxx proto tcp
ufw allow in on enpXXX from xxx.xxx.xxx.xxx port xxxx proto tcp
ufw enable


It worked but only for host machine. And my virtual machine can still access internet even if VPN is down. I tryed to delete this block from ufw rules:

ufw allow in on enpXXX from 192.168.1.0/24
ufw allow out on enpXXX to 192.168.1.0/24


But in this case I dont have internet on my VM at all. Also I tried to write same rules for virbr0 instead of enpXXX in different combinations. But in all cases I have the same - VM accesses internet even if VPN is down. Or VM has no internet even if VPN is up. So any clues would be appreciated. I am new to this subject. So please excuse me if I do or ask dumb things

Last edited by kgQyJt5dRg; 10-28-2022 at 07:34 AM.
 
Old 10-28-2022, 11:54 AM   #2
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Your VM has its own network.
You did not state how the networking is configured for the VM, but it likely needs its own rules on the host.

Mine is bridged using virbr0 and has its own subnet on the host at 192.168.124.0/24. Thus on the host I can establish rules that communications from 192.168.124.1 (virbr0) are only allowed via tun0 and no other path.
 
Old 10-28-2022, 04:32 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,987

Rep: Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626
Make another VM of a firewall device and route to and out of it.
 
Old 11-02-2022, 08:03 PM   #4
kgQyJt5dRg
LQ Newbie
 
Registered: Oct 2022
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by computersavvy View Post
Your VM has its own network.
You did not state how the networking is configured for the VM, but it likely needs its own rules on the host.

Mine is bridged using virbr0 and has its own subnet on the host at 192.168.124.0/24. Thus on the host I can establish rules that communications from 192.168.124.1 (virbr0) are only allowed via tun0 and no other path.
Thank you for your reply!
My VM is bridged using virbr0 and its subnet is 192.168.122.0/24
But I have difficulties with writing the rules for UFW anyway.

I tried this:

Code:
ufw --force reset
ufw default deny incoming
ufw default deny outgoing
ufw allow in on tun0
ufw allow out on tun0
ufw allow in on enpXXX from 192.168.122.0/24
ufw allow out on enpXXX to 192.168.122.0/24
ufw allow out on virbr0 to xxx.xxx.xxx.xxx port xxxx proto tcp
ufw allow in on virbr0 from xxx.xxx.xxx.xxx port xxxx proto tcp
ufw enable
or this:

Code:
ufw --force reset
ufw default deny incoming
ufw default deny outgoing
ufw allow in on tun0
ufw allow out on tun0
ufw allow in on virbr0 from 192.168.122.0/24
ufw allow out on virbr0 to 192.168.122.0/24
ufw allow out on virbr0 to xxx.xxx.xxx.xxx port xxxx proto tcp
ufw allow in on virbr0 from xxx.xxx.xxx.xxx port xxxx proto tcp
ufw enable
But still I have the same - it doesn't work at all or it works allways.
Could you please tell me more exactly how should I write it?
I really would appreciate.
Sorry, I'm really new to it...

Quote:
Originally Posted by jefro View Post
Make another VM of a firewall device and route to and out of it.
This probably would be even more difficult for me. Besides I would prefer not to run too many VMs

Last edited by kgQyJt5dRg; 11-02-2022 at 08:05 PM.
 
Old 11-02-2022, 09:19 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,311
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by computersavvy View Post
Your VM has its own network.
That'll be the matter. For its default, it looks like Qemu behaves like any other networked application such as a web browser, a mail client, etc. The network the guest sees is all internal to that. So maybe one option would be to launch it using a particular group and then use nftables (not UFW) to filter by group or maybe even use a cgroup.

However, Qemu can be configured to operate in a bridged mode and then from there it should be possible to filter in the host more easily by interface.
 
Old 11-03-2022, 09:31 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,660
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Indeed. If you simply want the VMs to see and use "the host network," bridging might be a simple option. Obviously, "less commonly used," but it might be perfect in this situation.

Your host's routing rules should then specify that there is no route to the outside world at all, except through the VPN gateway addresses and therefore available only when the VPN is up.

Last edited by sundialsvcs; 11-03-2022 at 09:32 AM.
 
Old 11-04-2022, 05:19 AM   #7
kgQyJt5dRg
LQ Newbie
 
Registered: Oct 2022
Posts: 5

Original Poster
Rep: Reputation: 0
But I think I already use it in a bridged mode. At least I see a virbr0 net as I mentioned before.
Anyway I have no idea how to write the correct rules to "specify that there is no route to the outside world at all, except through the VPN gateway addresses"

Or how can I check if it is in a bridged mode?

Last edited by kgQyJt5dRg; 11-04-2022 at 05:35 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] Introducing vms - more qemu virtual machines ! ninikos Slackware 17 08-30-2018 08:46 PM
[SOLVED] Does Slackware tell qemu/kvm virtual machines to shutdown? Altiris Slackware 6 08-26-2016 02:13 PM
How to run qemu Linux virtual machines properly on android tabs. rupeshforu3 Linux - Virtualization and Cloud 4 06-22-2014 10:05 PM
LXer: Set up qemu-kvm-1.0+noroms as spice enabled qemu server vs qemu-kvm-spice on Ubuntu Precise LXer Syndicated Linux News 0 05-26-2012 07:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration