准备一台linux服务器,要求服务器能够上网
[root@lb-1 ~]# cat /etc/centos-release CentOS Linux release 7.9.2009 (Core) [root@lb-1 ~]# [root@lb-1 ~]# ping www.baidu.com -c 2 PING www.baidu.com (14.215.177.38) 56(84) bytes of data. 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=16.0 ms 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=15.5 ms --- www.baidu.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 15.512/15.757/16.003/0.275 ms
步骤:
1.安装dhcp相关的软件包
[root@lb-1 ~]# yum install dhcp -y
2.修改配置文件,配置dhcp服务器
[root@lb-1 ~]# cd /etc/dhcp/ [root@lb-1 dhcp]# ls dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
dhcpd.conf 是我们的主要的配置文件-->ipv4
dhcpd6.conf 是ipv6的配置文件
拷贝样例文件到/etc/dhcp目录
[root@lb-1 dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y [root@lb-1 dhcp]# vim /etc/dhcp/dhcpd.conf 编辑配置文件 # have to hack syslog.conf to complete the redirection). log-facility local7; #指定日志类型 # A slightly different configuration for an internal subnet. subnet 192.168.88.0 netmask 255.255.255.0 { #指定分配ip地址的网段号和子网掩码 range 192.168.88.10 192.168.88.254; #指定地址池的范围,必须是上面的网段里的ip option domain-name-servers 114.114.114.114; #指定dns服务器地址 option routers 192.168.88.1; #指定网关 default-lease-time 600; #最短租约时间600秒 max-lease-time 7200; #最长租约时间7200秒 }
dhcp服务器里的网关或者dns服务器配置错误,就会误导客户机上不了网
3.启动服务,调试
如何服务启动不起来,需要在接口上配置dhcp服务器里分配的网段的ip地址
在我的实验里,需要在ens33接口配置192.168.88.0网段的ip
[root@lb-1 network-scripts]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:37:fb:39 brd ff:ff:ff:ff:ff:ff inet 192.168.88.1/24 brd 192.168.88.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever
然后再次启动dhcp服务
[root@lb-1 network-scripts]# service dhcpd start Redirecting to /bin/systemctl start dhcpd.service
如何知道dhcp服务启动了呢?
1.看进程 dhcpd
2.看端口 67 udp
[root@lb-1 network-scripts]# ps aux|grep dhcpd 看进程 dhcpd 3890 0.0 0.2 105996 11964 ? Ss 11:37 0:00 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid root 3893 0.0 0.0 112832 2292 pts/3 S+ 11:37 0:00 grep --color=auto dhcpd [root@lb-1 network-scripts]# netstat -anplut|grep dhcp 看端口 udp 0 0 0.0.0.0:67 0.0.0.0:* 3890/dhcpd [root@lb-1 network-scripts]#
4.测试使用
再开一台linux服务器做客户机,网卡类型选择nat模式,前面的linux dhcp服务器网卡也是nat模式
web3 这台linux服务器
需要在测试的客户机上使用dhcp动态获得ip地址
[root@web3 ~]# cd /etc/sysconfig/network-scripts/ [root@web3 network-scripts]# ls ifcfg-ens33 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team init.ipv6-global ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort network-functions ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel network-functions-ipv6 ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless [root@web3 network-scripts]# cat ifcfg-ens33 BOOTPROTO="dhcp" #指定动态获得ip地址 dhcp表示动态获得 none表示静态配置 NAME="ens33" DEVICE="ens33" #网卡的名字 ONBOOT="yes" #开机激活网卡
[root@web3 network-scripts]# service network restart 刷新网卡服务,重新去获得ip地址 [root@web3 network-scripts]# ifdown ens33;ifup ens33 刷新网卡服务,重新去获得ip地址
正在确定 ens33 的 IP 信息... 完成
ip地址的配置
1.手工配置ip
2.dhcp动态获得
如何修改了配置文件,需要刷新服务,生效
[root@lb-1 dhcp]# vim dhcpd.conf # have to hack syslog.conf to complete the redirection). log-facility local7; # A slightly different configuration for an internal subnet. subnet 192.168.88.0 netmask 255.255.255.0 { range 192.168.88.10 192.168.88.254; option domain-name-servers 114.114.114.114; option routers 192.168.88.88; default-lease-time 600; max-lease-time 7200; }
提醒:网关和dns服务器一定要配置正确,不然后导致客户机获得错误的网关和dns会不能上网
[root@lb-1 dhcp]# service dhcpd restart Redirecting to /bin/systemctl restart dhcpd.service
临时配置ip地址-->重启系统会丢失
永久配置ip地址
写到配置文件里
[root@web3 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO="dhcp" NAME="ens33" DEVICE="ens33" ONBOOT="yes" [root@web3 ~]# [root@lb-1 ~]# cd /etc/sysconfig/network-scripts/ [root@lb-1 network-scripts]# ls ifcfg-ens33 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team init.ipv6-global ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort network-functions ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel network-functions-ipv6 ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless ifcfg --》interface config [root@lb-1 network-scripts]# cat ifcfg-ens33 BOOTPROTO="none" #静态配置ip NAME="ens33" #接口的名字 UUID="05a84b8b-df06-4b4f-b1a0-d43e0edce7de" #网卡这个硬件的唯一标识 DEVICE="ens33" #接口的对应的设备 ONBOOT="yes" #开机的时候激活网卡 yes是激活 no禁用 IPADDR=192.168.88.1 #ip地址 PREFIX=24 #子网掩码的长度为24个1,相当于 255.255.255.0 #NETMASK=255.255.255.0 GATEWAY=192.158.88.2 DNS1=114.114.114.114 DNS2=192.168.88.1
ifcfg-ens33 文件里的配置,我们刷新网络服务的会写到/etc/resolv.conf,这个文件才是真正域名解析的时候看的文件
ifcfg-ens33 文件可以决定/etc/resolv.conf文件里的内容
[root@lb-1 network-scripts]# cat ifcfg-lo DEVICE=lo IPADDR=127.0.0.1 NETMASK=255.0.0.0 NETWORK=127.0.0.0 # If you're having problems with gated making 127.0.0.0/8 a martian, # you can change this to something else (255.255.255.255, for example) BROADCAST=127.255.255.255 ONBOOT=yes NAME=loopback
NetworkManager服务
添加路由
1.静态路由
2.默认路由
[root@web3 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:de:2c:63 brd ff:ff:ff:ff:ff:ff inet 192.168.88.10/24 brd 192.168.88.255 scope global dynamic ens33 valid_lft 553sec preferred_lft 553sec inet6 fe80::20c:29ff:fede:2c63/64 scope link valid_lft forever preferred_lft forever
临时添加ip地址
[root@web3 ~]# ip address add 192.168.227.144/24 dev ens33 [root@web3 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:de:2c:63 brd ff:ff:ff:ff:ff:ff inet 192.168.88.10/24 brd 192.168.88.255 scope global dynamic ens33 valid_lft 506sec preferred_lft 506sec inet 192.168.227.144/24 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fede:2c63/64 scope link valid_lft forever preferred_lft forever
service network restart 本质是停止网络服务(网络接口的ip地址会失效,路由表会刷新),重新去配置ip地址和生成新的路由表
nmcli 命令的使用依赖于NetworkManger服务需要开启
nmcli n 查看网络是否启用 networking
nmcli d 查看有哪些网络设备 device
nmcli c 查看哪些网络是连接在使用的 connection