Linux中的网络设定
1.网络链接和网路设备网络链接系统使用设备的方法是一个软件程序的名字名字可以自定义[rootlocalhost ~]# nmcli connection showNAME UUID TYPE DEVICEeth0 d2975348-e208-38df-9b76-2314670fb475 ethernet eth0lo 00b2d78b-f37d-49cf-9acb-9d04b7949c0c loopback lo网络设备硬件真实存在的设备并且被系统识别的名称名称是什么就用什么不可自定义[rootlocalhost ~]# nmcli device show | grep DEVICEGENERAL.DEVICE: eth0GENERAL.DEVICE: loGENERAL.DEVICE: eth12.配置IP地址临时生效#方法1[rootlocalhost ~]# ifconfig eth1 172.25.254.100 netmask 255.255.255.0#方法2[rootlocalhost ~]# ip a a 172.25.254.100/24 dev eth1查看[rootlocalhost ~]# ip a3: eth1: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:58:35:f8 brd ff:ff:ff:ff:ff:ffaltname enp11s0altname ens192inet 172.25.254.100/24 scope global eth1valid_lft forever preferred_lft forever注意临时设定通常在系统重新启动后失效一般用于临时使用永久生效通过命令设定[rootlocalhost ~]# nmcli connection add type ethernet ifname eth1 con-name lee ipv4.method manual ipv4.addresses 172.25.254.200/24#ifname 设备名称不能随意指定有什么用什么可以用nmcli device show 查看#con-name 链接名称可以自定义但是确保系统中没有使用这个名字的链接更改IP[rootlocalhost ~]# nmcli connection modify lee ipv4.addresses 172.25.254.100/24[rootlocalhost ~]# nmcli connection reload[rootlocalhost ~]# nmcli connection up lee通过文件设定[rootlocalhost ~]# cd /etc/NetworkManager/system-connections/[rootlocalhost system-connections]# vim eth1.nmconnection[connection]ideth1typeethernetinterface-nameeth1[ipv4]methodmanualaddress1172.25.254.100/24address2172.25.254.200/24[rootlocalhost system-connections]# chmod 600 eth1.nmconnection #给文件权限不给会出现未知链接错误[rootlocalhost system-connections]# nmcli connection reload[rootlocalhost system-connections]# nmcli connection up eth1[rootlocalhost system-connections]# ip a s eth13: eth1: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:58:35:f8 brd ff:ff:ff:ff:ff:ffaltname enp11s0altname ens192inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute eth1valid_lft forever preferred_lft foreverinet 172.25.254.200/24 brd 172.25.254.255 scope global secondary noprefixroute eth1valid_lft forever preferred_lft foreverinet6 fe80::d1d5:cba1:e13e:b053/64 scope link noprefixroutevalid_lft forever preferred_lft forever注意IP设定完成后通常不能直接访问外网因为我们设定的IP不是合法IP即IP不是公网IP如果需要访问公网那么需要借助能够链接公网的路由3.设定网关通过命令方式[rootlocalhost system-connections]# nmcli connection modify eth1 ipv4.gateway 172.25.254.2[rootlocalhost system-connections]# nmcli connection reload[rootlocalhost system-connections]# nmcli connection up eth1连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/9[rootlocalhost system-connections]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 172.25.254.2 0.0.0.0 UG 100 0 0 eth00.0.0.0 172.25.254.2 0.0.0.0 UG 101 0 0 eth1172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0172.25.254.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1172.25.254.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1通过文件设定网关[rootlocalhost ~]# cd /etc/NetworkManager/system-connections/[rootlocalhost system-connections]# vim eth1.nmconnection[connection]ideth1typeethernetinterface-nameeth1[ipv4]address1172.25.254.100/24address2172.25.254.200/24gateway172.25.254.2methodmanual[rootlocalhost system-connections]# chmod 600 eth1.nmconnection[rootlocalhost system-connections]# nmcli connection reload[rootlocalhost system-connections]# nmcli connection up eth14.域名解析人类对数字不敏感对于拼音英文或者中文敏感但是ip是数字使用者访问时会记不住为了迎合客户好记的需求通常我们访问一个站点时都使用这个站点的域名www.baidu.com但是域名不是IP不能直接使用域名通信所以要把域名转换成IP来通信把域名转换成IP的过程叫做域名及解析ip叫做这个域名的A记录也叫做正向解析本地域名解析[rootlocalhost ~]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.25.254.100 www.baidu.com[rootlocalhost ~]# ping www.baidu.comPING www.baidu.com (172.25.254.100) 56(84) 比特的数据。64 比特来自 www.baidu.com (172.25.254.100): icmp_seq1 ttl64 时间0.078 毫秒64 比特来自 www.baidu.com (172.25.254.100): icmp_seq2 ttl64 时间0.056 毫秒64 比特来自 www.baidu.com (172.25.254.100): icmp_seq3 ttl64 时间0.076 毫秒64 比特来自 www.baidu.com (172.25.254.100): icmp_seq4 ttl64 时间0.074 毫秒64 比特来自 www.baidu.com (172.25.254.100): icmp_seq5 ttl64 时间0.072 毫秒64 比特来自 www.baidu.com (172.25.254.100): icmp_seq6 ttl64 时间0.072 毫秒64 比特来自 www.baidu.com (172.25.254.100): icmp_seq7 ttl64 时间0.069 毫秒dnsdns服务器的IP不是最终你要的问题的答案但是访问这个IP可以得到你想要访问的域名IP[rootlocalhost ~]# vim /etc/resolv.conf# Generated by NetworkManagersearch localdomainnameserver 114.114.114.114[rootlocalhost ~]# dig www.qq.com; DiG 9.16.23-RH www.qq.com;; global options: cmd;; Got answer:;; -HEADER- opcode: QUERY, status: NOERROR, id: 48887;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 1232;; QUESTION SECTION:;www.qq.com. IN A;; ANSWER SECTION:www.qq.com. 45 IN A 183.194.238.19www.qq.com. 45 IN A 183.194.238.117;; Query time: 69 msec;; SERVER: 114.114.114.114#53(114.114.114.114);; WHEN: Wed Jul 15 16:30:57 CST 2026;; MSG SIZE rcvd: 71#方法2[rootlocalhost ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection[connection]ideth1typeethernetinterface-nameeth1[ipv4]address1172.25.254.100/24address2172.25.254.200/24gateway172.25.254.2dns8.8.8.8;methodmanual[rootlocalhost ~]# nmcli connection reload[rootlocalhost ~]# nmcli connection up eth1连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/10[rootlocalhost ~]# cat /etc/resolv.conf# Generated by NetworkManagersearch localdomainnameserver 172.25.254.2nameserver 8.8.8.85.编写网络设定脚本要求1.执行vmset.sh eth1 172.25.254.1002.执行vmset.sh eth1 172.25.254.100 noroute#eth1 有没有被其他链接使用#eth1 存不存#设定的ip是否已经被占用#检测脚本后需要的执行素材是否给够rootlocalhost ~]# cat /bin/vmset.sh#!/bin/bash[ $# -lt 3 ] {echo error: Please input device deviceip and hostname following $0exit 1}ifconfig $1 /dev/null || {echo $1 is not existexit 2}CONNECTIONnmcli connection show | awk /$1/{print $1}[ -n $CONNECTION ] {nmcli connection delete $CONNECTION}sleep 2ping -c1 -w1 $2 /dev/null {echo $2 is in used !!!exit 3}hostnamectl hostname $3cat /etc/hosts EOF127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6$2 $3EOFif [ $4 ! noroute ]thennmcli connection add type ethernet ifname $1 con-name $1 ipv4.method manual ipv4.addresses $2/24 ipv4.dns 8.8.8.8 ipv4.gateway 172.25.254.2nmcli connection reloadnmcli connection up $1elsenmcli connection add type ethernet ifname $1 con-name $1 ipv4.method manual ipv4.addresses $2/24nmcli connection reloadnmcli connection up $1fiip addr show $1hostname[rootlocalhost ~]# chmod x /bin/vmset.sh将其母盘关机克隆一个虚拟机用vmset.sh eth0 172.0.0.0. hostname命令测试脚本是否成功