在rhel7中,ifconfig需要自己安装才可以使用,我们可以查看自己是否已经安装:
[root@server ~]# rpm -qf /sbin/ifconfig
net-tools-2.0-0.17.20131004git.el7.x86_64
如果没有查询出来,使用yum进行安装:
[root@server ~]# yum whatprovides */ifconfig
Loaded plugins: langpacks
rhel7/filelists_db | 3.0 MB 00:00
net-tools-2.0-0.17.20131004git.el7.x86_64 : Basic networking tools
Repo : rhel7
Matched from:
Filename : /sbin/ifconfig # ficonfig命令包含在这个软件包中
[root@server ~]# yum install net-tools -y # 安装软件包
[root@server ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.5.10 netmask 255.255.255.0 broadcast 172.25.5.255
inet6 fe80::5054:ff:fe00:50a prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:05:0a txqueuelen 1000 (Ethernet)
RX packets 413 bytes 3144511 (2.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 308 bytes 32690 (31.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 52:54:00:d3:36:54 txqueuelen 1000 (Ethernet)
RX packets 41 bytes 5284 (5.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 52:54:00:e1:dc:0e txqueuelen 1000 (Ethernet)
RX packets 41 bytes 5284 (5.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 30 bytes 1948 (1.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30 bytes 1948 (1.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
这里有三块物理网卡eth0、eth1、eth2,lo为回环接口,代表自己本身。
ifconfig使用以下命令,这里设定的是临时IP,重启失效:
ifconfig | 查看或检测网络接口 |
---|---|
ifconfig device ip/24 | 设定网络ip |
ifconfig device down | 关闭网卡 |
ifconfig device up | 开启网卡 |
注意:device的名字是一个物理事实,看到什么网卡名字就用什么网卡名字
1)查看网络接口
lo回环接口:数据在主机的内部传输,安全高效,服务和为服务之间使用
2)查看固定的网卡网络配置
3)设定网络ip
[root@client ~]# ifconfig eth0 123.0.0.2/24 # 给eth0设定ip为123.0.0.2,子网掩码24:255.255.255.0
[root@client ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 123.0.0.2 netmask 255.255.255.0 broadcast 123.0.0.255
inet6 fe80::5054:ff:fe00:50b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:05:0b txqueuelen 1000 (Ethernet)
RX packets 203 bytes 19971 (19.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 174 bytes 23034 (22.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4)关闭网卡
查看命令是否安装:
[root@server ~]# rpm -qf /usr/sbin/ip
iproute-3.10.0-13.el7.x86_64
没有安装进行查找:
[root@server ~]# yum whatprovides */ip
Loaded plugins: langpacks
iproute-3.10.0-13.el7.x86_64 : Advanced IP routing and network device
: configuration tools
Repo : installed
Matched from:
Filename : /usr/sbin/ip
Filename : /sbin/ip
[root@server ~]# yum install iproute -y
设定的是临时IP,重启失效:
ip addr | 检测网络接口 |
---|---|
ip addr show | 检测 |
ip addr add ip/24 dev device | 设定网络ip |
ip addr del dev device ip/24 | 删除网络 ip |
ip addr flush eth0 | 刷新网络ip |
1)查看ip
2)添加IP:一个网卡可以添加多个IP
3)删除IP
此方法可用于图形界面,也可用于字符界面:
1)输入nmtui:
[root@client ~]# nmtui
1、查看所有网络连接
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
eno50338560 1f77d6f7-ad3a-4061-82d5-332ed0894034 802-3-ethernet --
eno16780032 44b5ae70-4c85-4475-b7ff-6b584b393739 802-3-ethernet eno16780032
eno33559296 1ccf4590-335a-441f-ba17-854a78d6b613 802-3-ethernet --
virbr0-nic 60c999b0-34ba-4dd6-85a3-f7a63cbc4350 generic virbr0-nic
virbr0 bffd0bf1-5503-4de3-afb0-4d0fe75ea007 bridge virbr0
2、查看活动的网络连接
[root@localhost ~]# nmcli connection show -active
NAME UUID TYPE DEVICE
eno16777984 329a55f6-56f8-4f58-89fe-d3221d353a8f 802-3-ethernet eno16777984
virbr0-nic 2479e6f3-7227-4199-8893-8a1ad815a10d generic virbr0-nic
virbr0 3705152b-654a-4740-b244-efbddd068741 bridge virbr0
3、查看指定链接的详细信息
[root@localhost ~]# nmcli connection show eno16777984
connection.id: eno16777984
connection.uuid: 329a55f6-56f8-4f58-89fe-d3221d353a8f
connection.interface-name: eno16777984
connection.type: 802-3-ethernet
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.timestamp: 1548145948
connection.read-only: no
省略部分输出
4、显示设备的连接状态
[root@localhost ~]# nmcli device status
DEVICE TYPE STATE CONNECTION
virbr0 bridge connected virbr0
eno16777984 ethernet connected eno16777984
virbr0-nic tap connected virbr0-nic
lo loopback unmanaged --
5、显示所有设备网络设备详情信息
[root@localhost ~]# nmcli device show
GENERAL.DEVICE: eno16777984
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:50:56:B0:77:F5
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eno16777984
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 172.16.6.16/16
IP4.GATEWAY: 172.16.1.1
IP4.DNS[1]: 114.114.114.114
IP6.ADDRESS[1]: fe80::250:56ff:feb0:77f5/64
IP6.GATEWAY:
省略部分输出
6、显示指定网络设备的详细信息
[root@localhost ~]# nmcli device show eno16777984
GENERAL.DEVICE: eno16777984
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:50:56:B0:77:F5
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eno16777984
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 172.16.6.16/16
IP4.GATEWAY: 172.16.1.1
IP4.DNS[1]: 114.114.114.114
IP6.ADDRESS[1]: fe80::250:56ff:feb0:77f5/64
IP6.GATEWAY:
1、启用网络连接
[root@localhost network-scripts]# nmcli connection up eno16780032
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5771)
2、停用网络连接-可以被自动激活
[root@6000m ~]# nmcli connection down ens37
成功取消激活连接 'ens37'(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/16)
3、禁用网卡放置被激活
[root@6000m ~]# nmcli device disconnect ens37
成功断开设备 'ens37'。
4、删除网络连接的配置文件
[root@6000m ~]# nmcli connection delete ens37
成功删除连接 'ens37'(6c6bf2e6-111b-3f09-80bb-c177263585e6)。
5、重新加载网络配置文件
[root@6000m ~]# nmcli connection reload
1、设置自动启动网卡
[root@6000m network-scripts]# nmcli connection modify ens37 connection.autoconnect yes
实际修改的是网卡配置文件ONBOOT=yes
2、设置IP地址获取方式是手动或者DHCP
手动设置IPv4地址,如果原本是DHCP获取地址,改手动后,如果后面不接IP地址,可能会报错
[root@localhost network-scripts]# nmcli connection modify ens37 ipv4.method manual ipv4.addresses 172.16.10.1/16
设置IP地址为DHCP
[root@localhost network-scripts]# nmcli connection modify ens37 ipv4.method auto
实际修改的是网卡配置文件BOOTPROTO,BOOTPROTO=none 表示手动;BOOTPROTO=dhcp 表示dhcp
3、修改IP地址
[root@localhost network-scripts]# nmcli connection modify ens37 ipv4.addresses 172.16.10.100/16
实际修改的是网卡配置文件:
IPADDR=172.16.10.100
PREFIX=16
4、修改网关
[root@localhost network-scripts]# nmcli connection modify ens37 ipv4.gateway 172.16.1.1
实际修改的是网卡配置文件:
GATEWAY=172.16.1.1
5、添加第二个IP地址
[root@6000m network-scripts]# nmcli connection modify ens37 +ipv4.addresses 172.16.10.10/16
实际修改的是网卡配置文件:
IPADDR1=172.16.10.10
PREFIX1=16
6、添加DNS
[root@6000m network-scripts]# nmcli connection modify ens37 ipv4.dns
114.114.114.114
实际修改的是网卡配置文件: DNS1=114.114.114.114
7、添加第二个DNS
[root@6000m network-scripts]# nmcli connection modify ens37 +ipv4.dns 8.8.8.8
实际修改的是网卡配置文件:
DNS2=8.8.8.8
8、删除第二个DNS
[root@6000m network-scripts]# nmcli connection modify ens37 -ipv4.dns 8.8.8.8
配置文件的目录:
[root@client ~]# cd /etc/sysconfig/network-scripts/ # 进入网络配置目录
[root@client network-scripts]# ls
ifcfg-eth0 # eth0配置文件
ifcfg-lo # 回环网卡配置文件
查看配置文件中的内容:
[root@client network-scripts]# vim ifcfg-eth0
DEVICE=eth0 # 使用的网卡名称
BOOTPROTO=none # 表示静态none或static,如果使用动态dhcp
ONBOOT=yes # 开机自动加载
TYPE=Ethernet
USERCTL=yes
IPV6INIT=no
PERSISTENT_DHCLIENT=1
IPADDR0=123.0.0.2 # 第一个ip
PREFIX0=24 # 第一个子网掩码
GATEWAY0=123.0.0.1 # 第一个网关
DNS1=114.114.114.114 # 第一个DNS
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME="System eth0" # 链接名称
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
注意:NETMASK后必须跟:255.255.255.0
PREFIX后必须跟:24