How to set up IP addresses on command line for connecting to network without using dhclient

6,365

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.

Share:
6,365
BenjiWiebe
Author by

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, 2022

Comments

  • BenjiWiebe
    BenjiWiebe almost 2 years

    What I do to connect to our wireless network:

    • run sudo ifconfig wlan0 up
    • run wpa (a script that invokes wpa_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
      Frank Thomas about 11 years
      have you tried setting it in your ifcfg-wlan0 ? docs.fedoraproject.org/en-US/Fedora/15/html/Deployment_Guide‌​/…
    • BenjiWiebe
      BenjiWiebe about 11 years
      Doesn'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
      shouso_boy about 11 years
    • Frank Thomas
      Frank Thomas about 11 years
      Other 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
      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!
  • BenjiWiebe
    BenjiWiebe about 11 years
    This works to get local network access, e.g. ping 192.168.0.1, but not ping google.com or nslookup google.com. My resolv.conf does have nameserver 8.8.8.8 in it. ping 8.8.8.8 works, but ping google.com says ping: unknown host google.com.
  • Janos Pasztor
    Janos Pasztor about 11 years
    Try 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
    BenjiWiebe about 11 years
    Here 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 says ping: unknown host google.com.
  • Janos Pasztor
    Janos Pasztor about 11 years
    The nsswitch.conf looks pretty standard. Can you check with tcpdump if your computer is actually sending out DNS packets? (port 53)