Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
1  
"...with a script" "...on OS X" –  Cory J Feb 11 '13 at 16:56
add comment

2 Answers

up vote 4 down vote accepted

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?

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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: developer.apple.com/library/mac/#documentation/Darwin/Reference/… address should be an optional argument. Maybe try "ifconfig eth0 inet netmask 255.255.255.0"? –  Cory J Feb 11 '13 at 16:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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