Linux system IP、网关及DNS

                                Unit 11.管理网络

 

1.ip基础知识

1)ipv4

2)进制32位-----10进制


172.25.0.10/255.255.255.0
172.25.0.10:ip地址
255.255.255.0:子网掩码
子网掩码255位对应的ip位为网络位
子网掩码0对应的ip位为主机位
 

2.配置ip

<<图形化>>

1)图形界面

nm-connection-editor

2)文本化图形

nmtui


<<命令>>
ifconfig 网卡 ip netmask ##临时设定
 

nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes #添加dhcp网络

nmcli connection add type ethernet con-name westos ifname eth0 ip4 ip/24 #添加静态网络

nmcli connection delete westos #删除westos链接

nmcli connection show #显示所有网络链接

nmcli connection down westos #关闭指定链接
nmcli connection up westos #开启指定链接
nmcli connection modify "westos" ipv4.addresses newip/24 #改变wetos的ip
nmcli connection modify "westos" ipv4.method <auto|manual> #改变westos的工作方式为动态或者静态

nmcli device connect eth0 #开启设备

nmcli device disconnect eth0 #关闭设备

nmcli device show #显示设备信息

nmcli device status #显示设备状态




<<文件>>
dhcp ##动态获取
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 ##接口使用设备
BOOTPROTO=dhcp ##网卡工作模式
ONBOOT=yes ##网络服务开启时自动激活
NAME=eth0 ##网络接口名称

:wq

systemctl restart network



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 ##子网掩码



 

3.gateway 网关

 

1)路由器

主要功能是用来作nat的
dnat 目的地地址转换
snat 源地址转换
 

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

 

 

#########主机充当路由--虚拟机联网#######

主机联网后“ping www.baidu.com"证明网络畅通且查看百度ip

在虚拟机 /etc/sysconfig/network中添加网关ip,其为主机ip

重启网络后ping百度ip检查是否成功设置网关且是否与主机连接

在主机网卡配置文件中添加网关并重启网络,在/etc/resolv.conf文件中查看相关网络配置

虚拟机在/etc/resolv.conf文件中添加和主机相同的网络配置并重启网络

主机执行火墙相关命令

虚拟机作最后网络操作

执行完毕,可检验虚拟机是否连接网络

 



 

4.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服务器,那个在前面那个优先

 

5)dhcp服务的配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java代码来获取Linux中的IP地址、网关DNS信息。以下是示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class NetworkInfo { public static void main(String[] args) { try { String ip = getIPAddress(); String gateway = getGateway(); String[] dns = getDNS(); System.out.println("IP Address: " + ip); System.out.println("Gateway: " + gateway); System.out.print("DNS: "); for (String server : dns) { System.out.print(server + " "); } System.out.println(); } catch (IOException e) { e.printStackTrace(); } } public static String getIPAddress() throws IOException { Process process = Runtime.getRuntime().exec("ifconfig | grep inet | awk '{print $2}' | cut -d':' -f2"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = reader.readLine(); return line; } public static String getGateway() throws IOException { Process process = Runtime.getRuntime().exec("ip route | grep default | awk '{print $3}'"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = reader.readLine(); return line; } public static String[] getDNS() throws IOException { Process process = Runtime.getRuntime().exec("cat /etc/resolv.conf | grep nameserver | awk '{print $2}'"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; StringBuilder output = new StringBuilder(); while ((line = reader.readLine()) != null) { output.append(line); output.append(" "); } return output.toString().trim().split(" "); } } ``` 该代码使用 `Runtime.exec()` 方法执行系统命令,并使用 `BufferedReader` 读取命令输出。 `getIPAddress()` 方法获取IP地址, `getGateway()` 方法获取网关, `getDNS()` 方法获取DNS服务器的IP地址。注意,这些命令在Linux系统上执行,所以该代码只能在Linux系统上运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值