六、网络服务
一、 linux 的网络服务
ifconfig 作用
可以开启和关闭网卡:ifconfig 网卡名称 up
关闭一块网卡:ifconfig 网卡名称 down
临时更改网络地址(更改内存 重启失效: ifconfig 网卡名称 ip 地址
永久更改网络的配置信息:
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
hosts 只能记录 15 条
二、 LINUX 主机
RHEL6 以下版本
/etc/sysconfig/network 文件
参数 说明
NETWORKING 设置是否启用计算机的网络功能
HOSTNAME 设置主机名 重启有效
GETWAY 设置默认网关,如果网卡文件内设置相关参数,则该参数失效
RHEL7 中修改主机名称
[root@wushengmin ~]# vim /etc/hostname
[root@wushengmin ~]# reboot
三、 路由
设置静态路由表:
创建静态路由
router【add|del】[-net] [-host 目标地址的类型主机/网段] target 目的地址【netmask 子网掩码】metrice 路由成本 gw 网关地址 dev 网络接口
如果更改默认路由 在 add|del 后边添加 default
通过 router 书写的命令在重启后实效
四、 单网卡多 ip
[root@localhost ~]# systemctl stop NetworkManager.service //关闭 networkmanager
[root@localhost ~]# systemctl disable NetworkManager //设置开机不自己启动
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:1
[root@localhost network-scripts]# vi ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.9.201
NETMASK=255.255.255.0
GATEWAY=192.168.9.254
[root@localhost network-scripts]# vi ifcfg-ens33:1
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
DEVICE=ens33:1
ONBOOT=yes
IPADDR=192.168.9.200
NETMASK=255.255.255.0
GATEWAY=192.168.9.254
[root@localhost network-scripts]# systemctl restart network
五、 nmcli
RHCL7 nmcli 的命令(支持参数补齐)
1.使用 networkmanager
[root@localhost network-scripts]# systemctl start NetworkManager
1.team:链路聚合 。 将多块网卡做绑定做一件事情
2.broadcast(广播):一个简单的运行程序,传输来自各个端口的每个包
3.Roundrobin(轮询):以轮询的形式传输每个端口的包
4.activebackup(热备):热备份
loadbalance(负载均衡):监视流量并使用哈希函数尝试每个包的传输过程中选择端口的时候达到万事均衡(最好
用的模式,但是 cpu 占用大 土豪请无视)
lacp:实施 820.3ad 链路聚合协议(硬件网卡,帮助 cpu 做运算)额外添加 贵
实验:使用 Team 聚合链接
[root@localhost ~]# nmcli connection add autoconnect yes type team con-name team0 ifname team0 config
'{"runner":{"name":"activebackup"}}' ip4 172.25.6.100/24 gw4 172.25.6.254 //添加链接 指定参数
Connection 'team0' (45ecef41-0a78-4a80-9353-159104d24be3) successfully added.
[root@localhost ~]# nmcli connection //查看链接
NAME UUID TYPE DEVICE
Wired connection 1 4de4ce91-1370-382b-86ca-c7a845da2835 802-3-ethernet ens37
Wired connection 2 82c8770b-2fc4-386b-b323-cf122f17a56e 802-3-ethernet ens38
ens33 c96bc909-188e-ec64-3a96-6a90982b08ad 802-3-ethernet ens33
team0 45ecef41-0a78-4a80-9353-159104d24be3 team team0
virbr0 73b667ac-78f6-48fb-91f0-c37897d87b29 bridge virbr0
[root@localhost ~]# nmcli connection add type team-slave con-name team0-port1 ifname ens33 master team0
//将 ens33 加入到 team0 这个组里
Connection 'team0-port1' (a9dfad3e-f134-417d-8b97-90959962f83e) successfully added.
[root@localhost ~]# nmcli connection add type team-slave con-name team0-port2 ifname ens37 master team0
//将 ens37 加入到 team0 这个组里
Connection 'team0-port2' (cfe09c3a-0916-4ecf-98ab-2eac717baed7) successfully added.
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
Wired connection 1 4de4ce91-1370-382b-86ca-c7a845da2835 802-3-ethernet ens37
Wired connection 2 82c8770b-2fc4-386b-b323-cf122f17a56e 802-3-ethernet ens38
ens33 c96bc909-188e-ec64-3a96-6a90982b08ad 802-3-ethernet ens33
team0 45ecef41-0a78-4a80-9353-159104d24be3 team team0
virbr0 73b667ac-78f6-48fb-91f0-c37897d87b29 bridge virbr0
team0-port1 a9dfad3e-f134-417d-8b97-90959962f83e 802-3-ethernet --
team0-port2 cfe09c3a-0916-4ecf-98ab-2eac717baed7 802-3-ethernet --
[root@localhost ~]# teamdctl team0 state //查看 team0 中的网络信息
setup:
runner: roundrobin
[root@localhost ~]# nmcli connection up team0-port1 //开启 team0 的链路聚合
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
[root@localhost ~]# nmcli connection up team0-port2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
Wired connection 2 82c8770b-2fc4-386b-b323-cf122f17a56e 802-3-ethernet ens38
team0 45ecef41-0a78-4a80-9353-159104d24be3 team team0
team0-port1 a9dfad3e-f134-417d-8b97-90959962f83e 802-3-ethernet ens33
team0-port2 cfe09c3a-0916-4ecf-98ab-2eac717baed7 802-3-ethernet ens37
virbr0 73b667ac-78f6-48fb-91f0-c37897d87b29 bridge virbr0
Wired connection 1 4de4ce91-1370-382b-86ca-c7a845da2835 802-3-ethernet --
ens33 c96bc909-188e-ec64-3a96-6a90982b08ad 802-3-ethernet --
[root@localhost ~]# ifdown eth33 . // 禁用 eth33 这块网卡
[root@localhost ~]# ifup eth33 //启用
创建桥接网络
[root@localhost ~]# nmcli connection add autoconnect yes type bridge con-name bridge1 ifname bridge1 ip4
192.168.1.201 gw4 192.168.1.100
[root@localhost ~]# nmcli connection add type bridge-slave ifname ens33 con-name bridge-port1 master bridge1
Connection 'bridge-port1' (a79f8dc9-b92a-4fe2-a117-c37d2ba23b89) successfully added.
[root@localhost ~]# nmcli connection add type bridge-slave ifname ens37 con-name bridge-port2 master bridge1
Connection 'bridge-port2' (59ea460a-c581-4610-af3e-177ff4dc4092) successfully added.
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
Wired connection 1 4de4ce91-1370-382b-86ca-c7a845da2835 802-3-ethernet ens37
Wired connection 2 82c8770b-2fc4-386b-b323-cf122f17a56e 802-3-ethernet ens38
bridge1 ab7569ea-6b37-4c77-b082-884bb402a93f bridge bridge1
ens33 1896dc07-66aa-41e4-bd85-60ee424a3971 802-3-ethernet ens33
virbr0 0a97df80-6d26-4d77-b5d5-064764eaa437 bridge virbr0
bridge-port1 a79f8dc9-b92a-4fe2-a117-c37d2ba23b89 802-3-ethernet --
bridge-port2 59ea460a-c581-4610-af3e-177ff4dc4092 802-3-ethernet --
[root@localhost ~]# nmcli connection up bridge-port1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
[root@localhost ~]# nmcli connection up bridge-port2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
bridge1 8000.000c29400e85 yes ens33
ens37
virbr0 8000.52540022d9b0 yes virbr0-nic
[root@localhost ~]# nmcli connection modify ens33 connection.id home
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
Wired connection 2 82c8770b-2fc4-386b-b323-cf122f17a56e 802-3-ethernet ens38
bridge-port1 a79f8dc9-b92a-4fe2-a117-c37d2ba23b89 802-3-ethernet ens33
bridge-port2 59ea460a-c581-4610-af3e-177ff4dc4092 802-3-ethernet ens37
bridge1 ab7569ea-6b37-4c77-b082-884bb402a93f bridge bridge1
virbr0 0a97df80-6d26-4d77-b5d5-064764eaa437 bridge virbr0
Wired connection 1 4de4ce91-1370-382b-86ca-c7a845da2835 802-3-ethernet --
home 1896dc07-66aa-41e4-bd85-60ee424a3971 802-3-ethernet --
[root@localhost ~]# nmcli d
DEVICE TYPE STATE CONNECTION
bridge1 bridge connected bridge1
virbr0 bridge connected virbr0
ens33 ethernet connected bridge-port1
ens37 ethernet connected bridge-port2
ens38 ethernet connected Wired connection 2
lo loopback unmanaged --
virbr0-nic tun unmanaged --
[root@localhost ~]# nmcli connection modify Wired\ connection\ 2 connection.id eno32
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
bridge-port1 a79f8dc9-b92a-4fe2-a117-c37d2ba23b89 802-3-ethernet ens33
bridge-port2 59ea460a-c581-4610-af3e-177ff4dc4092 802-3-ethernet ens37
bridge1 ab7569ea-6b37-4c77-b082-884bb402a93f bridge bridge1
eno32 82c8770b-2fc4-386b-b323-cf122f17a56e 802-3-ethernet ens38
virbr0 0a97df80-6d26-4d77-b5d5-064764eaa437 bridge virbr0
Wired connection 1 4de4ce91-1370-382b-86ca-c7a845da2835 802-3-ethernet --
home 1896dc07-66aa-41e4-bd85-60ee424a3971 802-3-ethernet --
[root@localhost ~]# nmcli general
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full enabled enabled enabled enabled