Linux的网络相关的配置
一、在ifconfig上修改配置
1.1 查看IP
[root@localhost ~]# ifconfig #命令
eth0 Link encap:Ethernet HWaddr 00:0C:29:5B:91:34 # 连接类型 硬件的MAC地址
inet addr:172.16.11.71 Bcast:172.16.11.255 Mask:255.255.255.0 #IP 子网 掩码
inet6 addr: fe80::20c:29ff:fe5b:9134/64 Scope:Link #IPV6的地址情况
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 # 网卡是否开启(RUNNING代表的是网卡的网线被连接上) 支持组播 MTU最大传输单元
RX packets:6927 errors:0 dropped:0 overruns:0 frame:0 #发送和接收数据包的统计情况
TX packets:176 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:454214 (443.5 KiB) TX bytes:16906 (16.5 KiB) #接收、发送数据字节数统计信息;
1.2 查看所有网卡
[root@localhost ~]# ifconfig -a
eth1 Link encap:Ethernet HWaddr 00:0C:29:5F:7A:F9
inet addr:172.16.11.55 Bcast:172.16.11.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5f:7af9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:19492 errors:0 dropped:0 overruns:0 frame:0
TX packets:293 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1411302 (1.3 MiB) TX bytes:29952 (29.2 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:720 (720.0 b) TX bytes:720 (720.0 b)
1.3 关闭网卡
[root@localhost ~]# ifconfig eth0 down关闭网卡
1.4 开启网卡
[root@localhost ~]# ifconfig eth0 up激活网卡
1.5 为网卡配置IPV6的地址及删除
[root@localhost ~]# ifconfig eth1 add 33ff:3333:8000:1002::2/64 立即生效,重启丢弃
[root@localhost ~]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:5F:7A:F9
inet addr:172.16.11.55 Bcast:172.16.11.255 Mask:255.255.255.0
inet6 addr: 33ff:3333:8000:1002::2/64 Scope:Global
inet6 addr: fe80::20c:29ff:fe5f:7af9/64 Scope:Link
[root@localhost ~]# ifconfig eth1 del 33ff:3333:8000:1002::2/64
[root@localhost ~]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:5F:7A:F9
inet addr:172.16.11.55 Bcast:172.16.11.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5f:7af9/64 Scope:Link
1.6 设置IP地址和掩码及网关 :立即生效,重启丢弃
[root@localhost ~]# ifconfig eth0 172.16.1.3 netmask 255.255.0.0
[root@localhost ~]# ifconfig eth0 172.16.1.3 netmask 255.255.0.0 broadcast 172.16.1.255
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:FF
inet addr:172.16.1.3 Bcast:172.16.1.255 Mask:255.255.0.0
设置网关:
[root@localhost 桌面]# route add default gw 172.16.11.1
[root@localhost 桌面]# route #查看网关
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.11.0 * 255.255.255.0 U 0 0 0 eth1
default 172.16.11.1 0.0.0.0 UG 0 0 0 eth1
1.7 修改MAC地址: 立即生效,重启丢弃
[root@localhost 桌面]# ifconfig eth1 hw ether 00:AA:BB:CC:DD:FF
[root@localhost 桌面]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:FF
1.8 启用网卡ARP协议或关闭:立即生效,重启丢弃
[root@localhost ~]# ifconfig eth0 arp
[root@localhost ~]# ifconfig eth0 -arp
1.9 设置网卡的最大传输单元 :立即生效,重启丢弃
[root@localhost ~]# ifconfig eth0 mtu 1500
1.10 网卡的DNS配置
本地解析
[root@localhost 桌面]# cd /
[root@localhost /]# cd /etc/
[root@localhost etc]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
1.11 DNS的服务器IP地址设置
[root@localhost /]# cd /etc/
[root@localhost etc]# ls resolv.conf
resolv.conf
[root@localhost etc]# cat resolv.conf
# Generated by NetworkManager
nameserver 61.139.2.69
nameserver 202.98.96.68
1.12 Linux网卡的子接口
[root@localhost etc]# ifconfig eth1:2 172.16.11.71
[root@localhost etc]# ifconfig eth1:3 172.16.11.72 netmask 255.255.255.0
[root@localhost etc]# ifconfig eth1:4 172.16.11.73 netmask 255.255.255.0
[root@localhost etc]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:FF
inet addr:172.16.11.239 Bcast:172.16.11.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5f:7af9/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:28040 errors:0 dropped:0 overruns:0 frame:0
TX packets:617 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1995398 (1.9 MiB) TX bytes:68237 (66.6 KiB)
eth1:2 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:FF
inet addr:172.16.11.71 Bcast:172.16.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
eth1:3 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:FF
inet addr:172.16.11.72 Bcast:172.16.11.255 Mask:255.255.255.0
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
eth1:4 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:FF
inet addr:172.16.11.73 Bcast:172.16.11.255 Mask:255.255.255.0
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
总结:
通过ifconfig配置的所有参数都是临时生效,要永久的生效必须修改配置文件,配置文件修改必须重启相应的服务才会立即生效(DHCP DNS TELNET --------HTTP)
二、修改配置文件
2.1 永久修改IP地址(工作中的应用方式)
[root@localhost network-scripts]# cat ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:5B:91:34
TYPE=Ethernet
UUID=e9d70dee-77ba-4336-a75d-76e8a20f1c49
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
[root@localhost network-scripts]# vi ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:5B:91:34
TYPE=Ethernet
UUID=e9d70dee-77ba-4336-a75d-76e8a20f1c49
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=172.16.11.239
NETMASK=255.255.255.0
GATWAY=172.16.11.1
DNS1=8.8.8.8
IPV6INIT=no
USERCTL=no
Linux修改配置文件一定要重启相关服务才会立即生效
[root@localhost network-scripts]# service network stop
正在关闭接口 eth0: 设备状态:3 (断开连接)
[确定]
关闭环回接口: [确定]
[root@localhost network-scripts]# service network start
弹出环回接口: [确定]
弹出界面 eth0: 活跃连接状态:激活的
活跃连接路径:/org/freedesktop/NetworkManager/ActiveConnection/2
[确定]
[root@localhost network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:5B:91:34
inet addr:172.16.11.239 Bcast:172.16.11.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5b:9134/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:41189 errors:0 dropped:0 overruns:0 frame:0
TX packets:542 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2984634 (2.8 MiB) TX bytes:48640 (47.5 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:22 errors:0 dropped:0 overruns:0 frame:0
TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2648 (2.5 KiB) TX bytes:2648 (2.5 KiB)
[root@localhost network-scripts]# service network restart
正在关闭接口 eth0: 设备状态:3 (断开连接)
[确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
弹出界面 eth0: 活跃连接状态:激活的
活跃连接路径:/org/freedesktop/NetworkManager/ActiveConnection/3
[确定]
2.2 全局修改网关或者DNS或者主机名:
[root@localhost /]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=172.16.11.1
2.3 修改DNS服务器
[root@localhost /]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 61.139.2.69
nameserver 202.98.96.68
2.4修改主机名
[root@localhost /]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=Train-open01
GATEWAY=172.16.1.1
三、实验
3.1 通过手工设置IP地址使Linux主要能够上网
-
配置代码:
[root@localhost 桌面]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth1 [root@localhost network-scripts]# vi ifcfg-eth1 EVICE=eth1 HWADDR=00:0C:29:5F:7A:F9 TYPE=Ethernet UUID=e9d70dee-77ba-4336-a75d-76e8a20f1c49 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=172.16.11.238 NETMASK=255.255.255.0 GATWAY=172.16.11.1 DNS1=8.8.8.8 IPV6INIT=no USERCTL=no [root@localhost network-scripts]# service network restart
-
实验截图:
3.2两台Linux服务器通过手工配置IP地址实现互相通信并且能上网
配置同上
实验截图: