文件路径:/etc/sysconfig/network-scripts/ifcfg-网卡名
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet(网卡类型) PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static(设置网络接口IP地址的配置方式) DEFROUTE=yes(是否设置默认路由,若为yes则可以在该文件通过IPADDR和NETMASK两个参数来设置网关) IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=41c7bb50-9989-42f5-8656-08e912d3340a DEVICE=ens33(网络接口名) ONBOOT=yes(设置网络接口是否在系统启动时激活)(1)ifconfig查看所有网卡信息
[root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.60.10 netmask 255.255.255.0 broadcast 192.168.60.255 inet6 fe80::46a1:7cc7:c89e:283a prefixlen 64 scopeid 0x20<link> ether 00:0c:29:79:c5:44 txqueuelen 1000 (Ethernet) RX packets 28 bytes 3613 (3.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 45 bytes 5665 (5.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0(2)ip add 查看网卡的IP地址,可看状态(ip link show 网卡名)
[root@localhost ~]# ip link show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:79:c5:44 brd ff:ff:ff:ff:ff:ff把虚拟机暴露在局域网环境下,可以直接被访问,但不能访问外网(如www.baidu.com)
把各个虚拟机包括宿主机之间构建成一个同一网段局域网,无法访问其他网段。
那虚拟机连接到类似三层交换机的虚拟网卡上,不仅可以局域网之内通信,而且还可以外部通信互联网。
一般用于网络调试 (1)物理网卡 命令格式: ifconfig 网卡名 IP地址 /子网掩码位数 ifconfig 网卡名 IP地址 netmask 子网掩码
[root@localhost ~]# ifconfig ens33 192.168.60.88/24 [root@localhost ~]# ifconfig ens33 192.168.60.87 netmask 255.255.255.0(2)逻辑网卡子接口 命令格式: ifconfig 网卡名:子接口(0/1/2/3…)IP地址 /子网掩码位数 ifconfig 网卡名:子接口(0/1/2/3…) IP地址 netmask 子网掩码
[root@localhost ~]# ifconfig ens33:0 192.168.60.86/24 [root@localhost ~]# ifconfig ens33:0 192.168.60.85 netmask 255.255.255.0启用网卡:ifup 网卡名 禁用网卡:ifdown 网卡名
[root@localhost ~]#ifup ens33 [root@localhost ~]#ifdown ens33修改配置文件
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=41c7bb50-9989-42f5-8656-08e912d3340a DEVICE=ens33 ONBOOT=yes IPADDR=192.168.60.88 GATEWAY=192.168.60.2 NETMASK=255.255.255.0文件路径:/etc/sysconfig/network
[root@localhost ~]# cat /etc/sysconfig/network # Created by anaconda命令格式:hostnamectl set-hostname 主机名 激活方式:su两次切换
[root@localhost ~]# hostnamectl set-hostname zhangqing [root@localhost ~]# su zhangsan su: user zhangsan does not exist [root@localhost ~]# useradd zhangsan [root@localhost ~]# su zhangsan [zhangsan@zhangqing root]$ su root 密码: [root@zhangqing ~]#命令格式:hostname 主机名 激活方式:bash
[root@zhangqing ~]# hostname localhost [root@zhangqing ~]# bash [root@localhost ~]#显示主机名:route(默认)
[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0显示IP地址:route -n
[root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.60.2 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.60.2 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0添加:route add -net 路由网段/几位子网掩码 gw 下一跳地址(一般是本机网关)
[root@localhost ~]# route add -net 192.168.70.0/24 gw 192.168.60.2 [root@localhost ~]# route add -net 192.168.80.0 netmask 255.255.255.0 gw 192.168.60.2 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.70.0 gateway 255.255.255.0 UG 0 0 0 ens33 192.168.80.0 gateway 255.255.255.0 UG 0 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0删除:route del 路由网段/掩码
[root@localhost ~]# route del -net 192.168.70.0/24 [root@localhost ~]# route del -net 192.168.80.0/24 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0添加:route add default gw 下一跳地址
[root@localhost ~]# route add default gw 192.168.60.2 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.60.2 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.60.2 0.0.0.0 UG 100 0 0 ens33删除:route del default gw 下一跳地址
[root@localhost ~]# route del default gw 192.168.60.2 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.60.2 0.0.0.0 UG 100 0 0 ens33