8

Currently the Ethernet ports in the building I work in are down, but the Wi-Fi works. I have a Wi-Fi-enabled laptop (Ubuntu 14.04 LTS (Trusty Tahr)) and a non-Wi-Fi enabled workstaion (Debian 8 (Jessie)) with only an Ethernet plug.

Is it possible to connect the two via an Ethernet cable and be able to get network connectivity on the workstation?

  • Doubtful, but you can plug the Ethernet cable directly into your Router.. – ryekayo Feb 12 '16 at 18:52
  • See this question on askubuntu - askubuntu.com/questions/359856/…. This assumes at least one of the network ports is auto-sensing (or you have a crossover cable). – Andy C Feb 12 '16 at 18:55
9

Yes, you can do this, and it's not even that hard. I have a laptop with a wireless card, and an ethernet port. I plugged a RapberryPi running Arch Linux into it, via a "crossover" ethernet cable. That's one special thing you might need - not all ethernet cards can do a machine-to-machine direct connection.

The other tricky part is IP addressing. It's best to illustrate this. Here's my little set-up script. Again, enp9s0 is the laptop's ethernet port, and wlp12s0 is the laptop's wireless device.

#!/bin/bash
/usr/bin/ip link set dev enp9s0 up
/usr/bin/ip addr add 172.16.1.1/24 dev enp9s0
sleep 10

modprobe iptable_nat
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE
iptables -A FORWARD -o enp9s0 -i wlp12s0 -s 172.16.1.0/24 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

dhcpd -cf /etc/dhcpd.enp9s0.conf enp9s0

The script sets a static IP address for the ethernet card, 172.16.1.1, then sets up NAT by loading a kernel module. It turns on IP routing (on the laptop), then does some iptables semi-magic to get packets routed from the wireless card out the ethernet, and vice versa.

I have dhcpd running on the ethernet port to give out IP addresses because that's what the Raspberry Pi wants, but you could do a static address on your workstation, along with static routing, DNS server, and NTP server.

The file /etc/dhcpd.enp9s0.conf looks like this, just in case you go down that route:

option domain-name "subnet";
option domain-name-servers 10.0.0.3;
option routers 172.16.1.1;
option ntp-servers 10.0.0.3;
default-lease-time 14440;
ddns-update-style none;
deny bootp;
shared-network intranet {
        subnet 172.16.1.0 netmask 255.255.255.0 {
                option subnet-mask 255.255.255.0;
                pool { range 172.16.1.50 172.16.1.200; }
        }
}

The IP address choice is pretty critical. I used 172.16.1.0/24 for the ethernet cable coming out of the laptop. The wireless card on the laptop ends up with a 192.161.1.0/24 . You need to look at what IP address the laptop's wireless has, and choose some other subnet for the ethernet card. Further, you need to choose one of the "bogon" or "non-routable" networks. In my example, 172.16.1.0/24 is from the official non-routable ranges of IP addresses, as is 192.168.1.0/24, and so is the 10.0.0.3 address dhcpd.enp9s0.conf gives out for a DNS server and NTP server. You'll have to use your head to figure out what's appropriate for your setup.

  • PS: it's easier to beg forgiveness than it is to get permission. Just do it. – Bruce Ediger Feb 12 '16 at 20:16
  • This is a great answer. I will get working on it. Thank you! – mbiokyle Feb 12 '16 at 20:18
0

Is it possible? Absolutely. All you need to do is configure IP forwarding, or routing, or some other (simple) way of moving packets from the ethernet port on the laptop to the WiFi on the laptop and vice-versa. Is it a good idea? Probably not - your network admins might get a tad upset with you.

0

Per wikipedia if one of ethernet ports is gigabit, ordinary cable will work, no need for cross, I have gigabit on both sides and ordinary cable works.

The question is asked in 2016, so I assume network manager is running on both Linux machines, you can use it's GUI to set ip-s, if not you probably made custom set up and know how to set up ip from command prompt, so I omit those details.

After you connect with cable, you will get network interfaces names in GUI relating to ethernet on laptop and workstation.

Assuming you don't care about connecting workstation (and/or laptop by ethernet) periodically to other network equipment you can set up static IP on both for ethernet interfaces.

Set your static IP's not same subnet as for wi-fi, e.g. if wi-fi is 192.168.1.0/16, set to 192.168.2.1 and 192.168.2.2, subnet mask is 255.255.255.0 as only last 'number' of IP differs, DNS on workstation should be set to same DNS IP as for wi-fi on laptop to be able to use DNS (open sites by name, not only IP) on workstation.

You need to enable IP forwarding on laptop:

sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'

As very probably you network manager on laptop considers Ethernet of better quality than wi-fi it will try to send everything to it by default and you will loose internet after setting up Ethernet. To get back to using wi-fi, on laptop you need in the [ipv4] section of file relating to ethernet connection (name you see in network manager GUI for ethernet) in location /etc/NetworkManager/ add line

never-default=true

(if your GUI have something like "Use only for resources on this connection" checkbox for ethernet - on mine it's in ip4 section of connection properties, you can use that instead).

And finally, assuming you IP tables rules remained by default ACCEPT everything, your packets from workstation will go to internet, but will not find way back traveling only to laptop until you set up NAT rule on laptop by one command (where eth0 should be name of your wi-fi connection, you can find out the name by sudo ifconfig):

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

If not working after that try restarting network manager:

sudo service network-manager restart

You should be all set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.