Networking directly to a laptop or computer
It is possible to connect 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 a router, including connection to the internet, if Internet Connection Sharing (ICS) is used, as follows:
Note
Make use of Raspberry Pi, with just a network cable, a standard imaged SD card, and power.
ICS allows Raspberry Pi to connect to the internet through another computer. However, some additional configuration is required for the computers to communicate across the link, as 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 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 Raspberry Pi with power and a standard network cable.
Note
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 selectNetwork and Internet
, thenEthernet,
and click onChange adapter options
from the list ofRelated Settings
.
To check the network settings on Windows 7 and Vista, perform the following steps:
- Open
Network and Sharing Center
from theControl Panel
and click onChange adapter settings
on the left-hand side. - To check the network settings on Windows XP, open
Network Connections
from theControl Panel
. - Find the item that relates to your wired network adapter (by default, this is usually called
Ethernet
orLocal Area Connection
, as shown in the following screenshot):
Locating your wired network connection
- Right-click on its icon and click on
Properties
. A dialog box will appear, as shown in this screenshot:
Selecting the TCP/IP properties and checking the settings
- Select the item called
Internet Protocol (TCP/IP)
orInternet Protocol Version 4 (TCP/IPv4)
if there are two versions (the other is Version 6), and click on theProperties
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 the
Network Settings
dialog box and selectConfigure Interface
. Refer to the following screenshot:
Linux Network Settings dialog box
- If any settings are manually set, ensure you take note of them so that you can restore them later if you want.
To check the network settings on macOS X, perform the following steps:
- Open
System Preferences
and click onNetworks
. 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:
OS X Network Settings dialog box
If you just need to access or control 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 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
orWireless Network Connection
device), and click onProperties
:
Locating your wired network connection
- 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 calledSharing
), as shown in the following screenshot:
Selecting the TCP/IP properties and noting the allocated IP address
- In the
Internet Connection Sharing
section, check the box forAllow other network users to connect through this computer's Internet connection
(if present, use the drop-down box to select theHome networking connection:
option asEthernet
orLocal Area Connection
). Click onOK
and confirm whether you previously had a fixed IP address set forLocal Area Connection
.
For macOS X, to enable the ICS, perform the following steps:
- Click on
System Preferences
and then click onSharing
. - Click on
Internet Sharing
and select the connection from which we want to share the internet (in this case, it will be the Wi-FiAirPort
). Then select the connection that we will connect Raspberry Pi to (in this case,Ethernet
).
For Linux to enable the ICS, perform the following steps:
- From the
System
menu, click onPreferences
and then onNetwork Connections
. Select the connection you want to share (in this case,Wireless
) and click onEdit
orConfigure
. In theIPv4 Settings
tab, change theMethod
option toShared to other computers
.
The IP address of the network adapter will be the Gateway IP address to be used on Raspberry Pi, and will be assigned an IP address within the same range (it will all match, except the last number). For instance, if the computer's wired connection now has 192.168.137.1
, the Gateway IP of 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 Raspberry Pi or scan for devices on our network, we do not know what IP address 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 a Mac OS X, 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 Raspberry Pi and the computer's network port, and then power up Raspberry Pi, ensuring that you have re-inserted the SD card if you previously removed it. To reboot Raspberry Pi, if you edited the file there, use
sudo reboot
to restart it. - Allow a minute or two for 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 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 the Raspberry Pi.
If you have a keyboard and a screen connected to Raspberry Pi, you can perform the following steps:
- You can ping the computer in return (for example,
192.168.137.1
) from 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 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 Raspberry Pi and the network adapter IP addresses.
- You can also check that when Raspberry Pi powers up, the correct IP address is being set using the following command:
hostname -I
- Check your firewall settings to ensure your firewall 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, 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 raspberrypi
hostname to connect to Raspberry Pi from the connected computer.
Finally, we check whether the computer can communicate over the direct network link to Raspberry Pi, back the other way, and also through to the internet.
There's more...
If you do not require the internet on Raspberry Pi, or your computer has only a single network adapter, you can still connect the computers together through a direct network link. Refer to the following diagram:
Connecting and using Raspberry Pi with just a network cable, a standard imaged SD card, and power
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 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, Raspberry Pi will automatically give itself a suitable IP address and connect to the network.
Therefore, assuming we have Apple Bonjour
(mentioned previously), we only need to know the hostname given to Raspberry Pi (raspberrypi
).
See also
If you don't have a keyboard or screen connected to Raspberry Pi, you can use this network link to remotely access 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 Raspberry Pi over the network using VNC and Connecting remotely to 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.