Linux 基础 之 ip 的设定
一、ip基础知识
1常用网络协议ipv4
进制32位-----10进制
172.25.0.10/255.255.255.0
172.25.0.10:ip地址
255.255.255.0:子网掩码 netmask 用来标实ip的网络位和主机位
子网掩码255位对应的ip位为网络位(网络区域)
子网掩码0对应的ip位为主机位(网络区域中的某台主机)
netmaks非0位所对应的ip地址数值为这个ip的网络位
netmaks0位所对应的ip地址数值为这个ip的主机位
网络位一致主机位不一致的两个ip是可以直接通信的
这样的两台主机叫做直连网络
2.设定ip
方法一
ifconfig device ip netmaks 255.255.255.0
ifconfig eth0 172.25.254.200 netmask 255.255.255.0
方法二
nm-connection-editor 图形的设定ip的方式
网络设备设定的ip的方式有两种
dhcp(DHCP) 动态获取,ip不固定
static 静态ip为操作者自行设定,ip固定
在此图形工具中选择dhcp为动态网络
在此图形工具中选择manual为静态网络,ip须自行手动输入
方法三
nmcli
nmcli device status eth0 显示eth0状态
nmcli device show eth0 显示eth0的详细信息
nmcli device disconnect etho 关闭eth0
nmcli device connect eth0 开启eth0
nmcli connection ...
nmcli connection down westos 关闭ip(再次输入ifconfig则不显示ip)
nmcli connection up westos 开启ip
nmcli connection show westos 显示ip
nmcli connection delete westos 删除ip
nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes 指定动态ip
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.100/24 指定静态ip
(先删除在操作)
dhcp(动态)---->static
nmcli connection modify westos ipv4.addresses 172.25.254.100/24
nmcli connection modify westos ipv4.method manual
systemctl restart network
static (静态)----> dhcp
nmcli connection modify westos ipv4.method auto
更改ip
nmcli connection modify westos ipv4.addresses 172.25.254.200/24
systemctl restart network
方法四
cd /etc/sysconfig/network-scripts/
vim ifcfg-xxxx
DEVICE=网卡
ONBOOT=yes
NETMAKS=子网掩码|PREFIX=子网掩码缩写
[NAME=连接名称 可加可不加]
动态网络(DHCP):
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
systemctl restart network
静态网络:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.229
NETMASK=255.255.255.0 | PREFIX=24
systemctl restart network
一个静态网卡设定多个ip
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.229
PREFIX0=24
IPADDR1=172.25.0.10
PREFIX1=24
systemctl restart network
ip addr show eth0
文件参数文档
/user/share/doc/initscripts-*/sysconfig.txt
3.gateway 网关####
2.网关
路由器上和自己处在同一个网段的那个ip
3.设定网关
systemctl stop NetwrokManager
vim /etc/sysconfig/network ##全局网关
GATEWAY=网关ip
vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件 ##网卡接口网关
GATEWAY=网关ip
systemctl restart netwrok
route -n ##查询网关
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 (网关)172.25.0.254 0.0.0.0 UG 0 0 0 eth0
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
####5.dns####
1.dns
dns是一台服务器
这太服务器提供了回答客户主机名和ip对应关系的功能
2.设定dns
vim /etc/resolv.conf
nameserver dns服务器ip
vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件
DNS1=dns服务器ip
3.本地解析文件
vim /etc/hosts
ip 主机名称
4.本地解析文件和dns读取的优先级调整
/etc/nsswitch.conf
38 #hosts: db files nisplus nis dns
39 hosts: files dns ##files代表本地解析文件,dns代表dns服务器,那个在前面那个优先
1常用网络协议ipv4
进制32位-----10进制
172.25.0.10/255.255.255.0
172.25.0.10:ip地址
255.255.255.0:子网掩码 netmask 用来标实ip的网络位和主机位
子网掩码255位对应的ip位为网络位(网络区域)
子网掩码0对应的ip位为主机位(网络区域中的某台主机)
netmaks非0位所对应的ip地址数值为这个ip的网络位
netmaks0位所对应的ip地址数值为这个ip的主机位
网络位一致主机位不一致的两个ip是可以直接通信的
这样的两台主机叫做直连网络
2.设定ip
方法一
ifconfig device ip netmaks 255.255.255.0
ifconfig eth0 172.25.254.200 netmask 255.255.255.0
这样的设定是临时的,在网络服务重启后失效
方法二
nm-connection-editor 图形的设定ip的方式
nmtui 当系统没有图形时可以用此命令开启网络设定的ui界面
网络设备设定的ip的方式有两种
dhcp(DHCP) 动态获取,ip不固定
static 静态ip为操作者自行设定,ip固定
在此图形工具中选择dhcp为动态网络
在此图形工具中选择manual为静态网络,ip须自行手动输入
方法三
nmcli
nmcli device status eth0 显示eth0状态
nmcli device show eth0 显示eth0的详细信息
nmcli device disconnect etho 关闭eth0
nmcli device connect eth0 开启eth0
nmcli connection ...
nmcli connection down westos 关闭ip(再次输入ifconfig则不显示ip)
nmcli connection up westos 开启ip
nmcli connection show westos 显示ip
nmcli connection delete westos 删除ip
nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes 指定动态ip
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.100/24 指定静态ip
(先删除在操作)
dhcp(动态)---->static
nmcli connection modify westos ipv4.addresses 172.25.254.100/24
nmcli connection modify westos ipv4.method manual
systemctl restart network
static (静态)----> dhcp
nmcli connection modify westos ipv4.method auto
systemctl restart network
更改ip
nmcli connection modify westos ipv4.addresses 172.25.254.200/24
systemctl restart network
方法四
cd /etc/sysconfig/network-scripts/
vim ifcfg-xxxx
DEVICE=网卡
ONBOOT=yes
NETMAKS=子网掩码|PREFIX=子网掩码缩写
[NAME=连接名称 可加可不加]
动态网络(DHCP):
cd /etc/sysconfig/network-scripts/
vim ifcfg-westosDEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
systemctl restart network
静态网络:
cd /etc/sysconfig/network-scripts/
vim ifcfg-westosDEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.229
NETMASK=255.255.255.0 | PREFIX=24
systemctl restart network
一个静态网卡设定多个ip
cd /etc/sysconfig/network-scripts/
vim ifcfg-westosDEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.229
PREFIX0=24
IPADDR1=172.25.0.10
PREFIX1=24
systemctl restart network
ip addr show eth0
文件参数文档
/user/share/doc/initscripts-*/sysconfig.txt
3.gateway 网关####
2.网关
路由器上和自己处在同一个网段的那个ip
3.设定网关
systemctl stop NetwrokManager
vim /etc/sysconfig/network ##全局网关
GATEWAY=网关ip
vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件 ##网卡接口网关
GATEWAY=网关ip
systemctl restart netwrok
route -n ##查询网关
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 (网关)172.25.0.254 0.0.0.0 UG 0 0 0 eth0
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
####5.dns####
1.dns
dns是一台服务器
这太服务器提供了回答客户主机名和ip对应关系的功能
2.设定dns
vim /etc/resolv.conf
nameserver dns服务器ip
vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件
DNS1=dns服务器ip
3.本地解析文件
vim /etc/hosts
ip 主机名称
4.本地解析文件和dns读取的优先级调整
/etc/nsswitch.conf
38 #hosts: db files nisplus nis dns
39 hosts: files dns ##files代表本地解析文件,dns代表dns服务器,那个在前面那个优先