好物优选点击查看详情 京东购买

暂无内容

UBUNTU服务器网络操作

作者一直是ubuntu服务器的忠实拥护者,关于UBUNTU系统的很多配置都能通过脚本在软件包中一键部署,但关于网络配置则需要根据现场环境变动,因此本文的目的在于指导ubuntu服务器的常用的网络部署。

ubuntu的网络配置文件和其他linux发行版本不同,其网络配置文件为“/etc/network/interfaces”,它的优点在于配置简单,一个文件可搞定所有网卡配置。

1.1.查看网卡

查看所有网卡的指令为“ifconfig -a”,如下是作者测试机的网卡,总共包含eth0、eth1和eth2三张网卡(注意lo是回环网卡,不应计入)

root@msos:~# ifconfig -a eth0 Link encap:Ethernet HWaddr b8:ca:3a:b7:72:65 inet addr:192.168.202.245 Bcast:192.168.202.255 Mask:255.255.255.0 inet6 addr: fda5:c9dc:1a19:0:baca:3aff:feb7:7265/64 Scope:Global inet6 addr: fe80::baca:3aff:feb7:7265/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:4446963 errors:0 dropped:6966 overruns:0 frame:0 TX packets:188550792 errors:51 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3161258590 (3.1 GB) TX bytes:13076661272 (13.0 GB) eth1 Link encap:Ethernet HWaddr 2c:53:4a:01:11:2a inet addr:192.168.201.245 Bcast:192.168.201.255 Mask:255.255.255.0 inet6 addr: fe80::2e53:4aff:fe01:112a/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:530912 errors:0 dropped:0 overruns:0 frame:0 TX packets:166328360 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:57964481 (57.9 MB) TX bytes:10344129564 (10.3 GB) Memory:f7820000-f783ffff eth2 Link encap:Ethernet HWaddr 2c:53:4a:01:11:2b inet addr:192.168.200.245 Bcast:192.168.200.255 Mask:255.255.255.0 inet6 addr: fe80::2e53:4aff:fe01:112b/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:170654757 errors:0 dropped:2825 overruns:0 frame:0 TX packets:22269559 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11424225387 (11.4 GB) TX bytes:2827479338 (2.8 GB) Memory:f7800000-f781ffff 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:65536 Metric:1 RX packets:503680 errors:0 dropped:0 overruns:0 frame:0 TX packets:503680 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:41436424 (41.4 MB) TX bytes:41436424 (41.4 MB)
root@msos:~# ifconfig -a eth0 Link encap:Ethernet HWaddr b8:ca:3a:b7:72:65 inet addr:192.168.202.245 Bcast:192.168.202.255 Mask:255.255.255.0 inet6 addr: fda5:c9dc:1a19:0:baca:3aff:feb7:7265/64 Scope:Global inet6 addr: fe80::baca:3aff:feb7:7265/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:4446963 errors:0 dropped:6966 overruns:0 frame:0 TX packets:188550792 errors:51 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3161258590 (3.1 GB) TX bytes:13076661272 (13.0 GB) eth1 Link encap:Ethernet HWaddr 2c:53:4a:01:11:2a inet addr:192.168.201.245 Bcast:192.168.201.255 Mask:255.255.255.0 inet6 addr: fe80::2e53:4aff:fe01:112a/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:530912 errors:0 dropped:0 overruns:0 frame:0 TX packets:166328360 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:57964481 (57.9 MB) TX bytes:10344129564 (10.3 GB) Memory:f7820000-f783ffff eth2 Link encap:Ethernet HWaddr 2c:53:4a:01:11:2b inet addr:192.168.200.245 Bcast:192.168.200.255 Mask:255.255.255.0 inet6 addr: fe80::2e53:4aff:fe01:112b/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:170654757 errors:0 dropped:2825 overruns:0 frame:0 TX packets:22269559 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11424225387 (11.4 GB) TX bytes:2827479338 (2.8 GB) Memory:f7800000-f781ffff 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:65536 Metric:1 RX packets:503680 errors:0 dropped:0 overruns:0 frame:0 TX packets:503680 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:41436424 (41.4 MB) TX bytes:41436424 (41.4 MB) 
root@msos:~# ifconfig -a eth0 Link encap:Ethernet HWaddr b8:ca:3a:b7:72:65 inet addr:192.168.202.245 Bcast:192.168.202.255 Mask:255.255.255.0 inet6 addr: fda5:c9dc:1a19:0:baca:3aff:feb7:7265/64 Scope:Global inet6 addr: fe80::baca:3aff:feb7:7265/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:4446963 errors:0 dropped:6966 overruns:0 frame:0 TX packets:188550792 errors:51 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3161258590 (3.1 GB) TX bytes:13076661272 (13.0 GB) eth1 Link encap:Ethernet HWaddr 2c:53:4a:01:11:2a inet addr:192.168.201.245 Bcast:192.168.201.255 Mask:255.255.255.0 inet6 addr: fe80::2e53:4aff:fe01:112a/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:530912 errors:0 dropped:0 overruns:0 frame:0 TX packets:166328360 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:57964481 (57.9 MB) TX bytes:10344129564 (10.3 GB) Memory:f7820000-f783ffff eth2 Link encap:Ethernet HWaddr 2c:53:4a:01:11:2b inet addr:192.168.200.245 Bcast:192.168.200.255 Mask:255.255.255.0 inet6 addr: fe80::2e53:4aff:fe01:112b/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:9000 Metric:1 RX packets:170654757 errors:0 dropped:2825 overruns:0 frame:0 TX packets:22269559 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11424225387 (11.4 GB) TX bytes:2827479338 (2.8 GB) Memory:f7800000-f781ffff 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:65536 Metric:1 RX packets:503680 errors:0 dropped:0 overruns:0 frame:0 TX packets:503680 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:41436424 (41.4 MB) TX bytes:41436424 (41.4 MB)

