1.网络链接和网路设备
网络链接:系统使用设备的方法,是一个软件程序的名字,名字可以自定义
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
eth0 d2975348-e208-38df-9b76-2314670fb475 ethernet eth0
lo 00b2d78b-f37d-49cf-9acb-9d04b7949c0c loopback lo
网络设备:硬件真实存在的设备并且被系统识别的名称,名称是什么就用什么,不可自定义
[root@localhost ~]# nmcli device show | grep DEVICE
GENERAL.DEVICE: eth0
GENERAL.DEVICE: lo
GENERAL.DEVICE: eth1
2.配置IP地址
临时生效
#方法1
[root@localhost ~]# ifconfig eth1 172.25.254.100 netmask 255.255.255.0
#方法2
[root@localhost ~]# ip a a 172.25.254.100/24 dev eth1
查看
[root@localhost ~]# ip a
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:58:35:f8 brd ff:ff:ff:ff:ff:ff
altname enp11s0
altname ens192
inet 172.25.254.100/24 scope global eth1
valid_lft forever preferred_lft forever
注意:临时设定通常在系统重新启动后失效,一般用于临时使用
永久生效
通过命令设定
[root@localhost ~]# 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
[root@localhost ~]# nmcli connection modify lee ipv4.addresses 172.25.254.100/24
[root@localhost ~]# nmcli connection reload
[root@localhost ~]# nmcli connection up lee
通过文件设定
[root@localhost ~]# cd /etc/NetworkManager/system-connections/
[root@localhost system-connections]# vim eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
method=manual
address1=172.25.254.100/24
address2=172.25.254.200/24
[root@localhost system-connections]# chmod 600 eth1.nmconnection #给文件权限,不给会出现未知链接错误
[root@localhost system-connections]# nmcli connection reload
[root@localhost system-connections]# nmcli connection up eth1
[root@localhost system-connections]# ip a s eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:58:35:f8 brd ff:ff:ff:ff:ff:ff
altname enp11s0
altname ens192
inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet 172.25.254.200/24 brd 172.25.254.255 scope global secondary noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::d1d5:cba1:e13e:b053/64 scope link noprefixroute
valid_lft forever preferred_lft forever
注意:IP设定完成后通常不能直接访问外网,因为我们设定的IP不是合法IP,即IP不是公网IP,如果需要访问公网那么需要借助能够链接公网的路由
3.设定网关
通过命令方式
[root@localhost system-connections]# nmcli connection modify eth1 ipv4.gateway 172.25.254.2
[root@localhost system-connections]# nmcli connection reload
[root@localhost system-connections]# nmcli connection up eth1
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/9)
[root@localhost system-connections]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.2 0.0.0.0 UG 100 0 0 eth0
0.0.0.0 172.25.254.2 0.0.0.0 UG 101 0 0 eth1
172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
172.25.254.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1
172.25.254.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1
通过文件设定网关
[root@localhost ~]# cd /etc/NetworkManager/system-connections/
[root@localhost system-connections]# vim eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=172.25.254.100/24
address2=172.25.254.200/24
gateway=172.25.254.2
method=manual
[root@localhost system-connections]# chmod 600 eth1.nmconnection
[root@localhost system-connections]# nmcli connection reload
[root@localhost system-connections]# nmcli connection up eth1
4.域名解析
人类对数字不敏感,对于
拼音,英文,或者中文敏感,但是ip是数字,使用者访问时会记不住
为了迎合客户好记的需求,通常我们访问一个站点时都使用这个站点的域名(www.baidu.com)
但是域名不是IP,不能直接使用域名通信,所以要把域名转换成IP来通信,把域名转换成IP的过程叫做域名及解析
ip叫做这个域名的A记录,也叫做正向解析
本地域名解析
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.100 www.baidu.com
[root@localhost ~]# ping www.baidu.com
PING www.baidu.com (172.25.254.100) 56(84) 比特的数据。
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=1 ttl=64 时间=0.078 毫秒
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=2 ttl=64 时间=0.056 毫秒
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=3 ttl=64 时间=0.076 毫秒
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=4 ttl=64 时间=0.074 毫秒
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=5 ttl=64 时间=0.072 毫秒
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=6 ttl=64 时间=0.072 毫秒
64 比特,来自 www.baidu.com (172.25.254.100): icmp_seq=7 ttl=64 时间=0.069 毫秒
dns
dns服务器的IP不是最终你要的问题的答案,但是访问这个IP可以得到你想要访问的域名IP
[root@localhost ~]# vim /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 114.114.114.114
[root@localhost ~]# 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.19
www.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
[root@localhost ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=172.25.254.100/24
address2=172.25.254.200/24
gateway=172.25.254.2
dns=8.8.8.8;
method=manual
[root@localhost ~]# nmcli connection reload
[root@localhost ~]# nmcli connection up eth1
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/10)
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 172.25.254.2
nameserver 8.8.8.8
5.编写网络设定脚本
要求:
1.执行vmset.sh eth1 172.25.254.100
2.执行vmset.sh eth1 172.25.254.100 noroute
#eth1 有没有被其他链接使用
#eth1 存不存
#设定的ip是否已经被占用
#检测脚本后需要的执行素材是否给够
root@localhost ~]# cat /bin/vmset.sh
#!/bin/bash
[ "$#" -lt "3" ] && {
echo "error: Please input device deviceip and hostname following $0"
exit 1
}
ifconfig $1 &> /dev/null || {
echo $1 is not exist
exit 2
}
CONNECTION=`nmcli connection show | awk "/$1/"'{print $1}'`
[ -n "$CONNECTION" ] && {
nmcli connection delete $CONNECTION
}
sleep 2
ping -c1 -w1 $2 &> /dev/null && {
echo $2 is in used !!!
exit 3
}
hostnamectl hostname $3
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
$2 $3
EOF
if [ "$4" != "noroute" ]
then
nmcli 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.2
nmcli connection reload
nmcli connection up $1
else
nmcli connection add type ethernet ifname $1 con-name $1 ipv4.method manual ipv4.addresses $2/24
nmcli connection reload
nmcli connection up $1
fi
ip addr show $1
hostname
[root@localhost ~]# chmod +x /bin/vmset.sh
将其母盘关机,克隆一个虚拟机,用vmset.sh eth0 172.0.0.0. hostname命令测试脚本是否成功