ifcfg-eth0文件参数PREFIX 和 NETMASK的配置不一致问题
http://blog.csdn.net/aikui0621/article/details/9148997
搭建一个简单的网络测试环境,现有服务器ip配置为10.131.4.33 掩码为255.255.254.0网关为 10.131.5.254, 需要访问
ip为10.1.7.110 的服务器,需要手动配置ifcfg-eth0文件,系统中自带了个PREFIX参数,由于不知道什么用就没有删除,
结果在修改NETMASK的过程中悲剧就发生了。
原始系统自带配置文件(为接手此服务器时的配置文件)如下:
由于不太了解PREFIX参数具体作用,就没有删除,仅修改IPADDR、NETMASK和GATEWAY,修改后如下:
执行 $services network restart 后 执行 $ ifconfig eth0 会发现netmask 项值并没有更改,而是255.0.0.0
一直找不到原因,进入图形查看网络设备信息发现 掩码项 为配置值 255.255.254.0 (此处未截图)
百思不得其解,不知道为什么配置正常后,掩码没有修改,反复检查配置文件后依然无果,无奈之下,写了脚本,
在重启网卡后,手动键入命令 $ ifconfig eth0 netmask 255.255.254.0 up 发现 $ ifconfig eth0 显示掩码配置正常,
但是依然ping不通 10.1.7.110的服务器,进入了死胡同,网上查找不到相关问题,最后尝试将配置文件精简到最简,版本如下:
发现奇迹般的正常了,可以ping同10.1.7.110服务器,问题解决,但是此时一肚子的疑惑, 开始查找原因!
首先对比发现前后配置文件缺失一个关键选项 PREFIX。回过头来注意此选项怀疑跟掩码有关,经验证果然如此,
在PREFIX设置8-23之间的值时,$ ifconfig eth0发现 netmask会根据此值相应改变,配置项NETMASK并未生效。但是
在将PREFIX项配置错误(即NETMASK为255.255.254.0时,PREFIX值在24-32)时,执行 $services network restart 时报错,
出错信息如下:
查找到一个相关的帖子,地址如下:
不过其中有个问题是仅配置PREFIX项不配置NETMASK会出现不稳定问题,未进行测试。现在就剩下当配置PREFIX时,
NETMASK选项会不生效的问题,先查看 /etc/sysconfig/network-scripts/network-functions 脚本发现如下代码:
- 133 expand_config ()
- 134 {
- 135 if [ -z "${NETMASK}" ]; then
- 136 eval `/bin/ipcalc --netmask ${IPADDR}`
- 137 fi
- 138
- 139 if [ -z "${PREFIX}" ]; then
- 140 eval `/bin/ipcalc --prefix ${IPADDR} ${NETMASK}`
- 141 fi
- 142
- 143 if [ -z "${BROADCAST}" ]; then
- 144 eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
- 145 fi
- 146
- 147 if [ -z "${NETWORK}" ]; then
- 148 eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
- 149 fi
- 150 }
发现脚本会读取ifcfg-eth0中配置项,作为ipcalc工具的参数进行配置,接下来查找ipcalc工具源码,发现有prel脚本编写的也有c编写的,
由于对prel不是很熟悉,贴出C源码如下:
- /* vi: set sw=4 ts=4: */
- /*
- * Mini ipcalc implementation for busybox
- *
- * By Jordan Crouse <jordan@cosmicpenguin.net>
- * Stephan Linz <linz@li-pro.net>
- *
- * This is a complete reimplementation of the ipcalc program
- * from Red Hat. I didn't look at their source code, but there
- * is no denying that this is a loving reimplementation
- *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
- */
- #include "busybox.h"
- #include <ctype.h>
- #include <getopt.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #define CLASS_A_NETMASK ntohl(0xFF000000)
- #define CLASS_B_NETMASK ntohl(0xFFFF0000)
- #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
- static unsigned long get_netmask(unsigned long ipaddr)
- {
- ipaddr = htonl(ipaddr);
- if ((ipaddr & 0xC0000000) == 0xC0000000)
- return CLASS_C_NETMASK;
- else if ((ipaddr & 0x80000000) == 0x80000000)
- return CLASS_B_NETMASK;
- else if ((ipaddr & 0x80000000) == 0)
- return CLASS_A_NETMASK;
- else
- return 0;
- }
- #ifdef CONFIG_FEATURE_IPCALC_FANCY
- static int get_prefix(unsigned long netmask)
- {
- unsigned long msk = 0x80000000;
- int ret = 0;
- netmask = htonl(netmask);
- while (msk) {
- if (netmask & msk)
- ret++;
- msk >>= 1;
- }
- return ret;
- }
- #else
- int get_prefix(unsigned long netmask);
- #endif
- #define NETMASK 0x01
- #define BROADCAST 0x02
- #define NETWORK 0x04
- #define NETPREFIX 0x08
- #define HOSTNAME 0x10
- #define SILENT 0x20
- #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
- static const struct option long_options[] = {
- { "netmask", no_argument, NULL, 'm' },
- { "broadcast", no_argument, NULL, 'b' },
- { "network", no_argument, NULL, 'n' },
- # if ENABLE_FEATURE_IPCALC_FANCY
- { "prefix", no_argument, NULL, 'p' },
- { "hostname", no_argument, NULL, 'h' },
- { "silent", no_argument, NULL, 's' },
- # endif
- { NULL, 0, NULL, 0 }
- };
- #endif
- int ipcalc_main(int argc, char **argv)
- {
- unsigned opt;
- int have_netmask = 0;
- in_addr_t netmask, broadcast, network, ipaddr;
- struct in_addr a;
- char *ipstr;
- #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
- applet_long_options = long_options;
- #endif
- opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
- argc -= optind;
- argv += optind;
- if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
- if (argc > 2 || argc <= 0)
- bb_show_usage();
- } else {
- if (argc != 1)
- bb_show_usage();
- }
- if (opt & SILENT)
- logmode = LOGMODE_NONE; /* Suppress error_msg() output */
- ipstr = argv[0];
- if (ENABLE_FEATURE_IPCALC_FANCY) {
- unsigned long netprefix = 0;
- char *prefixstr;
- prefixstr = ipstr;
- while (*prefixstr) {
- if (*prefixstr == '/') {
- *prefixstr = (char)0;
- prefixstr++;
- if (*prefixstr) {
- unsigned msk;
- netprefix = xatoul_range(prefixstr, 0, 32);
- netmask = 0;
- msk = 0x80000000;
- while (netprefix > 0) {
- netmask |= msk;
- msk >>= 1;
- netprefix--;
- }
- netmask = htonl(netmask);
- /* Even if it was 0, we will signify that we have a netmask. This allows */
- /* for specification of default routes, etc which have a 0 netmask/prefix */
- have_netmask = 1;
- }
- break;
- }
- prefixstr++;
- }
- }
- ipaddr = inet_aton(ipstr, &a);
- if (ipaddr == 0) {
- bb_error_msg_and_die("bad IP address: %s", argv[0]);
- }
- ipaddr = a.s_addr;
- if (argc == 2) {
- if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
- bb_error_msg_and_die("use prefix or netmask, not both");
- }
- netmask = inet_aton(argv[1], &a);
- if (netmask == 0) {
- bb_error_msg_and_die("bad netmask: %s", argv[1]);
- }
- netmask = a.s_addr;
- } else {
- /* JHC - If the netmask wasn't provided then calculate it */
- if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
- netmask = get_netmask(ipaddr);
- }
- if (opt & NETMASK) {
- printf("NETMASK=%sn", inet_ntoa((*(struct in_addr *) &netmask)));
- }
- if (opt & BROADCAST) {
- broadcast = (ipaddr & netmask) | ~netmask;
- printf("BROADCAST=%sn", inet_ntoa((*(struct in_addr *) &broadcast)));
- }
- if (opt & NETWORK) {
- network = ipaddr & netmask;
- printf("NETWORK=%sn", inet_ntoa((*(struct in_addr *) &network)));
- }
- if (ENABLE_FEATURE_IPCALC_FANCY) {
- if (opt & NETPREFIX) {
- printf("PREFIX=%in", get_prefix(netmask));
- }
- if (opt & HOSTNAME) {
- struct hostent *hostinfo;
- int x;
- hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
- if (!hostinfo) {
- bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
- }
- for (x = 0; hostinfo->h_name[x]; x++) {
- hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
- }
- printf("HOSTNAME=%sn", hostinfo->h_name);
- }
- }
- return EXIT_SUCCESS;
- }
阅读源码后发现,选项PREFIX的配置值在此处并未生效,此工具完全根据输入的ip地址和掩码进行分析,应该不会出现ifcfg-eth0中的掩码配置不生效的问题。
测试环境暂时搭建完成,NETMASK和PREFIX配置冲突问题还未找到合理的解释!!!