参照网上设置linux 静态ip的教程,为了练习相应shell脚本,本人特为之编写了一个自动化设置静态ip的脚本,相应设置信息均以shell命令完成,如需改动也可以再在脚本里改动相应内容,具体shell代码如下:
- #! /bin/sh -
- # the shell used for redhat linux or redhat-based linux os
- cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
- DEVICE=eth0
- BOOTPROTO=static
- ONBOOT=yes
- IPADDR=192.168.94.133
- NETMASK=255.255.255.0
- GATEWAY=192.168.94.1
- EOF
- if [ $? -ne 0 ]
- then
- echo "Error!May don't have the privileges"
- exit 1
- fi
- /sbin/ifdown eth0
- /sbin/ifup eth0
- if [ $? -ne 0 ]
- then
- echo "Error!May cannot find eth0 or canot drive!"
- exit 2
- fi
- #强制设置权限取消
- chattr -i /etc/resolv.conf
- cat > /etc/resolv.conf << EOF
- domain localdomain
- search localdomain
- nameserver 192.168.94.1
- nameserver 202.96.134.133
- EOF
- chattr +i /etc/resolv.conf
- /etc/init.d/network restart