I know this is a pretty broad title but I didn't know what to start with.

Anyway, straight to the point. I have a little VPS I run apache and a Minecraft server on. I don't ever turn it off, but should I restart it for some reason, IPTables blocks most of my ports, including port 80. I've tried so many different suggestions on fixing this, but with no luck. Also, since the provider is OVH, the support is... lacking.

So, I've created a workaround, which I'm happy with. I created a simple shell script file to open certain ports I need opened on restart (80 and 25565 for now). The important ones such as 21 and 22 are not affected on restart.

The script looks like this:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p udp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 25565 -j ACCEPT
iptables -I INPUT -p udp --dport 25565 -j ACCEPT
/sbin/service iptables save

When I manually run it by typing /iptdef.sh, it runs fine, the ports become open and it's all good.

Of course, it's not practical having to remember to run it every time I restart the server, so I added a crontab. The problem is, it doesn't work/run. This is my crontab file:

*/5 * * * * /backup2.sh
*/55 * * * * /backup3.sh
@reboot /iptdef.sh
* * * * *  /iptdef.sh

The first two lines work. They are just simple scripts that make a backup of a folder for me. The second two lines are what's not working.

Is there a chance that perhaps it's not possible to run iptables commands from a cron? It sounds silly, but I can't see any other reason for it not to work. The scripts have the correct permissions.

  • Are the scripts being run as root (basically, are they in root's crontab)? – saiarcot895 Jul 21 '15 at 1:50
  • Yes, it is. I even tried giving the script public execution permissions, just in case, but nope, still the same old nothingness. – Byonex Jul 21 '15 at 1:55
  • WAIT A SECOND I THINK I FOUND THE PROBLEM – Byonex Jul 21 '15 at 1:58
  • Or not - I just realised I missed off the all important #!/bin/bash line at the top of the iptdef.sh file, so I added it, yet still no luck. Give up time soon. – Byonex Jul 21 '15 at 2:03
up vote 6 down vote accepted

It's because cron forcibly sets PATH to /usr/bin:/bin. You need to invoke iptables as /sbin/iptables or add PATH=/usr/sbin:/sbin:/usr/bin:/bin in your script or crontab. See crontab(5) for details.

  • 1
    I added PATH=/usr/sbin:/sbin:/usr/bin:/bin to top of the script file and it works perfectly! You, sir, are a gentleman and a scholar and a breeder of fine horses! – Byonex Jul 21 '15 at 2:32

This looks like it is covered well here: crontab's @reboot only works for root? basically the answer depends heavily on what you use for Cron and your Linux distribution.

  • The issue isn't just that. As you can see, it's also supposed to run every minute too (although it's also running as root anyway) – Byonex Jul 21 '15 at 2:02

I had to add this to /etc/hosts so that mail to anyone at my server's own FQDN would be resolved:

127.0.0.1 mydomain.com.

Notice the dot at the end. In my case, I'm sending email essentially to root@localhost with MAILTO=root which gets translated to root@mydomain.com.

Here's some more detail on this answer.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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