龙空技术网

linux修改ip的方式和web服务器重启生效的问题

三千雷 451

前言:

目前兄弟们对“linux重启ip没了”大致比较关切,朋友们都想要了解一些“linux重启ip没了”的相关知识。那么小编同时在网摘上汇集了一些对于“linux重启ip没了””的相关资讯,希望咱们能喜欢,小伙伴们快快来了解一下吧!

环境:web服务器采用的是Lighttpd,移植到嵌入式开发板上。

修改ip方式一:

{

int sock = 0;

struct ifreq ifr;

if((pIPAddr == NULL) || (pIfName == NULL))

{

PRINT_ERR("set ip: pInterface == NULL\r\n");

return -1;

}

memset(&ifr, 0, sizeof(ifr));

strncpy(ifr.ifr_name, pIfName, IFNAMSIZ);

sock = socket(AF_INET, SOCK_DGRAM, 0);

if(sock <= 0)

{

PRINT_ERR("set ip: sock error, %s\r\n", strerror(errno));

return -1;

}

struct sockaddr_in sin;

memset(&sin, 0, sizeof(struct sockaddr_in));

sin.sin_family = AF_INET;

sin.sin_addr.s_addr = inet_addr(pIPAddr);

memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr_in));

if(ioctl(sock, SIOCSIFADDR, &ifr) < 0)

{

PRINT_ERR("set ip(%s) error!\r\n", pIPAddr);

close(sock);

return -1;

}

//设置激活标志

ifr.ifr_flags |= IFF_UP |IFF_RUNNING;

//get the status of the device

if( ioctl( sock, SIOCSIFFLAGS, &ifr ) < 0 )

{

perror("SIOCSIFFLAGS");

close(sock);

return -1;

}

close(sock);

return 0;

}

修改ip方式二:

/*config ip netmask*///

system("/sbin/ifconfig eth0 down");

sprintf(cmd,"/sbin/ifconfig eth0 %s netmask %s",ipAddr,netMask);

system(cmd);

system("ifconfig eth0 up");

分别以以上两种方式对ip进行修改,然后重启Lighttpd web服务器。方式一的情况下,ip修改成功,但是web服务器登录不上去,方式二的方式可以正常运行。

是否这两种方式修改ip,都对系统底层即时生效了?

标签: #linux重启ip没了