Iptables Restricting Access By Time

How to control internet time access for each IP?

How to add time to this rule: iptables -I FORWARD -s 192.168.0.56 -j ACCEPT?

I tried this

iptables -I FORWARD -s 192.168.0.56 -m time --timestart 13:00 --timestop 14:00 -j ACCEPT

and

iptables -I FORWARD -s 192.168.0.56 --match time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 09:00 --timestop 10:00 -j ACCEPT

but it doesn't work

Maybe there is another way to do it?

Operating system: Ubuntu 18.04.4 LTS
root@router:/home/wlodek# iptables -L FORWARD
Chain FORWARD (policy DROP)
target prot opt source destination
ACCEPT all -- 192.168.0.56 anywhere TIME from 09:00:00 to 10:00:00 on Mon,Tue,Wed,Thu,Fri UTC
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED

IP: 192.168.0.56 - has a connection iptables -I FORWARD -s 192.168.0.56 -j ACCEPT

IP: 192.168.0.56 - no connection iptables -I FORWARD -s 192.168.0.56 -j ACCEPT + time

6

1 Answer

iptables is working with UTC time not your local time.

Try the following formula:

iptables -I FORWARD -s 192.168.0.56 --match time --weekdays Mon,Tue,Wed,Thu,Fri --timestart $(date -u -d @$(date "+%s" -d "09:00") +%H:%M) --timestop $(date -u -d @$(date "+%s" -d "10:00") +%H:%M) -j ACCEPT

This converts your local start and end time to UTC before handing it over to iptables.

Of course you can replace start time 09:00 and end time 10:00 by any other time.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like