Network Interface name change

本文介绍如何在 CentOS 7 中更改网络接口名称的方法,包括编辑 GRUB 启动参数和创建 udev 规则文件来确保重启后网络接口名称保持一致。

参考:http://ask.xmodulo.com/change-network-interface-name-centos7.html
http://blog.chinaunix.net/uid-29179844-id-4417065.html

restart network: systemctl restart network

1
editing /etc/default/grub and adding “net.ifnames=0” to GRUB_CMDLINE_LINUX variable.
run:grub2-mkconfig -o /boot/grub/grub.cfg

2
edit (or create) a udev network naming rule file (/etc/udev/rules.d/70-persistent-net.rules)
such as:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:4e:40:9a", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
public String setupInterfaceForClientInScanMode( 1666 @NonNull InterfaceCallback interfaceCallback, @NonNull WorkSource requestorWs, 1667 @NonNull ConcreteClientModeManager concreteClientModeManager) { 1668 synchronized (mLock) { 1669 if (!startHal()) { 1670 Log.e(TAG, "Failed to start Hal"); 1671 mWifiMetrics.incrementNumSetupClientInterfaceFailureDueToHal(); 1672 return null; 1673 } 1674 Iface iface = mIfaceMgr.allocateIface(Iface.IFACE_TYPE_STA_FOR_SCAN); 1675 if (iface == null) { 1676 Log.e(TAG, "Failed to allocate new STA iface"); 1677 return null; 1678 } 1679 iface.externalListener = interfaceCallback; 1680 iface.name = createStaIface(iface, requestorWs, concreteClientModeManager); 1681 if (TextUtils.isEmpty(iface.name)) { 1682 Log.e(TAG, "Failed to create iface in vendor HAL"); 1683 mIfaceMgr.removeIface(iface.id); 1684 mWifiMetrics.incrementNumSetupClientInterfaceFailureDueToHal(); 1685 return null; 1686 } 1687 if (!mWifiCondManager.setupInterfaceForClientMode(iface.name, Runnable::run, 1688 new NormalScanEventCallback(iface.name), 1689 new PnoScanEventCallback(iface.name))) { 1690 Log.e(TAG, "Failed to setup iface in wificond=" + iface.name); 1691 teardownInterface(iface.name); 1692 mWifiMetrics.incrementNumSetupClientInterfaceFailureDueToWificond(); 1693 return null; 1694 } 1695 registerInterfaceObserver(); 1696 iface.networkObserver = new NetworkObserverInternal(iface.id); 1697 if (!registerNetworkObserver(iface.networkObserver)) { 1698 Log.e(TAG, "Failed to register network observer for iface=" + iface.name); 1699 teardownInterface(iface.name); 1700 return null; 1701 } 1702 mWifiMonitor.startMonitoring(iface.name); 1703 // Just to avoid any race conditions with interface state change callbacks, 1704 // update the interface state before we exit. 1705 onInterfaceStateChanged(iface, isInterfaceUp(iface.name)); 1706 mWifiVendorHal.enableLinkLayerStats(iface.name); 1707 Log.i(TAG, "Successfully setup " + iface); 1708 1709 iface.featureSet = getSupportedFeatureSetInternal(iface.name); 1710 updateSupportedBandForStaInternal(iface); 1711 1712 mWifiVendorHal.enableStaChannelForPeerNetwork(mContext.getResources().getBoolean( 1713 R.bool.config_wifiEnableStaIndoorChannelForPeerNetwork), 1714 mContext.getResources().getBoolean( 1715 R.bool.config_wifiEnableStaDfsChannelForPeerNetwork)); 1716 return iface.name; 1717 } 1718 }分析这段函数中传入所有参数的作用
08-21
iptables -t nat -A PREROUTING -p tcp --dport 2028 -j DNAT --to-destination 192.168.61.198:2028 不迁移 应该怎么写 Usage: iptables -[ACD] chain rule-specification [options] iptables -I chain [rulenum] rule-specification [options] iptables -R chain rulenum rule-specification [options] iptables -D chain rulenum [options] iptables -[LS] [chain [rulenum]] [options] iptables -[FZ] [chain] [options] iptables -[NX] chain iptables -E old-chain-name new-chain-name iptables -P chain target [options] iptables -h (print this help information) Commands: Either long or short options are allowed. --append -A chain Append to chain --check -C chain Check for the existence of a rule --delete -D chain Delete matching rule from chain --delete -D chain rulenum Delete rule rulenum (1 = first) from chain --insert -I chain [rulenum] Insert in chain as rulenum (default 1=first) --replace -R chain rulenum Replace rule rulenum (1 = first) in chain --list -L [chain [rulenum]] List the rules in a chain or all chains --list-rules -S [chain [rulenum]] Print the rules in a chain or all chains --flush -F [chain] Delete all rules in chain or all chains --zero -Z [chain [rulenum]] Zero counters in chain or all chains --new -N chain Create a new user-defined chain --delete-chain -X [chain] Delete a user-defined chain --policy -P chain target Change policy on chain to target --rename-chain -E old-chain new-chain Change chain name, (moving any references) Options: --ipv4 -4 Nothing (line is ignored by ip6tables-restore) --ipv6 -6 Error (line is ignored by iptables-restore) [!] --protocol -p proto protocol: by number or name, eg. `tcp' [!] --source -s address[/mask][...] source specification [!] --destination -d address[/mask][...] destination specification [!] --in-interface -i input name[+] network interface name ([+] for wildcard) --jump -j target target for rule (may load target extension) --goto -g chain jump to chain with no return --match -m match extended match (may load extension) --numeric -n numeric output of addresses and ports [!] --out-interface -o output name[+] network interface name ([+] for wildcard) --table -t table table to manipulate (default: `filter') --verbose -v verbose mode --wait -w [seconds] maximum wait to acquire xtables lock before give up --wait-interval -W [usecs] wait time to try to acquire xtables lock default is 1 second --line-numbers print line numbers when listing --exact -x expand numbers (display exact values) [!] --fragment -f match second or further fragments only --modprobe=<command> try to insert modules using this command --set-counters PKTS BYTES set the counter during insert/append [!] --version -V print package version. 这个-h结果
07-10
#!/bin/sh [ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1 . /lib/functions.sh . /lib/netifd/common_api.sh . /lib/netifd/netifd-proto.sh . /usr/lib/libmsglog.sh . /usr/share/libubox/jshn.sh PPP_FLAG_FILE="/tmp/ppp_up_success" DHCPC_FILE="/tmp/firewall/dhcpc_status" deconfig_interface() { proto_init_update "*" 0 proto_send_update "$INTERFACE" } setup_interface () { proto_init_update "$1" 1 proto_add_ipv4_address "$3" "$4" # TODO: apply $broadcast msglog $LOG_PRIO_NOTICE "DHCPC" "DHCP Client obtained ip $3 mask $4" if [ ! -e $PPP_FLAG_FILE ]; then for i in $5; do proto_add_ipv4_route 0.0.0.0 0 "$i" done msglog $LOG_PRIO_NOTICE "DHCPC" "add route $5" else msglog $LOG_PRIO_NOTICE "DHCPC" "not set route, for pppoe successed" fi for tmp_dns in $6; do proto_add_dns_server "$tmp_dns" done for tmp_domain in $7; do proto_add_dns_search "$tmp_domain" done proto_send_update "$2" # IP变化,通知CHM更新本地节点 ubus call chm_dev_manager chm_ubus_localnode_change_handler > /dev/null 2>&1 } case "$1" in leasefail) local pre_ip local pre_subnet local pre_router local pre_dns #如果获取IP地址失败,使用上一个有效的IP地址 config_load protocol config_get pre_ip "smart_network" ipaddr config_get pre_subnet "smart_network" netmask config_get pre_router "smart_network" gateway config_get pre_dns "smart_network" dns if [ -n "$pre_ip" ]; then setup_interface "$IFNAME" "$INTERFACE" "$pre_ip" "$pre_subnet" "$pre_router" "$pre_dns" "" fi ;; renew|bound) local MACADDRSTR= local READTIME=0 local FAC_MACADDR= local FAC_MODE=`uci get system.basic.factory_test_mode` #当mac为烧录默认mac或产测模式时固定为静态地址192.168.0.240 MACADDRSTR=$(ubus call tddpServer getInfo '{"infoMask":1, "sep":":"}') while [ -z "$MACADDRSTR" -a "$READTIME" -le "4" ]; do sleep 1s MACADDRSTR=$(ubus call tddpServer getInfo '{"infoMask":1, "sep":":"}') READTIME=$(expr $READTIME + 1) done json_init json_load "$MACADDRSTR" json_get_var FAC_MACADDR mac if [ "$FAC_MACADDR" = "A8:57:4E:FD:93:5E" ] || [ "$FAC_MODE" = "on" ] ; then exit 0 fi local ignore=yes local pre_proto local pre_ip local dec_pre_ip local pre_subnet local dec_pre_subnet local dec_ip local dec_subnet #获取正在使用的有效IP地址 config_load protocol config_get ignore "smart_network" ignore config_get pre_proto "smart_network" proto config_get pre_ip "smart_network" ipaddr config_get pre_subnet "smart_network" netmask config_get pre_router "smart_network" gateway config_get pre_dns "smart_network" dns str2ip $pre_ip dec_pre_ip str2ip $pre_subnet dec_pre_subnet #上一次获取的IP地址,不一定是正在使用的IP地址 local last_ip local last_subnet local dec_last_ip local dec_last_subnet local added_device_num last_ip=$(cat "$DHCPC_FILE" | grep ip |cut -d \" -f2) last_subnet=$(cat "$DHCPC_FILE" | grep subnet |cut -d \" -f2) str2ip $last_ip dec_last_ip str2ip $last_subnet dec_last_subnet #转化成16进制 str2ip $ip dec_ip str2ip $subnet dec_subnet echo "ifname=\"$IFNAME\"" > "$DHCPC_FILE" echo "interface=\"$INTERFACE\"" >> "$DHCPC_FILE" echo "ip=\"$ip\"" >> "$DHCPC_FILE" echo "subnet=\"$subnet\"" >> "$DHCPC_FILE" echo "gateway=\"$router\"" >> "$DHCPC_FILE" echo "dns=\"$dns\"" >> "$DHCPC_FILE" if [ "$pre_proto" = "dhcp" ]; then if [ "$1" = "bound" ]; then if [ $(($dec_pre_ip & $dec_pre_subnet)) != $(($dec_ip & $dec_subnet)) ]; then for i in $(seq 1 1 20) do json_load "$(ubus call chm_dev_manager chm_ubus_get_device_num)" json_get_vars added_device_num if [ ! -z "$added_device_num" ]; then break fi sleep 1 done if [ $added_device_num = 0 ]; then setup_interface "$IFNAME" "$INTERFACE" "$ip" "$subnet" "$router" "$dns" "" config_load protocol uci_set protocol smart_network ignore no uci_set protocol smart_network proto dhcp uci_set protocol smart_network ipaddr $ip uci_set protocol smart_network netmask $subnet uci_set protocol smart_network gateway $router uci_set protocol smart_network dns "$dns" uci commit protocol else ubus call nvrgui nvrgui_refresh_status '{"type":4}' # 避免网络状态卡在连接中 if [ -n "$pre_ip" ]; then setup_interface "$IFNAME" "$INTERFACE" "$pre_ip" "$pre_subnet" "$pre_router" "$pre_dns" "" fi fi exit 0 fi elif [ "$1" = "renew" ]; then if [ "$ip" = "$pre_ip" ] && [ "$subnet" = "$pre_subnet" ] && [ "$router" = "$pre_router" ] && [ "$dns" = "$pre_dns" ]; then exit 0 else if [ "$ignore" = "no" ]; then if [ $(($dec_pre_ip & $dec_pre_subnet)) != $(($dec_ip & $dec_subnet)) ]; then json_load "$(ubus call chm_dev_manager chm_ubus_get_device_num)" json_get_vars added_device_num if [ $added_device_num = 0 ]; then setup_interface "$IFNAME" "$INTERFACE" "$ip" "$subnet" "$router" "$dns" "" else ubus call nvrgui nvrgui_refresh_status '{"type":4}' # 避免网络状态卡在连接中 if [ -n "$pre_ip" ]; then setup_interface "$IFNAME" "$INTERFACE" "$pre_ip" "$pre_subnet" "$pre_router" "$pre_dns" "" fi fi exit 0 fi else if [ $(($dec_last_ip & $dec_last_subnet)) != $(($dec_ip & $dec_subnet)) ]; then json_load "$(ubus call chm_dev_manager chm_ubus_get_device_num)" json_get_vars added_device_num if [ $added_device_num = 0 ]; then setup_interface "$IFNAME" "$INTERFACE" "$ip" "$subnet" "$router" "$dns" "" else ubus call nvrgui nvrgui_refresh_status '{"type":4}' # 避免网络状态卡在连接中 if [ -n "$pre_ip" ]; then setup_interface "$IFNAME" "$INTERFACE" "$pre_ip" "$pre_subnet" "$pre_router" "$pre_dns" "" fi fi exit 0 else exit 0 fi fi fi fi fi deconfig_interface setup_interface "$IFNAME" "$INTERFACE" "$ip" "$subnet" "$router" "$dns" "" if [ "$pre_proto" != "dhcp" ] || [ "$ip" != "$pre_ip" ] || [ "$subnet" != "$pre_subnet" ] || [ "$router" != "$pre_router" ] || [ "$dns" != "$pre_dns" ]; then config_load protocol uci_set protocol smart_network ignore no uci_set protocol smart_network proto dhcp uci_set protocol smart_network ipaddr $ip uci_set protocol smart_network netmask $subnet uci_set protocol smart_network gateway $router uci_set protocol smart_network dns "$dns" uci commit protocol fi ;; manuconfig) local pre_ifname local pre_interface local pre_ip local pre_subnet local pre_router local pre_dns pre_ifname=$(cat "$DHCPC_FILE" | grep ifname |cut -d \" -f2) pre_interface=$(cat "$DHCPC_FILE" | grep interface |cut -d \" -f2) pre_ip=$(cat "$DHCPC_FILE" | grep ip |cut -d \" -f2) pre_subnet=$(cat "$DHCPC_FILE" | grep subnet |cut -d \" -f2) pre_router=$(cat "$DHCPC_FILE" | grep gateway |cut -d \" -f2) pre_dns=$(cat "$DHCPC_FILE" | grep dns |cut -d \" -f2) if [ -n "$pre_ip" ]; then setup_interface "$pre_ifname" "$pre_interface" "$pre_ip" "$pre_subnet" "$pre_router" "$pre_dns" "" config_load protocol uci_set protocol smart_network ignore no uci_set protocol smart_network proto dhcp uci_set protocol smart_network ipaddr $pre_ip uci_set protocol smart_network netmask $pre_subnet uci_set protocol smart_network gateway $pre_router uci_set protocol smart_network dns "$pre_dns" uci commit protocol fi ;; cancel) config_load protocol uci_set protocol smart_network ignore yes uci commit protocol ;; esac exit 0
最新发布
11-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值