You have a linux machine without GUI and you want to change its IP address? I was in this situation and spent few days to find way out. Now, I want to share this with you. You just copy and create your own shell script, that's all.
Create a bash script file
From command line, just type this then press Enter
$ vi setIP.shCopy and paste below scripts and save it
#!/bin/bash #Usage: setIPHostName <ip> <netMask> <gateWay> <domainName> <domainIP> get_prop(){ propfile=$1 key=$2 grep "^${2}=" ${1}| sed "s%${2}=\(.*\)%\1%" } ip=$1 netmask=$2 gateway=$3 domainname=$4 domainip=$5 hostname=$( cat /etc/hosts | grep 127.0.0.1 | awk '{ print $2 }' ) hostnameAlias=$( cat /etc/hosts | grep 127.0.0.1 | awk '{ print $3 }' ) uuid=`get_prop /etc/sysconfig/network-scripts/ifcfg-eth0 UUID` hwaddr=`get_prop /etc/sysconfig/network-scripts/ifcfg-eth0 HWADDR` checkVar () { echo "" echo "Variables to be set" echo IP = $ip echo NETMASK = $netmask echo GATEWAY = $gateway echo DOMAINNAME = $domainname echo DOMAINIP = $domainip echo HOSTNAME = $hostname echo HOSTNAME ALIAS = $hostnameAlias echo UUID = $uuid echo HWADDR = $hwaddr echo "" echo "" } setIpHostname () { # set hosts file echo "127.0.0.1 $hostname $hostnameAlias localhost" > /etc/hosts # set resolve.conf echo "search $domainname" > /etc/resolv.conf echo "nameserver $domainip" >> /etc/resolv.conf # set network file echo "NETWORKING=yes" > /etc/sysconfig/network echo "HOSTNAME=$hostname" >> /etc/sysconfig/network echo "GATEWAY=$gateway" >> /etc/sysconfig/network # set ifcfg-eth0 echo "DEVICE=eth0" > /etc/sysconfig/network-scripts/ifcfg-eth0 echo "NAME=\"System eth0\"" > /etc/sysconfig/network-scripts/ifcfg-eth0 echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "NETMASK=$netmask" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "IPADDR=$ip" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "GATEWAY=$gateway" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "NM_CONTROLLED=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "IPV4_FAILURE_FATAL=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "IPV6INIT=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "DEFROUTE=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "DOMAIN=$domainname" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "DNS1=$domainip" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "UUID=$uuid" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "HWADDR=$hwaddr" >> /etc/sysconfig/network-scripts/ifcfg-eth0 } main () { setIpHostname # This VA so set it for every boot #cp /etc/hosts /root/ #cp /etc/resolv.conf /root/ #cp /etc/sysconfig/network /root/ #cp /etc/sysconfig/network-scripts/ifcfg-eth0 /root/ #{ #echo "## Following line is added by ISV" #echo "cp /root/hosts /etc/hosts" #echo "cp /root/resolv.conf /etc/resolv.conf" #echo "cp /root/network /etc/sysconfig/network" #echo "cp /root/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0" #} >> /etc/rc.local echo "IP is changed. Rebooting..." #sleep 2 #shutdown -r 1 shutdown -r now } #checkVar #setIpHostname mainHow to use
From command line, type this
$ ./setIP.shFor example
$ ./setIP.sh 192.168.1.241 192.168.1.254 255.255.255.0 google.com 8.8.8.8NOTE: After run this script, your computer will be restart automatically. After your machine started, check your IP to make sure it is changed by command
$ ifconfigDone!
No comments:
Post a Comment