lxq icon indicating copy to clipboard operation
lxq copied to clipboard

suggestion: improve network interface name

Open tannoiser opened this issue 6 years ago • 5 comments

Hi,

I made a little change in your script and I'd like to share it with you.

You assume in apps/nginx.rules that the network interface is eth0, but, nowday it often has another name. Because this is very crucial for dnat success of the configuration, I propose this changing:

diff --git a/apps/nginx/nginx.rules b/apps/nginx/nginx.rules
index 6a12be6..1e7b0e1 100644
--- a/apps/nginx/nginx.rules
+++ b/apps/nginx/nginx.rules
@@ -1,2 +1,2 @@
-iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport 80 -j DNAT --to-destination $CONTAINER_IP:80
-iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport 443 -j DNAT --to-destination $CONTAINER_IP:443
\ No newline at end of file
+iptables -t nat -I PREROUTING -i $INTERFACE -p TCP -d $PUBLIC_IP --dport 80 -j DNAT --to-destination $CONTAINER_IP:80
+iptables -t nat -I PREROUTING -i $INTERFACE -p TCP -d $PUBLIC_IP --dport 443 -j DNAT --to-destination $CONTAINER_IP:443

and

diff --git a/lxq b/lxq
index 49eb7dc..daac463
--- a/lxq
+++ b/lxq
@@ -354,8 +354,11 @@ init() {
     mkdir /etc/lxq/
     echo "LXQ_INIT=TRUE" >> /etc/lxq/lxq.cfg
     PUBLIC_IP=`curl -s ifconfig.me`
+    INTERFACE=`ip link | awk -F: '$0 !~ "lo|vir|lxd|^[^0-9]"{print $2a;getline}'`
     echo "Public IP: $PUBLIC_IP"
     echo "PUBLIC_IP=$PUBLIC_IP" >> /etc/lxq/lxq.cfg
+    echo "Network Interface: $INTERFACE"
+    echo "INTERFACE=$INTERFACE" >> /etc/lxq/lxq.cfg
     $ECHO_GREEN"SUCCESS: LXQ init Complete!"$NC
     lxq install nginx null
   fi

I used as "recipe" to get the interface name this code:

ip link | awk -F: '$0 !~ "lo|vir|lxd|^[^0-9]"{print $2a;getline}'

It's seems correct to me, but could be even better (or maybe asking it during init process?)

Hope it's useful

tannoiser avatar Feb 11 '19 13:02 tannoiser

I had thought of that in the past, but I havn't had a chance to work on it yet. This is great, thanks! Can you do a pull request?

X1Aaron avatar Feb 11 '19 15:02 X1Aaron

sure, I'll make more test (i found a little issue) and then I'll do!

tannoiser avatar Feb 11 '19 15:02 tannoiser

Ok, here I am. I had to fix the code that extract the iface name. The reason is avoid the space as first character.

I can do the pull request, do you have any preference? I'll do on your master/origin, or I have to make a branch? (I'm an old guy have patience with me ^_^ )

tannoiser avatar Feb 11 '19 16:02 tannoiser

Can you post the code you are using here?

X1Aaron avatar Feb 19 '19 16:02 X1Aaron

this is the git diff

sysadm@lxd-server:/opt/lxq$ git diff lxq
diff --git a/lxq b/lxq
old mode 100644
new mode 100755
index 49eb7dc..e95f580
--- a/lxq
+++ b/lxq
@@ -354,8 +354,11 @@ init() {
     mkdir /etc/lxq/
     echo "LXQ_INIT=TRUE" >> /etc/lxq/lxq.cfg
     PUBLIC_IP=`curl -s ifconfig.me`
+    INTERFACE=`ip link | awk -F: '$0 !~ "lo|vir|lxd|^[^0-9]"{print $2;getline}' | cut -c2-`
     echo "Public IP: $PUBLIC_IP"
     echo "PUBLIC_IP=$PUBLIC_IP" >> /etc/lxq/lxq.cfg
+    echo "Network Interface: $INTERFACE"
+    echo "INTERFACE=$INTERFACE" >> /etc/lxq/lxq.cfg
     $ECHO_GREEN"SUCCESS: LXQ init Complete!"$NC
     lxq install nginx null
   fi

tannoiser avatar Feb 21 '19 10:02 tannoiser