Networking directly to a laptop or computer
It is possible to connect the Raspberry Pi LAN port directly to a laptop or computer using a single network cable. This will create a local network link between the computers, allowing all the things you can do if connected to a normal network without the need for a hub or router, including connection to the Internet, if Internet Connection Sharing (ICS) is used as follows:
ICS allows the Raspberry Pi to connect to the Internet through another computer. However, some additional configuration is required for the computers in order to communicate across the link, as the Raspberry Pi does not automatically allocate its own IP address.
We will use the ICS to share a connection from another network link, such as a built-in Wi-Fi on a laptop. Alternatively, we can use a direct network link (refer to the Direct network link section under the There's more… section) if the Internet is not required or if the computer has only a single network adapter.
Note
Although this setup should work for most of the computers, some setups are more difficult than the others. For additional information, see www.pihardware.com/guides/direct-network-connection.
Getting ready
You will need the Raspberry Pi with power and a standard network cable.
Note
The Raspberry Pi Model B LAN chip includes Auto-MDIX (Automatic Medium-Dependent Interface Crossover). Removing the need to use a special crossover cable (a special network cable wired so that the transmit lines connect to receive lines for direct network links), the chip will decide and change the setup as required automatically.
It may also be helpful to have a keyboard and monitor available to perform additional testing, particularly if this is the first time you have tried this.
To ensure that you can restore your network settings to their original values, you should check whether it has a fixed IP address or the network is configured automatically.
To check the network settings on Windows 10, perform these steps:
- Open Settings from the start menu, then select Network & Internet, then Ethernet, and click on Change adapter options from the list of Related Settings.
To check the network settings on Windows 7 and Vista, perform the following steps:
- Open Network and Sharing Center from the Control Panel and click on Change adapter settings on the left-hand side.
- To check the network settings on Windows XP, open Network Connections from the Control Panel.
- Find the item that relates to your wired network adapter (by default, this is usually called Ethernet or Local Area Connection, as shown in the following screenshot):
- Right-click on its icon and click on Properties. A dialog box will appear, as shown in this screenshot:
- Select the item called Internet Protocol (TCP/IP) or Internet Protocol Version 4 (TCP/IPv4) if there are two versions (the other is Version 6), and click on the Properties button.
- You can confirm that your network is set by using automatic settings or a specific IP address (if so, take note of this address and the remaining details as you may want to revert the settings at a later point).
To check the network settings on Linux, perform the following steps:
- Open up the Network Settings dialog box and select Configure Interface. Refer to the following screenshot:
- Ensure that if any settings are manually set, you take note of them so that you can restore them later if you want.
To check the network settings on Mac OS X, perform the following steps:
- Open System Preferences and click on Networks. You can then confirm whether the IP address is allocated automatically (using DHCP) or not.
- Ensure that if any settings are manually set you take note of them so you can restore them later if you want to. Refer to the following screenshot:
If you just need to access or control the Raspberry Pi without an Internet connection, refer to the Direct network link section in the There's more…section.
How to do it…
First, we need to enable ICS on our network devices. In this case, we will be sharing the Internet, which is available on Wireless Network Connection through the Ethernet connection to the Raspberry Pi.
For Windows, perform these steps:
- Return to the list of network adapters, right-click on the connection that links to the Internet (in this case, the WiFi or Wireless Network Connection device), and click on Properties.
- At the top of the window, select the second tab (in Windows XP, it is called Advanced; in Windows 7 and Windows 10, it is called Sharing), as shown in the following screenshot:
- In the Internet Connection Sharing section, check the box for Allow other network users to connect through this computer's Internet connection (if present, use the drop-down box to select the Home networking connection: option as Ethernet or Local Area Connection). Click on OK and confirm whether you previously had a fixed IP address set for Local Area Connection.
For Mac OS X, to enable the ICS, perform the following steps:
- Click on System Preferences and then click on Sharing.
- Click on Internet Sharing and select the connection from which we want to share the Internet (in this case, it will be the Wi-Fi AirPort). Then select the connection that we will connect the Raspberry Pi to (in this case, Ethernet).
For Linux to enable the ICS, perform the following steps:
- From the System menu, click on Preferences and then on Network Connections. Select the connection you want to share (in this case, Wireless) and click on Edit or Configure. In the IPv4 Settings tab, change the Method option to Shared to other computers.
The IP address of the network adapter will be the Gateway IP address to be used on the Raspberry Pi, and be assigned an IP address within the same range (matching except the last number). For instance, if the computer's wired connection now has 192.168.137.1
, the Gateway IP of the Raspberry Pi will be 192.168.137.1
and its own IP address might be set to 192.168.137.10
.
Fortunately, thanks to updates in the operating system, Raspbian will now automatically allocate a suitable IP address to join the network and set the gateway appropriately. However, unless we have a screen attached to the Raspberry Pi or scan for devices on our network, we do not know what IP address the Raspberry PI has given itself.
Fortunately (as mentioned in the Networking and connecting your Raspberry Pi to the Internet via the LAN connector recipe in the There's more… section), Apple's Bonjour software will automatically ensure hostnames on the network are correctly registered. As stated previously, if you have an OSX Mac you will have Bonjour running already. On Windows you can either install iTunes, or you can install it separately (available from https://support.apple.com/kb/DL999). By default, the hostname raspberrypi can be used.
We are now ready to test the new connection, as follows:
- Connect the network cable to the Raspberry Pi and the computer's network port, and then power up the Raspberry Pi, ensuring that you have reinserted the SD card if you previously removed it. To reboot the Raspberry Pi if you edited the file there, use
sudo reboot
to restart it. - Allow a minute or two for the Raspberry Pi to fully power up. We can now test the connection.
- From the connected laptop or computer, test the connection by pinging with the hostname of the Raspberry Pi, as shown in the following command (on Linux or OS X, add
-c 4
to limit to four messages or press Ctrl + C to exit):ping raspberrypi
Hopefully, you will find you have a working connection and receive replies from theRaspberry Pi.
If you have a keyboard and screen connected to the Raspberry Pi, you can perform the following steps:
- You can ping the computer in return (for example,
192.168.137.1
) from the Raspberry Pi terminal as follows:sudo ping 192.168.137.1 -c 4
- You can test the link to the Internet by using
ping
to connect to a well-known website as follows, assuming you do not access the Internet through a proxy server:sudo ping www.raspberrypi.org -c 4
If all goes well, you will have full Internet available through your computer to the Raspberry Pi, allowing you to browse the web as well as update and install new software.
If the connection fails, perform the following steps:
- Repeat the process, ensuring that the first three sets of numbers match with the Raspberry Pi and the network adapter IP addresses.
- You can also check that when the Raspberry Pi powers up, the correct IP address is being set using the following command:
hostname -I
- Check your firewall settings to ensure it is not blocking internal network connections.
How it works…
When we enable ICS on the primary computer, the operating system will automatically allocate a new IP address to the computer. Once connected and powered up, the Raspberry Pi will set itself to a compatible IP address and use the primary computer IP address as an Internet Gateway.
By using Apple Bonjour, we are able to use the raspberrypi
hostname to connect to the Raspberry Pi from the connected computer.
Finally, we check whether the computer can communicate over the direct network link to the Raspberry Pi, back the other way, and also through to the Internet.
There's more…
If you do not require the Internet on the Raspberry Pi, or your computer only has a single network adapter, we can still connect the computers together through a direct network link. Refer to the following diagram:
Direct network link
For a network link to work between two computers, they need to be using the same address range. The allowable address range is determined by the subnet mask (for example, 255.255.0.0
or 255.255.255.0
would mean all IP addresses should be the same except for the last two or just the last number in the IP address; otherwise, they will be filtered).
To use a direct link without enabling ICS, check the IP settings of the adapter you are going to connect to and determine whether it is automatically allocated or fixed to a specific IP address.
Most PCs connected directly to another computer will allocate an IP address in the range 169.254.X.X
(with a subnet mask of 255.255.0.0
). However, we must ensure that the network adaptor is set to Obtain an IP address automatically.
For the Raspberry Pi to be able to communicate through the direct link, it needs to have an IP address in the same address range, 169.254.X.X
. As mentioned before, the Raspberry Pi will automatically give itself a suitable IP address and connect to the network.
Therefore, assuming we have Apple Bonjour (see previously for details), we only need to know the hostname given to the Raspberry Pi (raspberrypi
).
See also
If you don't have a keyboard or screen connected to the Raspberry Pi, you can use this network link to remotely access the Raspberry Pi just as you would on a normal network (just use the new IP address you have set for the connection). Refer to the Connecting remotely to the Raspberry Pi over the network using VNC and Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding) recipes.
There is lots of additional information available on my website, https://pihw.wordpress.com/guides/direct-network-connection, including additional troubleshooting tips and several other ways to connect to your Raspberry Pi without needing a dedicated screen and keyboard.