Search code examples
linuxcontainersvpslxc

LXC Container as VPS


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.


Solution

  • 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.