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 047.75.162.122 -j DROP
``````

But the IP address would be banned as 39.75.162.122!

Why do you think this is happening?

This is what is happening:

``````\$ printf "%d\n" 047
39
``````

`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.

• 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". Commented Mar 10, 2017 at 19:40
• @DigitalTrauma+ or just using `inet_addr aka inet_aton` which requires the effect of `strtol(,,0)` Commented Mar 10, 2017 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)." Commented Mar 11, 2017 at 17:12

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

``````octal:
020.0.1.22     ->  16.0.1.22
0x10.0.1.22    ->  16.0.1.22
combination:
020.0.1.0x16   ->  16.0.1.22
bottom two bytes together (old Class B)
16.0.278       ->  16.0.1.22
bottom three bytes together (old Class A)
16.278         ->  16.0.1.22
all in one, hex
0x10000116     ->  16.0.1.22
all in one, decimal (completely unreadable)
268435734      ->  16.0.1.22
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.