I have been trying to ban an IP address in iptables that starts with 047, but it would change it to 039.

iptables -v -w -I INPUT 1 -s -j DROP

But the IP address would be banned as!

Why do you think this is happening?


This is what is happening:

$ printf "%d\n" 047

047 in octal is 39 in decimal.

You just need to drop the leading 0.

At a guess, this is happening because something in iptables is splitting IPv4 addresses into 4 decimal numbers so it can convert the IP string representation to a long. But that's conjecture.

  • 5
    This behaviour ultimately comes from the underlying stdlib strtol() function: "An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 to 7 only". Mar 10 '17 at 19:40
  • 1
    @DigitalTrauma+ or just using inet_addr aka inet_aton which requires the effect of strtol(,,0) Mar 10 '17 at 21:22
  • It's POSIX conformant: "All numbers supplied as parts in IPv4 dotted decimal notation may be decimal, octal, or hexadecimal, as specified in the ISO C standard (that is, a leading 0x or 0X implies hexadecimal; otherwise, a leading '0' implies octal; otherwise, the number is interpreted as decimal)."
    – hobbs
    Mar 11 '17 at 17:12

inet_aton also accepts a couple of other less usual forms (the manual actually even describes them):

octal:     ->
0x10.0.1.22    ->
combination:   ->
bottom two bytes together (old Class B)
16.0.278       ->
bottom three bytes together (old Class A)
16.278         ->
all in one, hex
0x10000116     ->
all in one, decimal (completely unreadable)
268435734      ->
this should be simple
0020.0426      ->  ...

They're likely to work on web browsers too.

Prefixing octal numbers with a zero, and hexadecimal numbers with 0x is at least as old as the C language.

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.