- How to setup a network between a windows computer and a linux computer?
- 1 Answer 1
- How to join a Windows network with your Linux device
- About the author
- David Adams
- Accessing network applications with WSL
- Accessing Linux networking apps from Windows (localhost)
- Accessing Windows networking apps from Linux (host IP)
- Connecting via remote IP addresses
- Accessing a WSL 2 distribution from your local area network (LAN)
- IPv6 access
How to setup a network between a windows computer and a linux computer?
I connected my Windows computer to my Linux computer with an Ethernet cable. The problem is that the Linux interface enp5s0 doesn’t acquire an IP and when I try to ping the Windows computer it says network unreachable. I have tried systemctl start dhcpcd@enp5s0.service but it fails to start with
Which should be normal, but then how do I set an IP for the Linux machine in this network of two? And how do I enable the Linux machine to ping and download files from the Windows machine?
wiki.archlinux.org/index.php/… shows you how to setup a static IP. You may want to set up Samba for file sharing.
are you sure you are either a cross-over cable between your two computers or at least one of the network interfaces have signal auto-sensing capability ?
@MelBurslan: Fortunately, many Ethernet adapters from the last decade support both types of cables so hopefully one of them supports it.
Sorry to contradict you @RuiFRibeiro, but I’ve seen 100mbps adapters that support both cables 12 years ago.
1 Answer 1
In order to acquire an IP adress on the Linux machine using the DHCP method you have to install an DHCP-Server on the Windows machine first and configure it.
I assume that is not really what you want. In order to exchange network packages between both hosts you have to assure that both hosts are on the same network. In this hardware setting the best way to do so is to manually set up static network configurations on both hosts.
Configuration on the Windows (7?) machine:
When the cable is connected via ethernet your networking notifier in the windows task bar should show a wired connection symbol (or a spinning circle notifying you it is trying to automatically get an IP via DHCP).
Click on the symbol and then open the network and sharing center. On the right hand side of the upcoming window you should see something like an underlined «LAN-connection 1». Click on it an then go to «Properties». In the upcoming list mark «Internet Protocol Version 4 (TCP/IPv4)» and then again click on «Properties». Now check «Use the following IP-Adresses» (or something like that — I only have the german version of Windows on my screen and can’t tell what the exact english translation is). Now type in the following fields:
- IP-adress: 10.0.0.1
- subnet mask: 255.255.255.0
- gateway: 10.0.0.2 (does not matter as there is no way out of your small two host network. Using the following configuration steps, your Linux host will be the gateway host)
Configuration on the Linux machine (Debian/Ubuntu):
Open up a terminal window and do the following commands (as superuser):
ifconfig enp5s0 up ifconfig enp5s0 10.0.0.2 ifconfig enp5s0 netmask 255.255.255.0
You have now set up a small network containing the two hosts with the Linux machine being the gateway host (when the Linux host has a second interface connected to an WAN and the forwarding is set up properly then it can act as a «real» internet-gateway for the windows machine).
Verify the setup using the ping command on the both hosts (the terminal can be used on both OSes to do so).
And both should consecutively show up successfully issued ping packets.
How to join a Windows network with your Linux device
The network used for this tutorial does not assign IP automatically by using DHCP, we’ll assign everything manually. In this tutorial I assume your Linux device is a Debian or Ubuntu based distribution, it may be useful for other distributions too but some commands may be different, for example when restarting services like Samba.
The first step is to remove all previous data belonging to old connections or networks by executing the command “dhclient -r”
Where “dhclient” makes mention to the dhcp client and “-r” is similar to the “release” used in Windows (ipconfig /release) .
Next we need to learn what network device will interact with the network by running “ifconfig”, similar to the Windows command “ipconfig”, in this case the wired network device is “enp2s0” as shown in the screenshot below.
Then we’ll check information on the network from a Windows workstation by running the equivalent command “ipconfig” to see the IP range and gateway address.
We can see the network device has assigned two IP addressed, in this case I know one IP address (10.100.100.141) is limited by a switch to a little portion of the network without internet access while the second (172.31.124.141) has total access. I apologize but the network belongs to a Latin American company and all workstation are in Spanish. Where “Direcciòn” means “address” and “Puerta de enlace determinada” means “gateway”.
From the Windows workstation we’ll ping IP addresses belonging to the same range in order to get an unassigned, or free IP to assign our device (remember within a network IP addresses can’t be repeated, and each device must an unique address). In this case I did ping to the IP 172.31.124.142 and it was unresponsive, therefore it was free.
The next step is to assign to our Linux device it’s own IP and enroute it through the proper gateway by executing “ifconfig enp2s0 X.X.X.X” to assign the IP address and “route add default gw X.X.X.X¨ to determine the gateway.
Remember to replace “X.X.X.X” for the proper addresses according to your Windows network information as shown in the following image.
Then we need to edit the file /etc/resolv.conf which stores the DNS addresses (Domain Name Server) to add Domain Name Servers capable to translate domain names like www.linuxhint.com to IP address. To edit the file we’ll use the text editor Nano by running “nano /etc/resolv.conf”
We’ll use Google’s Domain Name Server 8.8.8.8, you can use the same DNS for your network if you are looking for Internet access too.
After editing the file we’ll save it by pressing CTRL+X and confirm by pressing Y.
Next we’ll test our internet access by pinging an internet address like google.com
If we want more than internet access and need to interact with other devices within the same network we need to install Samba, a service which allows us to interact with Windows services.
After Samba was installed we’ll need to add the Workgroup, in this tutorial I assume you know to use Windows graphically, you can check the Workgroup by pressing on “This Computer” with a right click and then checking the Properties. In this case the Workgroup is “Modernizacion”, we’ll edit our Samba configuration file stored at /etc/samba by using Nano again:
We’ll see the same file shown below, and among the first uncommented lines the parameter “workgroup”, the one we need to edit in order to add our device to the Windows workgroup.
Again to save the file press CTRL+X and then Y when asked if you want to save it.
After making changes on samba´s configuration file we’ll need to restart the service to apply the changes by running “/etc/init.d/smbd restart”
Now we can check with our file manager new locations and printers belonging to the network.
Adding a Linux device to a Windows network is really easy, it may even be easier than in this tutorial if the network assigned IP addresses automatically with the DHCP server.
I hope this tutorial was helpful. Have a great day and keep enjoying by reading LinuxHint.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
Accessing network applications with WSL
There are a few considerations to be aware of when working with networking apps, whether you are accessing a Linux networking app from a Windows app or accessing a Windows networking app from a Linux app, you may need to identify the IP address of the virtual machine you are working with, which will be different than the IP address of your local physical machine.
Accessing Linux networking apps from Windows (localhost)
If you are building a networking app (for example an app running on a NodeJS or SQL server) in your Linux distribution, you can access it from a Windows app (like your Edge or Chrome internet browser) using localhost (just like you normally would).
Accessing Windows networking apps from Linux (host IP)
If you want to access a networking app running on Windows (for example an app running on a NodeJS or SQL server) from your Linux distribution (ie Ubuntu), then you need to use the IP address of your host machine. While this is not a common scenario, you can follow these steps to make it work.
- Obtain the IP address of your host machine by running this command from your Linux distribution: cat /etc/resolv.conf
- Copy the IP address following the term: nameserver .
- Connect to any Windows server using the copied IP address.
The picture below shows an example of this by connecting to a Node.js server running in Windows via curl.
Connecting via remote IP addresses
When using remote IP addresses to connect to your applications, they will be treated as connections from the Local Area Network (LAN). This means that you will need to make sure your application can accept LAN connections.
For example, you may need to bind your application to 0.0.0.0 instead of 127.0.0.1 . In the example of a Python app using Flask, this can be done with the command: app.run(host=’0.0.0.0′) . Please keep security in mind when making these changes as this will allow connections from your LAN.
Accessing a WSL 2 distribution from your local area network (LAN)
When using a WSL 1 distribution, if your computer was set up to be accessed by your LAN, then applications run in WSL could be accessed on your LAN as well.
This isn’t the default case in WSL 2. WSL 2 has a virtualized ethernet adapter with its own unique IP address. Currently, to enable this workflow you will need to go through the same steps as you would for a regular virtual machine. (We are looking into ways to improve this experience.)
Here’s an example Windows command to add a port proxy that listens on port 4000 on the host and connects it to port 4000 to the WSL 2 VM with IP address 192.168.101.100.
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100
IPv6 access
WSL 2 distributions currently cannot reach IPv6-only addresses. We are working on adding this feature.