0

I've been looking into LXC containers and I was wondering as to whether or not it is possible to use an LXC container like an ordinary VPS?

What I mean is;

  1. How do I assign an external IP address to an LXC container?
  2. How do I ssh into an LXC container directly?

I'm quite new to LXC containers so please let me know if there are any other differences I should be aware of.

Francis
  • 23
  • 1

2 Answers2

0
lxc-create -t download -n cn_name
lxc-start -n cn_name -d
lxc-attach -n cn_name

then in container cn_name install openssh server so you can use ssh then reboot it or restart ssh service.

To make any container "services" available to the world configure port forwarding from the host to the container.

For instance if you had a web server in a container, to forward port 80 from the host ip 192.168.1.1 to a container with ip 10.0.3.1 you can use the iptables rule below.

iptables -t nat -I PREROUTING -i eth0 -p TCP -d 191.168.1.1/32 --dport 80 -j DNAT --to-destination 10.0.3.1:80

now the web server on port 80 of the container will be available via port 80 of the host OS.

bmullan
  • 364
  • 2
  • 6
0

It sounds like what you want is to bridge the host NIC to the container. In that case, the first thing you need to do is create a bridge. Do this by first ensuring bridge-utils is installed on the system, then open /etc/networking/interfaces for editing and change this:

auto eth0
iface eth0 inet dhcp

to this:

auto br0
iface br0 inet dhcp
    bridge-interfaces eth0
    bridge-ports eth0
    up ifconfig eth0 up

iface eth0 inet manual

If your NIC is not named eth0, you should replace eth0 with whatever your NIC is named (mine is named enp5s0). Once you've made the change, you can start the bridge by issuing the command

sudo ifup br0

Assuming all went well, you should maintain internet access and even your ssh session should stay online during the process. I recommend you have physical access to the host because messing up the above steps could block the host from internet access. You can verify your setup is correct by running ifconfig and checking that br0 has an assigned IP address while eth0 does not.

Once that's all set up, open up /etc/lxc/default.conf and change

lxc.network.link = lxcbr0

to

lxc.network.link = br0

And that's it. Any containers that you launch will automatically bridge to eth0 and will effectively exist on the same LAN as the host. At this point, you can install ssh if it's not already and ssh into the container using its newly assigned IP address.

"Converting eth0 to br0 and getting all your LXC or LXD onto your LAN"

lpreams
  • 151
  • 6