一、修改配置文件,重启后设置不丢失
【Red Hat Linux/CentOS】
使用ifconfig查看使用的网口:
[root@localhost /]# ifconfig
修改对应网口配置文件:
[root@localhost /]# vi /etc/sysconfig/network-scripts/ifcfg-ens192
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens192
UUID=dcb6f08a-1e19-44fe-9427-479fcebc1cd3
DEVICE=ens192
ONBOOT=yes
IPADDR=10.8.3.40
NETMASK=255.255.255.0
GATEWAY=10.8.3.254
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
DNS1=10.8.1.2
重新启动网卡:
[root@localhost network-scripts]# service network restart
【Ubuntu】
使用ifconfig查看使用的网口:
[root@localhost /]# ifconfig
修改配置网口文件:
root@ubuntu14:/# vi /etc/network/interfaces
#This file describes the network interfaces available on your system
#and how to activate them. For more information, see interfaces(5).
#The loopback network interface
auto lo
iface lo inet loopback
#The primary network interface
#This is an autoconfigured IPv6 interface
auto eth1
iface eth1 inet static
address 10.8.3.105
netmask 255.255.255.0
gateway 10.8.3.254
修改DNS:
root@ubuntu14:/etc# vi /etc/resolv.conf
#Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#DO NOT EDIT THIS FILE BY HAND – YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.8.1.25
nameserver 8.8.8.8
重新启动网卡:
root@ubuntu14:/etc# service networking restart
二、命令修改,重启后设置丢失
ifconfig命令只能在root用户下执行
[root@localhost /]# ifconfig eth0 10.8.3.159 netmask 255.255.255.0
[root@localhost /]#route add default gw 10.8.3.254
三、其他命令使用
1)关闭或者重启网口
[root@localhost /]# ifconfig eth0 down
[root@localhost /]# ifconfig eth0 up
2)查看路由表
root@ubuntu14:/# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.8.3.254 0.0.0.0 UG 0 0 0 eth1
10.8.3.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1