1.2.基本格式

auto eth0 iface eth0 inet static address 192.168.200.136 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8
 auto eth0 iface eth0 inet static address 192.168.200.136 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 
auto eth0 iface eth0 inet static address 192.168.200.136 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8
auto eth0 iface eth0 inet dhcp
 auto eth0 iface eth0 inet dhcp 
auto eth0 iface eth0 inet dhcp
auto eth0 iface eth0 inet static address 192.168.200.136 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 auto eth0:1 iface eth0:1 inet dhcp
 auto eth0 iface eth0 inet static address 192.168.200.136 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 auto eth0:1 iface eth0:1 inet dhcp 
auto eth0 iface eth0 inet static address 192.168.200.136 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 auto eth0:1 iface eth0:1 inet dhcp

1.3.实际配置

通过vi或vim编辑器打开配置文件“/etc/network/interfaces”(注意:关于vi或vim编辑器如何使用,请自行上网搜索学习);

以下是作者测试机的配置信息,值得注意的是gateway和dns-nameservers这个字段只能配置到一个网卡,否则将造成多默认网关和DNS的问题

auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.202.245 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.202.245 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0 
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.202.245 netmask 255.255.255.0 gateway 192.168.200.1 dns-nameservers 8.8.8.8 auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0

以下是作者测试机的配置信息,值得注意的是eth0由于采用了静态和动态的方式,不应该再额外设置gateway

auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0 
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0

以下是作者测试机的配置信息,值得注意的是eth0由于采用了静态和动态的方式,不应该再额外设置gateway

auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.202.245 netmask 255.255.255.0 auto eth0:1 iface eth0:1 inet dhcp auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.202.245 netmask 255.255.255.0 auto eth0:1 iface eth0:1 inet dhcp auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0 
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.202.245 netmask 255.255.255.0 auto eth0:1 iface eth0:1 inet dhcp auto eth1 iface eth1 inet static address 192.168.201.245 netmask 255.255.255.0 auto eth2 iface eth2 inet static address 192.168.200.245 netmask 255.255.255.0

通过reboot指令来重启系统,达到修改配置生效的目的

使用flush加网络重启的方法达到立即生效,具体如下:
<1>修改了eth0,则使用如下命令:

ip addr flush dev eth0 && service networking restart
ip addr flush dev eth0 && service networking restart 
ip addr flush dev eth0 && service networking restart

<2>修改了eth0和eth1,则使用如下命令:

ip addr flush dev eth0 && ip addr flush dev eth1 &&service networking restart
ip addr flush dev eth0 && ip addr flush dev eth1 &&service networking restart 
ip addr flush dev eth0 && ip addr flush dev eth1 &&service networking restart

实现的目标:可通过以太网访问内网中某台设备的某个端口(多个端口原理一样)
实现方式:
1.DDNS(类似花生壳的软件),该方式自行摸索,略
2.虚拟服务器(主讲)

2.1.虚拟服务器配置

用于标识该虚拟服务项,主要是方便识别和记忆,一个名称而已
用于标识该虚拟服务项,主要是方便识别和记忆,一个名称而已 
用于标识该虚拟服务项,主要是方便识别和记忆,一个名称而已
外部访问的端口号
外部访问的端口号 
外部访问的端口号
内部访问的端口号
内部访问的端口号 
内部访问的端口号
内部访问的IP地址
内部访问的IP地址 
内部访问的IP地址
开启还是关闭虚拟服务项
开启还是关闭虚拟服务项 
开启还是关闭虚拟服务项

2.2.虚拟服务器实例

  • 服务器局域网地址为192.168.200.136
  • 服务器提供的其中一个直播流局域网的访问URL为http://192.168.200.136:8010/ch1.ts
  • 固定公网IP为153.124.89.16 映射后该直播流的访问URL为:http://153.124.89.16:10000/ch1.ts
  • 固定公网到服务器只经过一个路由器
  • 路由器的vlan口的DHCP功能开启
  • 路由器vlan的IP地址为192.168.1.1
  • 使用笔记本连接到路由器的vlan口上能正常上网(使用ping www.baidu.com进行验证
  • 服务器通过网卡eth0和路由器连接
  • 修改服务器eth0的网段为192.168.1.136
  • 通过重启服务器或flush的方式使能网络配置
  • 确认服务器能连接到以太网(使用ping www.baidu.com进行验证);
  • 在路由器上的虚拟服务器功能中配置虚拟服务项;服务名称:ccx_streamserver、外部端口:10000、内部端口:8010、内部服务器IP:192.168.1.136、状态:开启
  • 完成配置,访问http://153.124.89.16:10000/ch1.ts进行验证

原文链接:https://blog.csdn.net/weixin_35804181/article/details/77865285

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享