linux下网络管理及设定IP
实验前准备,设置网关及查询网关
############################gateway 网关################################################
(1)路由器
主要功能是用来作nat的,dnat目的地地址转换,snat源地址转换 。
(2)网关
路由器上和自己处在同一个网段的那个ip
route -n 查询网关
添加的网络,网关与真机ip一致
在确定将自己的电脑设置为路由之后,想看看虚拟机都可以上网时,可以在虚拟机环境ping其他IP。例如百度等等,如果能ping通,说明联网成功,也可以在firefox里面测试一下。
以上操作只能用地址访问,如果需要名称访问时就要设定dns
方式:
vim /etc/resolv.conf
nameserver dns服务器ip
要想通过网络名称访问服务器,需真机和虚拟机同时打开 vim /etc/resolv.conf
################1.网络设定######################################
1.查看网卡:ip addr show
查看ip: ifconfig eth0
2.常用网络协议ipv4
2进制 32位 ----- 10进制
172.25.254.06/255.255.255.0
172.25.254.06 #ip地址
255.255.255.0 #子网掩码
网络位一致的主机不一致的两个ip是可以直接通信的,这样的两台主机叫做直连网络。
eg:
11111110.11111110.11111110.11111110 = 254.254.254.254
注意:
netmask:
用来标实Ip的网络位和主机位,网络位表示网络区域,主机位表示网络区域中的某提阿主机
netmak 非零位所对应的ip地址数值为这个ip的网络位
netmask 零位所对应的ip地址数值为这两个ip的主机位
##########################2.设定Ip###############################################
方法一:
ifconfig device ip netmask 255.255.255.0 //但这样的设定是临时的,在网络重启后失效:
eg: ifconfig eth0 172.25.254.78 netmask 255.255.255.0
方法二:
nmtui //当系统没有图形时可以用此命令开启
<<命令>>
ifconfig 网卡 ip netmask 临时设定
网络设备设定ip的方式有两种
dhcp 动态获取,ip不固定
static 静态ip为操作者自行设定,ip固定
static|none##静态网络
vim /etc/sysconfig/network-scripts/ifcfg-eth0
编辑内容如下:
DEVICE=eth0 ##设备名
BOOTPROTO=static|none ##设备工作方式
ONBOOT=yes ##开启网络服务**设备
NAME=eth0 ##网络接口名称
IPADDR=172.25.0.100 ##IP
NETMASK=255.255.255.0 | PREFIX=24 ##子网掩码
动态获取IP:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 ##接口使用设备
BOOTPROTO=dhcp ##网卡工作模式
ONBOOT=yes ##网络服务开启时自动**
NAME=eth0 ##网络接口名称
:wq
systemctl restart network
方法三:
nmcli device status eth0 //查看eth0的状态
nmcli device show eth0 //显示eth0的详细信息
nmcli device disconnect eth0 //关闭eth0,此时就查看不到ip,也不能改ip
nmcli device connect eth0 //开启eth0
****用命令设定ip地址******* (nmcli connection.......)
nmcli connection down westos //关闭westos的ip
nmcli connection up westos //打开westos的ip
nmcli connection show westos //显示westos的详细信息
nmcli connection delete westos //删除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.138/24 //设定静态ip
****已经有链接的前提下,更改链接****
dhcp(动态)---->static(静态)
nmcli connection modify "name" ipv4.addresses newip/24 -----------> 改变name的ip
nmcli connection modify "name" ipv4.method <auto|manual>
----------->改变name的工作方式为动态或者静态
eg:nmcli connection modify westos ipv4.addresses 172.25.254.138/24
nmcli connection modify westos ipv4.method manual
systemctl restart network
sattic(静态)--->dhcp(动态)
nmcli connection modify westos ipv4.method auto
nmcli connection modify 网卡名字(westos) ipv4.addresses 172.25.254.78/24
systemctl restart network
方法四:
前提:
cd /etc/sysconfig/network-scripts/ //必须在此目录下操作,
*****STATIC*******
vim ifcfg-xxxx
DEVICE=网卡
ONBOOT=yes
BOOTPROTO=dhcp/(none/static)
IPADDR=ip
NETWORK=子网掩码(255.255.255.0)/PREFIX=子网掩码缩写(24)
NAME=链接名称 (可加可不加)
*****DHCP*******
vim ifcfg-xxxx
DEVICE=网卡
ONBOOT=yes
BOOTPROTO=dhcp/(none/static)
systemctl restart network
具体操作(静态网络):
cd /etc/sysconfig/network-scripts/
vim ifcfg-westos
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.138
NETWORK=255.255.255.0/PREFIX=24
NAME=westos
systemctl restart network
**************************
**一个静态网卡设定多个ip**
**************************
cd /etc/sysconfig/network-scripts/
vim ifcfg-westos
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.138
PREFIX0=24
IPADDR1=172.25.254.138
PREFIX1=24
NAME=westos
systemctl restart network
**注**
此时查看ip必须用 ip addr show eth0