Altering the IP handle of a Linux system from dynamic to static shouldn’t be tough, however requires somewhat care and a set of instructions that you just probably hardly ever use. This publish offers a bash script that may run by way of the method, gather the wanted data after which challenge the instructions required to make the modifications whereas asking as little as potential from the individual operating it.
The majority of the script focusses on ensuring that the right settings are used. For instance, it collects the 36-charater universally distinctive identifier (UUID) from the system so that you just by no means need to sort it in or copy and paste it into place.
After asking solely a query or two, the script runs a collection of nmcli (Community Supervisor) instructions that may change the IP handle if requested and set the handle to be static. Discover that quite a few the fields within the instructions that it’ll run as proven under are variables derived from earlier instructions within the script.
sudo nmcli connection modify $UUID IPv4.handle $IP/$sz sudo nmcli connection modify $UUID IPv4.gateway $router sudo nmcli connection modify $UUID IPv4.methodology handbook sudo nmcli connection down $UUID sudo nmcli connection up $UUID
If a brand new IP handle is requested, the script will verify the format of the handle requested and make it possible for it’s appropriate with the present community.
Previous to operating the instructions proven above, the script collects the variables used to ensure the instructions are arrange accurately—largely by operating instructions that report on the present settings. The distinguished UUID proven in every of them is collected from the output of this command:
$ nmcli connection present NAME UUID TYPE DEVICE Wired connection 1 ec33e575-8059-3a20-b7a5-58393deb1783 ethernet enp0s25
The UUID is saved within the script by the command like that proven under in order that it may be used within the nmcli instructions proven earlier.
$ UUID=`nmcli connection present | tail -1 | awk ‘{print $4}’` $ echo $UUID ec33e575-8059-3a20-b7a5-58393deb1783
The command under pulls the subnet specification from an ip a command like that proven under. The outcome shall be one thing like “192.168.0.4/24” the place the 24 (i.e., 24 bits) tells us that the primary three bytes of the handle (192.168.0) symbolize the community.
$ subnet=`ip a | grep “inet “ | tail -1 | awk ‘{print $2}’` $ echo $subnet 192.168.0.11/24
Getting almost all of the wanted data from the system fairly than asking the consumer to offer it makes the method rather a lot simpler and, doubtlessly, extra dependable.
The script additionally offers you the choice of fixing the present IP handle should you don’t prefer it. Whereas it will depart you disconnected from the system by the point the script ends, you possibly can instantly log again in utilizing the brand new IP handle.
Word that the script makes use of sudo for the ultimate instructions as a result of these instructions will change system settings.
Right here is the script which I name “set_static_IP”:
#!/bin/bash # get subnet subnet=`ip a | grep “inet “ | tail -1 | awk ‘{print $2}’` # get router/gateway router=`ip route present | head -1 | awk ‘{print $3}’` # get measurement of community portion of handle in bytes sz=`echo $subnet | awk -F / ‘{print $2}’` bytes=`expr $sz / 8` prefix=`echo $subnet | minimize -d. -f1-$bytes` # e.g., 192.168.0 # get IP handle to be set
IP=`hostname -I | awk ‘{print $1}’` # present IP
echo -n “Hold IP handle?—$IP [yn]> “
learn ans if [ $ans =="n" ]; then echo -n “Enter new IP handle: “ learn IP # verify if specified IP is correctly formatted if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then echo Invalid IP fi # verify if specified IP works for native community if [[ ! $IP =~ ^$prefix ]]; then echo “ERROR: Specified IP not usable for native community” exit fi fi # verify if specified IP is correctly formatted if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then echo Invalid IP fi # fetch the UUID UUID=`nmcli connection present | tail -1 | awk ‘{print $4}’` # UUID=`nmcli connection present | head -2 | tail -1 | awk ‘{print $3}’` # Mint # run instructions to arrange the everlasting IP handle sudo nmcli connection modify $UUID IPv4.handle $IP/$sz sudo nmcli connection modify $UUID IPv4.gateway $router sudo nmcli connection modify $UUID IPv4.methodology handbook sudo nmcli connection up $UUID
The script was examined on each Fedora 35 and Linux Mint. Discover that the command for fetching the UUID on Mint is completely different as a result of the output of the “nmcli connection present” command is completely different. The command for Mint is included within the script however commented out for a simple change.
Interplay with the script will appear to be this:
$ ./set_static_IP Hold IP handle?—192.168.0.18 [yn]> y [sudo] password for yourid: Connection efficiently activated (D-Bus energetic path: /org/freedesktop/NetworkManager/ActiveConnection/24)
After operating the script, I ran a discover command to search for very just lately up to date recordsdata and located this one that features the “handbook” (i.e., static) setting specified within the thrid command proven above.
$ sudo cat “/and so forth/NetworkManager/system-connections/Wired connection 1.nmconnection” [connection] id=Wired connection 1 uuid=ec33e575-8059-3a20-b7a5-58393deb1783 sort=ethernet autoconnect-priority=-999 interface-name=enp0s25 permissions= timestamp=1653746936 [ethernet] mac-address-blacklist= [ipv4] address1=192.168.0.11/24,192.168.0.1 dns-search= methodology=handbook [ipv6] addr-gen-mode=stable-privacy dns-search= methodology=auto [proxy]
To search out solely very just lately created or up to date recordsdata, you should use a command like this that appears for recordsdata which were modified within the final 14.4 minutes:
$ sudo discover . -mtime -.01 -print
In case this isn’t clear, the -mtime variable when set to 1 means “1 day”. Since a day is 1440 minutes lengthy, .1 would imply 144 minutes and .01 means 14.4 minutes.
Wrap-Up
Dynamic addressing is nice besides when it isn’t. It isn’t when the system in query is one it’s important to hook up with pretty typically fairly than one you solely join from. Figuring out the way to change an IP handle from dynamic to static could be a massive deal while you want an IP handle that doesn’t change.
Copyright © 2022 IDG Communications, Inc.