Use Shell to modify dynamic IP to static IP, static IP to other static IP

Source: Internet
Author: User

(1) The idea of modifying dynamic IP to static IP


① back up the Ifcfg-eth0 file before modifying IP


② Enter the NIC configuration directory/etc/sysconfig/network-scripts/ifcfg-eth0


③ modifying Bootproto=static


④ modifying Onboot=yes


⑤ Configuring a static IP address ipaddr


⑥ Configuring DNS NETMASK


⑦ Configuring the Gateway Gatway


⑧ Restart the NIC authentication configuration, verify the configuration Results service network restart


(2) The idea of modifying static IP to other static IP

☆ First determine whether it is static IP grep "DHCP"/etc/sysconfig/network-scripts/ifcfg-eth0/


If[$? -ne 0]; Then

Sed-i ' s/^ipaddr/#IPADDR/g ' ifcfg-eth0

Read-p "Please Enter IP:" ipaddr

echo "ipaddr=" $IPADDR ">>/etc/sysconfig/network-scripts/

Fi


☆ Restart NIC Verify configuration results service Network restart


code example:

#!/bin/bash

#2017年12月17日19:34:40

#by author Daqi

#change IP Shell

Net_file= "/etc/sysconfig/network-scripts"

Net_dir= "Ifcfg-eth0"

CD $NET _file/

#change IP static for static:

grep "DHCP" $NET _file/$NET _dir

If [$?-ne 0];then

Sed-i s/^ipaddr/#IPADDR/g $NET _dir

Read-p "Please enter IP address,example 192.168.0.11 IP": ipaddr

echo "Ipaddr= $IPADDR" >> $NET _file/$NET _dir

Service Network restart

Else

#change IP dhcp for static

Sed-i s/dhcp/static/g $NET _dir

Sed-i s/onboot=no/onboot=yes/g $NET _dir

Read-p "Please enter IP address,example 192.168.0.11 IP": ipaddr

cat>> $NET _file/$NET _dir <<eof

Ipaddr= $IPADDR

netmask=255.255.255.0

gatway=192.168.2.1

Eof

Service Network restart

Fi


Use Shell to modify dynamic IP to static IP, static IP to other static IP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.