网络配置
rhel/Centos中常用的网络管理命令:
设置ip基本信息相关配置文件
5,6版本:system-config-network(setup)
配置文件位置:/etc/sysconfig/network-scripts/ifcfg-eth[0-n]
7版本:nmcil nmtui(图形界面)
配置文件位置:/etc/sysconfig/network-scripts/ifcfg-en*****
ifcfg-eth0,ifcfg-en******网络配置文件信息
TYPE=Ethernet #接口类型为以太网卡 BOOTPROTO=none #获取地址协议dhcp(自动获取),static(手动配置),none HWADDR=00:0C:29:75:A1:89 #网卡MAC UUID=be9d332f-ae79-42b6-8040-d2d431e4e195 #网卡标识 #DEVICE=eno16777736 #7系列的网卡名称 DEVICE=eth0 #5,6系列的网卡名 ONBOOT=yes #开机自动激活网卡,no不激活 IPADDR=10.76.249.91 GATEWAY=10.76.249.2 NETMASK=255.255.255.0 DNS=8.8.4.4 |
配置IP地址
临时添加IP地址,重启后失效
ip addr add 192.168.1.150/24 dev eth0 或者 # ifconfig eth0 192.168.1.150/24 up #重启后会失效,第一张网卡为eth0,第二张为eth1依此类推 # ifconfig eth0 1.1 Link encap:Ethernet HWaddr 00:0C:29:75:A1:89 inet addr:192.168.1.150 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe75:a189/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5682 errors:0 dropped:0 overruns:0 frame:0 TX packets:4136 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:412252 (402.5 KiB) TX bytes:4464715 (4.2 MiB) |
配置默认路由
# ip route add default via 192.168.1.1 #default via 192.168.1.1 devinterface |
网卡相关状态查看
1.查看IP
# ip addr show 1: lo: link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: link/ether 200:0c:29:e1:c5:8a brd ff:ff:ff:ff:ff:ff inet 310.76.249.91/24 brd 10.76.249.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fee1:c58a/64 scope link valid_lft forever preferred_lft forever 1:网卡状态已经启动 2:网卡的mac地址 3:网卡”eno16777736”地址 或者 # ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:75:A1:89 inet addr:192.168.1.150 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe75:a189/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6386 errors:0 dropped:0 overruns:0 frame:0 TX packets:4436 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:467110 (456.1 KiB) TX bytes:4502451 (4.2 MiB)
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:66 errors:0 dropped:0 overruns:0 frame:0 TX packets:66 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3482 (3.4 KiB) TX bytes:3482 (3.4 KiB) 或者 # nmcli dev show eth0 GENERAL.设备: eth0 GENERAL.类型: ethernet GENERAL.硬盘: 00:0C:29:E1:C5:8A GENERAL.MTU: 1500 GENERAL.状态: 100 (连接的) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.容器: 开 IP4.地址[1]: 10.76.249.91/24 IP4.网关: 10.76.249.2 IP6.地址[1]: fe80::20c:29ff:fee1:c58a/64 IP6.网关: |
2.查看收发包状态
# ip -s link show eth0 2: eth0: link/ether 00:0c:29:75:a1:89 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 477844 6528 0 0 0 0 TX: bytes packets errors dropped carrier collsns 4508951 4492 0 0 0 0 或者 # ifconfig –s eth0 Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 6459 0 0 0 4455 0 0 0 BMRU
|
3.显示显示主机默认路由
#ip route default via 10.76.249.2 dev eth0 proto static metric 100 10.76.249.0/24 dev eth0 proto kernel scope link src 10.76.249.91 metric 100 |
4.显示连接状态
常用到的选项
ss和netstat使用方法相似,但是ss效率要比较高,消耗性能比较少。
选项 |
功能 |
-l |
监听状态的连接 |
-n |
不解析服务名以数字显示 |
-t |
显示TCP连接 |
-u |
显示UDP连接 |
-a |
显示所有状态 |
-p |
显示以哪个进程发起的连接 |
常用的组合
-tan,-tanl, -tanlp, -uan
几种状态信息
State: LISTEN(监听),ESTAB(已建立的链接)
# ss -tna State Recv-Q Send-Q Local Address:Port 本地端口 Peer Address:Port 客户端端口 LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 ::1:6010 :::* LISTEN 0 128 127.0.0.1:6011 *:* LISTEN 0 128 ::1:6011 :::* LISTEN 0 128 127.0.0.1:6012 *:* LISTEN 0 128 ::1:6012 :::* LISTEN 0 128 127.0.0.1:6013 *:* LISTEN 0 128 ::1:6013 :::* ESTAB 0 0 192.168.1.106:22 192.168.1.103:63167 ESTAB 0 0 192.168.1.150:22 192.168.1.103:63753 ESTAB 0 52 192.168.1.150:22 192.168.1.103:54953 ESTAB 0 0 192.168.1.106:22 192.168.1.103:63134 |
Centos/Redhat 7系列回归传统网卡命名
修改网卡文件内容
# cat /etc/sysconfig/network-scripts/ifcfg- eno16777736 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes PEERDNS=yes PEERROUTES=yes NAME=eno16777736 UUID=be9d332f-ae79-42b6-8040-d2d431e4e195 #DEVICE=eno16777736 #注释掉原来的命名 DEVICE=eth0 #传统命名 ONBOOT=yes IPADDR=10.76.249.91 GATEWAY=10.76.249.2 NETMASK=255.255.255.0
|
重命名网卡文件
# mv /etc/sysconfig/network-scripts/ifcfg-eno16777736 /etc/sysconfig/network-scripts/ifcfg-eth0 |
编辑/etc/default/grub配置文件
# vim /etc/default/grub GRUB_TIMEOUT=5 GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" #GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet" #修改前 GRUB_CMDLINE_LINUX="net.ifnames=0 rhgb quiet" #修改后 GRUB_DISABLE_RECOVERY="true" |
为grub2生成其配置文件
# grub2-mkconfig -o /etc/grub2.cfg Generating grub configuration file ... Found linux p_w_picpath: /boot/vmlinuz-3.10.0-229.el7.x86_64 Found initrd p_w_picpath: /boot/initramfs-3.10.0-229.el7.x86_64.img Found linux p_w_picpath: /boot/vmlinuz-0-rescue-16de415bcbd643d886010c2fe954d9cb Found initrd p_w_picpath: /boot/initramfs-0-rescue-16de415bcbd643d886010c2fe954d9cb.img done
|
重启系统
验证结果
# ip addr show eth0 2: eth0: link/ether 00:0c:29:e1:c5:8a brd ff:ff:ff:ff:ff:ff inet 10.76.249.91/24 brd 10.76.249.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fee1:c58a/64 scope link valid_lft forever preferred_lft forever |
nmcli
nmcli:网络管理命令行接口:NetworkManagerCommand-Line Interface
nmtui:网络管理文本用户接口:NetworkManager Text-User Interface
RHEL7之后的网络配置命令
[root@localhost conf.d]# nmcli --help Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
OPTIONS -t[erse] terse output -p[retty] pretty output -m[ode] tabular|multiline output mode -f[ields] -e[scape] yes|no escape columns separators in values -n[ocheck] don't check nmcli and NetworkManager versions -a[sk] ask for missing parameters -w[ait] -v[ersion] show program version -h[elp] print this help
OBJECT g[eneral] NetworkManager's general status and operations n[etworking] overall networking control r[adio] NetworkManager radio switches c[onnection] NetworkManager's connections 简写为c d[evice] devices managed by NetworkManager 简写为d a[gent] NetworkManager secret agent or polkit agent |
重新加载网络配置文件
[root@localhost ~]# nmcli c reload
[root@localhost ~]# nmcli c load/etc/sysconfig/network-scripts/ifcfg-eth0
配置IP地址
[root@localhost ~]# nmcli con mod eno16777736 ipv4.addresses '192.168.1.119/24' 网卡名使用查看# nmcli con show |
配置DNS
[root@localhost ~]# nmcli con mod eno16777736 ipv4.dns '8.8.4.4' |
配置网关
[root@localhost ~]# nmcli con mod eno16777736 ipv4.gateway '192.168.1.1' |
停启用网卡
[root@localhost ~]# nmcli dev dis eth0 dis 为 disconnect [root@localhost ~]# nmcli con up eth0
|
查看相关信息
[root@localhost ~]# nmcli device status DEVICE TYPE STATE CONNECTION eth0 ethernet connected eno16777736 lo loopback unmanaged -- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
nmtui 图形配置界面比较简洁
[root@localhost ~]# nmtui