一、 设定IP的四种方法
(1)nm-connection-editor(有图形)
将IPv4设置为Manual
点击add添加ip及子网掩码,并save保存。
效果如下
(2)nmtui(有无图形均可)
选择编辑第一个点击ok
将原有的删除,并点击add
Device输入eth0,并添加地址1.1.1.173/24,点击ok
效果如下
(3)网络配置文件修改ip
【单网卡配单个ip】
在这里面/etc/sysconfig/network-scripts/ifcfg-eth0编辑文件
DEVICE=eth0 #网卡设备名
BOOTPROTO=none|static|dhcp (其中none和static一样,代表静态.dhcp代表动态)
PERFIX=24和NETMASK=255.255.255.0效果一样
ONBOOT=yes #开机自动读取该网卡,若设置为no则网卡信息ip显示不出来,需要通过命令【ifup eth0】
每次配置完,都需要重启网络
【单网卡配多个ip】
ip addr show eth0 #显示双网卡信息所用命令
配置完依旧重启网络
(4)命令行修改ip–nmcli
使用nmcli命令时NetworkManager必须开启
nmcli device connect eth0 | 启用eth0网卡 |
---|---|
nmcli device disconnect eth0 | 关闭eth0网卡 |
nmcli device show eth0 | 查看网卡信息 |
nmcli device status eth0 | 查看网卡服务接口信息 |
效果图:
(1)nmcli device #显示所有网卡信息
(2)nmcli device show eth0 #只显示eth0这块网卡信息(详细)
(3)nmcli device disconnect eth0 #关闭eth0网卡
nmcli connection show | 显示有connection名的网卡信息 |
---|---|
nmcli connection show “westos” | 显示connection名为westos的网卡信息 |
nmcli connection up “westos“ | 开启connection名为westos的网卡 |
nmcli connection down “westos“ | 关闭connection名为westos的网卡 |
nmcli connection delete “westos“ | 删除connection名为westos的网卡 |
本次实验connection name为System eth0
(1)显示有connection名的网卡信息
(2)显示connection名为System eth0的网卡信息
(3)nmcli connection down westos # 关闭connection名为System eth0的网卡
(4)nmcli connection up westos # 开启connection名为System eth0的网卡
(5)nmcli connection delete 删除connection名为westos的网卡
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.101/24 | 添加网卡信息 |
---|---|
nmcli connection modity westos ipv4.method auto | 设置网卡ip获取方式为自动 |
nmcli connection modity westos ipv4.method manual | 设置网卡ip获取方式为手动 |
nmcli connection modify westos ipv4.addresses 172.25.254.200/24 | 修改ip |
(1)nmcli connection add type ethernet con-name westos ifname eth0 ip4 1.1.1.6/24 #添加网卡信息
(2)nmcli connection modity westos ipv4.method auto #设置网卡ip获取方式为自动
(3)nmcli connection modity westos ipv4.method manual #设置网卡ip获取方式为手动
(4)nmcli connection modity westos ipv4.addresses 1.1.1.7/24|修改ip
修改完nm-connection-editor显示的已经是1.1.1.7,但是ifconfig仍然是1.1.1.6,此时需要重启网络
二、linux下的网络配置
1.什么是IP ADDRESS
internet protocol ADDRESS #网络进程地址
internet protocol version 4 ipv4
ip地址是由32位0和1组成的
11111110.11111110.11111110.11111110.=254.254.254
2.子网掩码
作用:用来划分网络区域
子网掩码上的非0位代表这个ip的网络位(表示网络区域),0位代表这个ip的主机位(网络区域内的某台主机)。
1.查看网络命令
ping | 检测网络是否连通 |
---|---|
ping -c 1 | ping 1次 |
ping -w 1 | 等待1秒 |
ping -c1 -w1 ip | ping 1次并等待1秒 |
ifconfig | 查看网络接口 |
---|---|
ifconfig device ip/24 | 设定 |
ifconfig device down | 关闭 |
ifcon fig device up | 开启 |
2.管理网络配置文件
网络配置目录:/etc/sysconfig/network-scripts/
网络配置文件命名规则:ifcfg-xxxx
3.dns
vim /etc/hosts #本地解析文件
ip | 域名 |
---|---|
220.181.111.118 | www.baid.com |
vim /etc/resolv.conf #dns的指向文件
nameserver 114.114.114.114 #当需要某个域名的IP地址时去问114.114.114.114要