1

I am attempting to use my pi to share an internet connection on wifi with the ethernet port. I am able to get it to work by running these commands in terminal:

sudo service dhcpcd restart
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
sudo iptables -A FORWARD -i wlan1 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wlan1 -j ACCEPT
sudo service dnsmasq start

That part I have figured out. What I have not figured out is how to run these commands on restart of the pi. I have tried putting them in the rc.local file as some online suggestions but does not work on boot up. These commands do run on starting a new terminal server session.

I have tried the ideas on this page, https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/ and https://raspberrytips.com/autostart-a-program-on-boot/ but have not been successful. Some suggestion refer to running a program or a script file or a service. I do not know how to take these commands and turn them into a script file, program or service.

I know the code does what I want it to. You help to get it running on boot up would be a big help. thanks

1
1

There are several ways to accomplish this. One way is to put "it" in rc.local, but that's hardly ever the best way to do it. Using systemd is the new way, and perhaps the best way to do it, but not the simplest way. Using cron is a third way, and that is what we shall try here.

Be forewarned: I usually try these things on my Pi before I put them in an answer, but I'm not going to do that in this case. Consequently, there may be a bit of "trial-and-error" to get this working. That said, here we go:

As always, you should be armed with some background before you begin copying and pasting, so please read, or briefly peruse, the following system manuals:

$ man cron       # the daemon/service that makes all of this possible
$ man crontab    # your instructions to cron
$ man 5 crontab  # details on how to write crontab

If you've not used system manuals before, you type the letter q to exit a man page & return to the CLI.

The version of cron used in Raspbian provides a feature that allows us to start a program when the system boots. Here's how to use that:

$ crontab -e      # "e" = edit; if prompted to select an editor - choose nano

You are now in the nano editor. At the bottom of the file, add the following:

@reboot /bin/echo "System rebooted at: $(date)" >> /home/pi/mydatalog.txt 2>&1

In nano, save the file, exit nano & reboot your system. Afterwards, login again & find the file mydatalog.txt in your (user pi I've assumed) home directory. Open it in nano & verify the time you re-booted. If all this worked, you've just created a cron job that logs each system reboot to a file. If you're happy with this, leave it; if not use crontab -e to change it, or delete the line entirely if you like.

In your question, you ask about running commands that require root privileges to run. In this case, I feel it's best to use root's crontab instead of your own, but for Raspbian, this may not be necessary. Since this is my answer, we'll do it this way:

$ sudo crontab -e      # opens root's crontab for editing

Note that root's crontab is a different crontab as you will not see the line you added in the steps above. Enter the following in nano:

@reboot /bin/sleep 30; /usr/sbin/service dhcpcd restart && /bin/sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" && /usr/sbin/iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE && /usr/sbin/iptables -A FORWARD -i wlan1 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT && /usr/sbin/iptables -A FORWARD -i eth0 -o wlan1 -j ACCEPT && /usr/sbin/service dnsmasq start >> /home/pi/netsharelog.txt 2>&1 

Save this file in nano & exit. Now reboot your system. Let us know if you have any issues or questions.

1
  • That worked perfectly. Thank you! – Andrew May 20 '20 at 14:49
1

As you already noted, running commands at bootup is done with a service on a modern operating system using systemd. Together with its built-in networking systemd-networkd it is very easy to setup routing with network address translation (NAT), as you want.

How to setup this you can look at How to make Raspberry Pi 4 as “WIFI” -> “Ethernet” adaptor. Just replace wlan0 everywhere given in the example with wlan1 for your interface.

4
  • thank you for the feedback. It appears these are directions for making the pi wifi access point which is not what I am trying to do. I am using the internet connection that is on wlan1 and sharing it to the ethernet port so I can connect via ethernet cable to a switch and any computer connected to that switch can get internet access via the pi's wifi connection. If you can point me to how to take these commands and turn them into a service and launch it at boot that would be helpful. – Andrew May 19 '20 at 14:28
  • @Andrew Ah, that way around, misunderstood it. No problem. I have changed the answer to link to the correct tutorial. – Ingo May 19 '20 at 17:38
  • Link to a link to a link :) I'll vote for this one, too! – Seamus May 21 '20 at 3:06
  • @Seamus Thanks; always the same questions, same questions, same questions :-) I try to reduce redundant information to have an easy update on important settings. I would prefer to close the question as duplicate, but there is nearly no chance to get 5 votes in short time. – Ingo May 21 '20 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.