7

The ifconfig command can change both the ip address and netmask of a network interface:

ifconfig eth0 10.10.10.10 netmask 255.255.255.0

Is it possible to change the netmask of the network interface eth0 without knowing (and changing) it's ip address?

I've tried

ifconfig eth0 netmask 255.255.255.0

but I get the error

ifconfig: ioctl (SIOCAIFADDR): Invalid argument
Randomblue
  • 1,135
  • 5
  • 15
  • 33

3 Answers3

9

Just specify the same IP address as currently configured with the new subnet mask to the ifconfig command.

ifconfig eth0 `/sbin/ifconfig eth0 | grep "inet addr" | tr -s " " | cut -f 3 -d " " | cut -f 2 -d ":"` netmask 255.255.255.0

(That makes me feel dirty...)

Edit:

Having said that, on an old-as-heck Fedora Core 2 box (the oldest box I could find on short notice) I am seeing ifconfig working fine to set the netmask w/o setting the IP. What version of net-tools are you looking at?

Evan Anderson
  • 141,071
  • 19
  • 191
  • 328
  • The point is that I will not know the IP beforehand. I'm doing these specific configurations programmatically, and having to first fetch the IP address is a hassle. – Randomblue Feb 11 '13 at 16:40
1

If you don't know what the IP is, just

ifconfig eth0

and it will be displayed. Then you can use that in your configuration command.

Cory J
  • 1,528
  • 4
  • 19
  • 28
  • The point is that I will not know the IP beforehand. I'm doing these specific configurations programmatically, and having to first fetch the IP address is a hassle. – Randomblue Feb 11 '13 at 16:41
  • @randomblue actually "ifconfig eth0 netmask 255.255.255.0" works fine for me on Debian. What OS is this? – Cory J Feb 11 '13 at 16:45
  • I'm using OS X. – Randomblue Feb 11 '13 at 16:46
  • I don't have an OS X box handy but here's the man page: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/ifconfig.8.html address should be an optional argument. Maybe try "ifconfig eth0 inet netmask 255.255.255.0"? – Cory J Feb 11 '13 at 16:53
-1

ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up its should work fine