Friday, September 3, 2010

Connect a device directly to your macbook with ethernet (for filesharing between computers or to configure for wireless)

This is what I did specifically for my Brother HL-2170w laserprinter in order to get it configured for wireless use. I couldn't use the automatic configured because I have a key on my network.

If you are using wireless at home, and you want to configure something like a wireless printer, you will first have to hook it up 'wired'--that is, via Ethernet. You can connect it directly to your router, but in my case the router is in the basement and not where I want my printer to be--it's easier to move my laptop.

This can also be used to connect another laptop up to your system without a router or hub between them. You can connect directly with just an ethernet cable, because the Mac (at least newer ones) have auto-sensing ports. This can be used for faster/secure file transfer, as well as giving the other laptop access to the internet by sharing your connection in the following cases
  • other laptop doesn't have a wireless
  • you don't want to share the key/password for your network
  • you want to observe the traffic of the other computer for research or forensic purposes (make sure you have consent of the parties)
  • you have a 3G, cellular network, or other network connection that the user can't connect to
  • any other reason you want to share
It was fairly straightforward for me because I have plenty of past experience with dhcp servers, routers and TCP/IP routing. If you don't, it may seem like magic. Best of luck.

The key thing is starting the dhcp server; this allows the other machine to get an IP address allocated automatically from you (and routing information, if you are doing connection sharing). The alternative is manually configuring the IP address manually, often more trouble than it's worth, and not an option for many wireless devices like printers.
  1. Open sharing settings in System Preferences
  2. Highlight the Internet Sharing selection on the left but don't click the checkbox yet; this will show the options on the right.
  3. Select the interface that has the connection you want to share (the one that you are using, e.g. wireless) from the pulldown
  4. To computers using: Ethernet
  5. Click on the 'internet sharing' check box and click Start in the ensuing dialog box.
(Here's a page with screenshots; it's from OS X 10.4, and some of the screens have changed.)

'Internet sharing' turns on a dhcp server and enables routing.
Note: that a 192.168.2.X network is used for sharing by default; you can find the rules that OSX uses in the 'man InternetSharing' entry (and you can change SharingNetworkNumberStart)--more info on this Mac OS X Hints page, be sure to read the comments.

This connection sharing can be used for more advanced scenarios as well.
Say you and a friend are on wireless, but different networks (they could be public and private, or wifi and tethered via cell phone).
You could send some traffic via one interface, and others via the other. This can be very useful to network professionals for troubleshooting.
(I usually use a squid proxy on a separate host, sometimes through an ssh tunnel, with a customized .pac proxy auto-config file; more on that later..)


Bill Wright said...

Great stuff. Thanks. I want to find the IPs that my Mac's DHCP server gives out. I tried watching the system.log file and I did see this line:

Jan 21 11:24:46 172-30-3-123 bootpd[20258]: interface en0: ip mask

But I could not ssh into that IP (and I should have been able to, if it was my device). So, is there a way to view all the IP addresses allocated by DHCP.

Todd Kaufmann said...

The .1 address is the mac itself. You probably don't have ssh turned on on the Mac itself.

Are you sure the device you plugged in is set to use dhcp? If so, you should have seen messages like this:

Jan 21 19:55:23 mymac bootpd[71]: dhcp: re-reading lease list
Jan 21 19:55:23 mymac bootpd[71]: DHCP DISCOVER [en0]: 1,0:30:1b:80:13:3a
Jan 21 19:55:23 mymac bootpd[71]: OFFER sent SN21V10 pktsize 300
Jan 21 19:55:23 mymac bootpd[71]: DHCP REQUEST [en0]: 1,0:30:1b:80:13:3a
Jan 21 19:55:23 mymac bootpd[71]: ACK sent SN21V10 pktsize 300

If not, there is likely still some traffic coming out, and so your Mac should have an arp entry for it. Try

arp -an | grep en0

you can also try (as root)

tcpdump -i en0 -n

and see what traffic is on there. If the device has a static IP, then to get to it you are going to need to add a route for that device to the en0 network, and maybe change the interface address (a bit more involved than I have time for here). It maybe be easier to log in to the other machine and change the network config there (ie, set it to dhcp).