hi,
please tell me how to change ip address randomly in ubuntu through terminal.
thanx in advance

Dani AI

Generated

If by "randomly" you mean "get a different address without editing files," first decide which IP you care about. Your LAN IP comes from your router’s DHCP server; your public IP comes from your ISP (or a VPN/Tor). Picking an arbitrary address usually breaks connectivity because it must be in-range and conflict-free, as hinted. Also, Ubuntu moved away from ifconfig/route years ago; today you’ll usually use ip and nmcli. ’s note about NetworkManager files was spot-on for that era, but you can do this safely from the terminal now.

To shuffle your LAN IP on Ubuntu with NetworkManager, release/renew DHCP. Changing your MAC first increases the chance of getting a new lease. Replace <conn> with the connection name from nmcli connection show and wlp2s0 with your Wi‑Fi interface from ip link:

# one-time: enable randomized MAC for this connection
sudo nmcli connection modify "<conn>" 802-11-wireless.cloned-mac-address random ipv4.method auto

# force a reconnect and new DHCP lease
sudo nmcli connection down "<conn>" && sudo nmcli connection up "<conn>"

# or explicitly release/renew on the interface
sudo dhclient -r wlp2s0 && sudo dhclient wlp2s0

Want it to change periodically? Run a simple loop while you test (on a network you control; some venues forbid MAC spoofing):

while true; do
  sudo nmcli connection up "<conn>" && sleep $((RANDOM%600+60))
  sudo nmcli connection down "<conn>"
done

If your goal is a different public IP, DHCP on your laptop will not help. Home ISPs often hand out sticky addresses, so you typically need a VPN provider (rotate endpoints) or Tor (new circuits) to change your outward IP on demand. Be mindful of local policies and laws; randomizing addresses on networks you do not administer can get you blocked or worse.

Recommended Answers

All 3 Replies

What do you mean by "change IP randomnly"? You normally wouldn't do this since you need an IP in a certain Range with proper MAsk and Gateway for proper communications.

Do assign an IP via command line, you would use (for example):

ifconfig eth0 192.168.1.2 netmask 255.255.255.0
route add default gw 192.168.1.1
ifconfig eth0 down
ifconfig eth0 up

On Red Hat systems it is /etc/sysconfig/network-scripts/ifcfg-Auto_ethN (where N is the NIC you want to set - usually eth0). You would edit that file to change the static IP address (IPADDR=IPV4address, such as 192.168.1.100 - you can't do this with DHCP) and then run the "service network restart". Bingo! Your address has now been changed.

For Ubuntu, you would go to /etc/NetworkManager/system-connections. Each NIC (hardware or WiFi) will appear as the file named "* connection N" where the leading asterisk is "wired" for a hardwire connection, and the trailing N is a number for the connection #, such as 0 for eth0, 1 for eth1, etc). I'm not sure what a WiFi would appear as since I am running this in a virtual machine without WiFi... :-)

In the appropriate file you will find the section [ipv4] and an entry "addresses1=YourIPV4Address;YourGatewayAddress". Example (from my VM):

[ipv4]
method=manual
dns=68.94.156.1;
addresses1=192.168.1.50;24;192.168.1.254;
may-fail=false

So, change the IPV4 address (leave the gateway alone), and then, as mentioned above, restart the network services, except that for Ubuntu you would use "service restart network-manager" either as root, or with sudo. Oops. I mean "service network-manager restart"... :-) Danged keyboard keeps mistyping for me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.