网卡可以通过 ifconfig 进行临时配置,也可以将配置内容写入/etc/sysconfig/network-scripts/ifcfg-ethx进行永久配置,然后执行 ifup/ifdown 会自动加载配置,进行网卡的启动和停止。这里主要描述 ifup/ifdown 调用的过程。
(1) 网卡根据网络配置的启动,需要根据四个主要脚本
(a) 启动和停止脚本:/sbin/ifup 和 /sbin/ifdown
(b) 具体启动和停止脚本:/etc/sysconfig/network-scripts/ifup-eth 和 /etc/sysconfig/network-scripts/ifdown-eth
(c) 执行函数:/etc/sysconfig/network-scripts/network-functions
(2) 执行顺序
ifup/ifdown --->ifup-eth/ifown-eth--->network-function
(3) 启动和停止命令
ifup ethx
ifdown ethx
3.1 DHCP模式执行过程
dhcclient 是dhcp客户端程序,选择dhcp获取ip地址的需要用到。dhclient 启动的时候会先找dhclient配置文件,寻找顺序为:先找该网口配置文件 /etc/dhcp/dhclient-ethx.conf,若找不到则找/etc/dhclient-ethx.conf,若找不到则不使用配置文件启动dhclient。
(1) 查找对应网卡 dhclient 配置文件
generate_config_file_name ()
{
local ver=$1
if [ -s /etc/dhcp/dhclient$ver-${DEVICE}.conf ]; then
DHCLIENTCONF="-cf /etc/dhcp/dhclient$ver-${DEVICE}.conf";
elif [ -s /etc/dhclient$ver-${DEVICE}.conf ]; then
DHCLIENTCONF="-cf /etc/dhclient$ver-${DEVICE}.conf";
else
DHCLIENTCONF='';
fi
}
(2) 启动dhclient
if [ -n "${DYNCONFIG}" ] && [ -x /sbin/dhclient ]; then
if is_true "${PERSISTENT_DHCLIENT}"; then
ONESHOT="";
else
ONESHOT="-1";
fi;
# 查找配置文件,然后生成配置文件名
generate_config_file_name
generate_lease_file_name
# Initialize the dhclient args and obtain the hostname options if needed:
# dhclient 参数
DHCLIENTARGS="${DHCLIENTARGS} ${ONESHOT} -q ${DHCLIENTCONF} -lf ${LEASEFILE}
-pf /var/run/dhclient-${DEVICE}.pid"
set_hostname_options DHCLIENTARGS
echo
echo -n $"Determining IP information for ${DEVICE}..."
if ! is_true "${PERSISTENT_DHCLIENT}" && check_link_down ${DEVICE}; then
echo $" failed; no link present. Check cable?"
exit 1
fi
ethtool_set
# 启动dhclient
if /sbin/dhclient ${DHCLIENTARGS} ${DEVICE} ; then
echo $" done."
dhcpipv4="good"
else
echo $" failed."
if is_true "${IPV4_FAILURE_FATAL}"; then
exit 1
fi
if is_false "$IPV6INIT" || ! is_true "$DHCPV6C"; then
exit 1
fi
net_log "Unable to obtain IPv4 DHCP address ${DEVICE}." warning
fi
# end dynamic device configuration
else