How to set up IP addresses on command line for connecting to network without using dhclient
You can add a static IP address with the ip command, but you need to specify the network range and default router aswell:
ip addr add 192.168.1.2/24 dev wlan0
ip route add default via 192.168.1.1
Without the later step you will only have connectivity to your local network. You will also need to update your /etc/resolv.conf with the correct nameserver address:
echo 'nameserver 8.8.8.8' >/etc/resolv.conf
Pay attention because if you have some sort of network manager running or the resolvconf package installed, it may easily overwrite your changes.
BenjiWiebe
My main job is cheese making. (Try some! It's really good!) I'm now a small engine mechanic. For hobbies, I write computer programs, fix other people's computers, and program AVR microcontrollers.
Updated on September 18, 2022Comments
-
BenjiWiebe almost 2 years
What I do to connect to our wireless network:
- run
sudo ifconfig wlan0 up
- run
wpa
(a script that invokeswpa_supplicant
with all the necessary arguments) - run
sudo dhclient wlan0
(this is the step I don't like)
The last step, the
dhclient
step, I would like to replace with a static IP configuration instead of getting it from the DHCP server. I know what the gateway address is, the DNS addresses, the IP address and the netmask, but I don't know how to set these via the command line. Is it possible?If it matters, I am running Fedora 18 x86_64.
P.S. The IP address is set with
sudo ip addr add <ip-address> dev wlan0
, right?-
Frank Thomas about 11 yearshave you tried setting it in your ifcfg-wlan0 ? docs.fedoraproject.org/en-US/Fedora/15/html/Deployment_Guide/…
-
BenjiWiebe about 11 yearsDoesn't ifcfg files use NetworkManager? I am trying to get away from NetworkManager, as NetworkManager is not working correctly. That is why I am looking for a "basic" command line alternative.
-
shouso_boy about 11 yearstry this guide wikihow.com/Assign-an-IP-Address-on-a-Linux-Computer
-
Frank Thomas about 11 yearsOther way around. these files are used by networkmanager if present, but just like the interfaces file in a debian-based system, it can be configured by hand if networkmanager is not being used. on my debian systems I configure my interfaces file and then disable networkmanager
-
BenjiWiebe about 11 years@shouso_boy That is exactly what I was looking for! That
route add default via <gateway>
is what I need! Thanks!
- run
-
BenjiWiebe about 11 yearsThis works to get local network access, e.g.
ping 192.168.0.1
, but notping google.com
ornslookup google.com
. My resolv.conf does havenameserver 8.8.8.8
in it.ping 8.8.8.8
works, butping google.com
saysping: unknown host google.com
. -
Janos Pasztor about 11 yearsTry ping 8.8.8.8 if you can actually reach Google's open DNS servers. If your resolv.conf is set up correctly and you are able to reach those servers, name resolution should work (unless you have some more advanced stuff in your system like a malconfigured nsswitch.conf, etc.)
-
BenjiWiebe about 11 yearsHere is my nsswitch.conf file. (I didn't set it up; I have no idea what it does; this is the default one). EDIT: I can ping 8.8.8.8, and I can also ping Google's IP address. I still can't
ping google.com
though. It saysping: unknown host google.com
. -
Janos Pasztor about 11 yearsThe nsswitch.conf looks pretty standard. Can you check with tcpdump if your computer is actually sending out DNS packets? (port 53)