Linux ->> UBuntu 14.04 LTE下设置静态IP地址

时间:2022-10-31 20:32:44

UBuntu 14.04 LTE设置IP地址和一些服务器版本的Linux还不太一样。以Centos 7.0为例,网卡IP地址的配置文件应该是/etc/sysconfig/network-scripts/ifcfg-eth0

首先UBuntu 14.04 LTE下面IP地址的配置文件是/etc/network/interfaces。

jerry@ubuntu:/etc/network$ cat /etc/network/interfaces
# interfaces() file used by ifup() and ifdown()
auto lo
iface lo inet loopback auto eth0
iface eth0 inet static
address 192.168.2.100
gateway 192.168.2.2
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
dns-nameservers 8.8.8.8
jerry@ubuntu:/etc/network$

把地址配好后用ifdown eth0后跟上ifup eth0重启网卡让前面的配置生效

jerry@ubuntu:/etc/network$ sudo ifdown eth0
jerry@ubuntu:/etc/network$ sudo ifup eth0
jerry@ubuntu:/etc/network$ ifconfig
eth0 Link encap:Ethernet HWaddr :0c::e7:e6:
inet addr:192.168.2.100 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fee7:e657/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (31.2 KB) TX bytes: (46.4 KB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (8.3 MB) TX bytes: (8.3 MB) jerry@ubuntu:/etc/network$ ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) () bytes of data.
bytes from 61.135.169.121: icmp_seq= ttl= time= ms
bytes from 61.135.169.121: icmp_seq= ttl= time=54.9 ms
^C
--- www.a.shifen.com ping statistics ---
packets transmitted, received, % packet loss, time 6396ms
rtt min/avg/max/mdev = 54.940/78.678/102.417/23.740 ms

有时你在一台刚刚安装好的Ubuntu机器上运行ifdown eth0命令可能会出现ifdown: interface eth0 not configured这样的错误提示,原因是ifdown和ifup是依据/run/network/ifstate里的内容来知道有哪些网络接口。cat一下有没有eth0,没有就添加eth0=eth0到文件的尾端。然后再运行下命令就解决了。

jerry@ubuntu:/etc/network$ sudo ifdown eth0
ifdown: interface eth0 not configured
jerry@ubuntu:/etc/network$ ifconfig
eth0 Link encap:Ethernet HWaddr :0c:::f1:cb
inet addr:192.168.2.129 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe96:f1cb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (7.5 MB) TX bytes: (232.6 KB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (16.9 KB) TX bytes: (16.9 KB) jerry@ubuntu:/etc/network$ ping 192.168.2.2
PING 192.168.2.2 (192.168.2.2) () bytes of data.
bytes from 192.168.2.2: icmp_seq= ttl= time=0.328 ms
bytes from 192.168.2.2: icmp_seq= ttl= time=0.153 ms
bytes from 192.168.2.2: icmp_seq= ttl= time=0.285 ms
^C
--- 192.168.2.2 ping statistics ---
packets transmitted, received, % packet loss, time 1998ms
rtt min/avg/max/mdev = 0.153/0.255/0.328/0.075 ms
jerry@ubuntu:/etc/network$ cd /run/network/
jerry@ubuntu:/run/network$ ls
ifstate ifup.eth0 ifup.lo static-network-up-emitted
jerry@ubuntu:/run/network$ cat if
ifstate ifup.eth0 ifup.lo
jerry@ubuntu:/run/network$ cat ifstate
lo=lo
jerry@ubuntu:/run/network$ vim ifstate
jerry@ubuntu:/run/network$ sudo vim ifstate
jerry@ubuntu:/run/network$ sudo ifdown eth0
RTNETLINK answers: No such process
jerry@ubuntu:/run/network$ sudo ifup eth0
jerry@ubuntu:/run/network$ ifconfig
eth0 Link encap:Ethernet HWaddr :0c:::f1:cb
inet addr:192.168.2.129 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe96:f1cb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (7.5 MB) TX bytes: (240.5 KB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (24.0 KB) TX bytes: (24.0 KB) jerry@ubuntu:/run/network$ ifconfig
eth0 Link encap:Ethernet HWaddr :0c:::f1:cb
inet addr:192.168.2.129 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe96:f1cb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (7.5 MB) TX bytes: (242.1 KB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (24.0 KB) TX bytes: (24.0 KB) jerry@ubuntu:/run/network$ sudo ifdown eth0
jerry@ubuntu:/run/network$ sudo ifup eth0
jerry@ubuntu:/run/network$ ifconfig
eth0 Link encap:Ethernet HWaddr :0c:::f1:cb
inet addr:192.168.2.110 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe96:f1cb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (7.5 MB) TX bytes: (250.3 KB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (24.5 KB) TX bytes: (24.5 KB) jerry@ubuntu:/run/network$

下面是Ubuntu的官方文档专门讲网络设置的

网络配置