One of the best thing about Mac laptops is the ability to share your internet connection with nearby devices. After completing this tutorial, your Raspberry Pi will have internet access and be configured for straightforward remote access via TightVNC and a static IP address. This proves to be invaluable when taking your Raspberry Pi for demo's on the road.
Please note that since the Raspberry Pi 3 now has a WiFi adapter built-in, this article is mostly intended for those of you running a Raspberry Pi 2 or below.
- Macbook Air/Pro running OSX
- Raspberry Pi
- Thunderbolt to Ethernet adapter**
** Only required if you're Mac does not have an onboard ethernet port
Since any Mac laptop manufactured in the last 2 years no longer has a onboard ethernet port, I will assume you will be using the thunderbolt adapter. That being said, simply plug in your adapter and then open up System Preferences > Network. In the left pane you will notice a service named "Thunderbolt Ethernet". Select it and mimic the configuration settings found below. Feel free to rename this service to whatever you'd like via the settings button near the bottom of the pane.
- Configure IPV4: Manually
- IP Address: 192.168.2.1
- Subnet Mask: 255.255.255.0
Since we have configured the connection to be static, we will need manually setup the DNS server. Without doing so our Pi will not be able to resolve any domain names. Click the Advanced button and then navigate to the DNS tab. Click the + button and add any DNS server's you'd like. The ones I have entered below, 184.108.40.206 and 220.127.116.11, are owned by Google and are publicly available. Please be aware that you may want to enter your home routers DNS as well as any others pertinent servers such as your work. Click OK when you have entered all of your DNS servers.
Now that we have configured our Ethernet port, let's actually enable the sharing of our Mac's internet connection. Head on over to System Preferences > Sharing and check the Internet Sharing checkbox. Finally, select Wi-Fi from the "Share your connection from" dropdown and then select Thunderbolt Ethernet from the "To computers using" dropdown. Whoa, that was easy!
Raspberry Pi Configuration
Let us now configure our Pi to always use the static IP provided by our Mac. It should be noted that the Pi should have internet access regardless of completing the following steps. However, I think it is well worth your time to complete the static IP setup. That way SSH'ing or RDC'ing into the Pi will always be headache free.
First plug the ethernet cord into the Pi and power the device on. Once the Pi is started either SSH into it or if you have a monitor and keyboard/mouse setup, open the command line. Enter the following command:
sudo vim.tiny /etc/resolv.conf
You have now opened the .conf file in the Vim editor. Here is a cheatsheet if you are not familiar with Vim commands. Add the following line into the file which will instruct the Pi to use the Mac for DNS lookups:
Note: If there are any other lines in the file either comment them out by adding a
# to the beginning of the line or just remove them entirely.
Save and exit this file. Next we will edit the ethernet configuration file. Again from the command line, enter the following command:
sudo vim.tiny /etc/network/interfaces
This file will not be empty and I would recommend commenting out all of the existing entries in case something goes awry. Now add the following lines to file to force the Pi to permanently use the static IP address:
auto eth0 iface eth0 inet static address 192.168.2.2 netmask 255.255.255.0 gateway 192.168.2.1
That's it, all the hard work is now (hopefully) done! Restart the Pi and once it is rebooted login and we'll do some quick confirmation of the changes. After the reboot has completed, either again SSH into the box or open the Terminal. Let's first check for internet connectivity by running this command:
You should see something similar to below as the output.
Next lets ensure we indeed are using the static IP address by running this command:
ip addr show eth0
Again, you should see something similar to below. The only important line is bolded below.
eth0: mtu 1500 qdisc mq state UP qlen 1000 link/ether b8:ac:6f:65:31:e5 brd ff:ff:ff:ff:ff:ff inet **192.168.2.2**/24 brd 192.168.2.255 scope global eth0 inet6 fe80::baac:6fff:fe65:31e5/64 scope link valid_lft forever preferred_lft forever
Remote Desktop Setup
Now that our RasPi will always have the same IP address (when connected to our Mac), let's quickly setup for remote desktop access via our same Mac. Again, this step is optional but who in their right mind wants to lug around a dedicated HDMI monitor and a USB keyboard/mouse pair everywhere they go with their RasPi? I know I don't!
To accomplish this we will be installing the xrdp package on our RasPi. But before we can install that, we must first install the tightvncserver package. Run the following command:
sudo apt install -y tightvncserver
After the install has finished, run this command:
sudo apt install -y xrdp
Once that is installed successfully, we are finally ready to remote into our RasPi! If you haven't already installed Microsoft Remote Desktop, do so now from the Mac App Store. Once this is installed run the program and enter the 192.168.2.2 into the connection field and press enter. Accept the warning message if one appears. You should then be presented with the xrdp login screen similar to below.
Unless you have modified the admin credentials of your RasPi, enter the following:
- Username: pi
- Password: raspberry
You should now be remoted into your RasPi via a never changing static IP address. Hopefully you learned something in this tutorial!