Linux 下的网络配置
#######linux下的網絡配置############
###1.什么是IP ADDRESS
?? ?internet protocol ADDRESS??? ##網絡進程地址
?? ?
?? ?ipv4?? ?internet protocol version 4
?? ?
?? ?1.2x32
?? ?ip是由32個01組成
?? ?11111110.11111110.11111110.11111110 = 254.254.254.254
###2.子網掩碼
?? ?用來劃分網絡區域
?? ?子網掩碼非0的位對應的ip上的數字表示這個ip的網絡位
?? ?子網掩碼0位對應的數字是ip的主機位
?? ?網絡位表示網絡區域
?? ?主機位表示網絡區域里某臺主機
###3.ip通信判定
?? ?網絡位一致,主機位不一致的2個IP可以直接通訊
?? ?172.25.254.1/24??? 24=255.255.255.0
?? ?172.25.254.101/24
?? ?172.25.0.1/16
####4.網絡設定工具
?? ??? ?
?? ?ping ?? ??? ?##檢測網絡是否通暢
?? ?ping -c 1?? ?##ping 1 次
?? ?ping -w 1?? ?##等待1秒
?? ?ping -c1 -w1 ip?? ?##ping1次等待1秒
?? ?
?? ?ifconfig?? ?##查看或設定網絡接口
?? ?ifconfig?? ?##查看
?? ?ifconfig device ip/24?? ?##設定
?? ?ifconfig device down?? ?##關閉
?? ?ifconfig device up?? ?##開啟?? ?
?? ?ip addr?? ??? ?##檢測或這頂網絡接口
?? ?ip addr show?? ?##檢測
?? ?ip addr add ip/24 dev device?? ?##設定
?? ?ip addr del dev enp1s0 ip/24?? ?##刪除?? ?
?? ?
ip addr flush enp1s0 ??? ##更改ip步驟????? 刷新
?? ?ip addr add? dev enp1s0 192.168.31.239/24
?
"注意:?? ?device的名字一個物理事實,看到什么名字只能用什么名字"
####5.圖形方式設定ip
設定:
1.nm-connection-editor
更改:
不建議用服務控制網絡
systemctl restart NetworkManager
建議使用:
nmcli connection show
nmcli connection down? enp1s0
nmcli connection up enp1s0
2.nmtui
?? ?
####6.命令方式設定網絡
nmcli?? ??? ??? ?##NetworkManager必須開啟
nmcli device connect ens160?? ?##啟用ens160網卡
nmcli device disconnect ens160?? ?##關閉ens160網卡
nmcli device show ens160?? ?##查看網卡信息
nmcli device status ens160?? ?##查看網卡服務接口信息
?
nmcli connection show?? ??? ?##查看連接
nmcli connection down enp1s0 ?? ##關閉連接
nmcli connection up enp1s0? ?##開啟連接
nmcli connection delete??enp1s0?? ##刪除連接
#添加連接
nmcli connection add type ethernet con-name westos ifname ens160 ip4 192.168.31.239/24
#更改
nmcli connection modify westos? ipv4.addresses 192.168.31.109/24
####7.管理網絡配置文件
網絡配置目錄
/etc/sysconfig/network-scripts/
網絡配置文件的名命規則
ifcfg-xxxx
DEVICE=xxx?? ??? ?##設備名稱
BOOTPROTO=dhcp|static|none?? ?##設備工作方式
ONBOOT=yes?? ??? ??? ?##網絡服務開啟時自動激活網卡
IPADDR=?? ??? ??? ??? ?##IP地址
PREFIX=24?? ??? ??? ?##子網掩碼
NETMASK=255.255.255.0?? ??? ?##子網掩碼
NAME=?? ??? ??? ??? ?##接口名稱
dhcp網絡設定
vim /etc/sysconfig/network-scripts/ifcfg-enp1s0
DEVICE=enp1s0
ONBOOT=yes
BOOTPROTO=dhcp
nmcli connection reload
nmcli connection down enp1s0
nmcli connection up enp1s0
?
?
示例:
靜態網絡設定文件
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=enp1s0
ONBOOT=yes
IPADDR=172.25.254.101
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=enp1s0
注意:編輯過網絡配置文件之后需要重啟網絡
nmcli connection reload
nmcli connection down enp1s0
nmcli connection up enp1s0
一塊網卡上配置多個IP
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=enp1s0
ONBOOT=yes
IPADDR0=172.25.254.101
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=westos
IPADDR1=1.1.1.101
PREFIX1=24
重啟網絡
nmcli connection reload
nmcli connection down enp1s0
nmcli connection up enp1s0
ip addr show ens160
####8.lo回環接口
回環接口------人的神經----127.0.0.1-----localhost
####9.網關#####
1.把雙網卡主機localhost2變成路由器???????????????????????????
***單網卡主機ip1.1.1.101***
***雙網卡主機ip1.1.1.201,ip172.25.254.201****
***真機ip172.25.254.1***
(設置雙網卡主機)
systemctl start firewalld
firewall-cmd --list
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all
public (default, active)
? interfaces: br0 enp0s25 wlp3s0
? sources:
? services: dhcpv6-client ssh
? ports:
? masquerade: yes?? <<<地址偽裝功能開啟,真實主機變成路由器
? forward-ports:
? icmp-blocks:
? rich rules:
?sysctl -a | grep ip_forward??????????????????????????????
net.ipv4.ip_forward = 1
echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
給單網卡主機添加雙網卡的網關
ip route add default via 1.1.1.201
測試:當單網卡的主機可以ping 通172.25.254.1主機時說明路由成功
2.設定單網卡主機localhost1的網關
vim /etc/sysconfig/network?? ?##全局網關,針對所有沒有設定網關的網卡生效
GATEWAY=1.1.1.201 ? ????
vim /etc/sysconfig/network-scripts/ifcfg-enp1s0
GATEWAY0=1.1.1.201 ? ???? ##當網卡中設定的IP有多個時,指定對于那個IP生效
GATEWAY=1.1.1.201 ? ???? ##當網卡中設定的IP只有一個時
route -n ?? ??? ?##查看網關
Destination???? Gateway???????? Genmask???????? Flags Metric Ref??? Use Iface
0.0.0.0???????? 1.1.1.201?????? 0.0.0.0???????? UG??? 0????? 0??????? 0 enp1s0
1.1.1.0???????? 0.0.0.0???????? 255.255.255.0?? U???? 100??? 0??????? 0 enp1s0
192.168.122.0?? 0.0.0.0???????? 255.255.255.0?? U???? 0????? 0??????? 0 virbr0
?
?
##10.設定dns
#
#地址解析
#系統操作者對字符敏感
#系統網絡通信需要通過ip地址這個數字
#當操作著輸入網址www.baidu.com,這個網址不是一個可以通信的IP地址
#于是必須要在系統中把www.baidu.com變成百度服務器的IP地址
#這樣的過程叫做地址解析
#
#
domain name server == 域名解析服務? ##解析就是把域名變成IP
vim /etc/hosts?? ??? ??? ?##本地解析文件,此文件中直接提供地址解析
ip?? ??????????????????? 域名
14.215.177.38?? www.baidu.com
123.151.137.18 www.qq.com
?
vim /etc/resolv.conf?? ??? ?##dns的指向文件?? ?不需要重新啟動網絡立即生效
nameserver 114.114.114.114?? ?##當需要某個域名的IP地址時去問114.114.114.114
vim /etc/sysconfig/network-scripts/ifcfg-xxxx
DNS1=114.114.114.114?? ??? ?##需要重啟網絡,當網絡重新啟動
?? ??? ??? ??? ?##此參數會修改/etc/resolv.conf
?
注意:
當網絡工作模式為dhcp時‘4302
系統會自動獲得ip 網關 dns
那么/etc/resolv.conf會被獲得到的信息修改
如果不需要獲得dns信息
在網卡配置文件中加入
PREEDNS=no
##11.設定解析的優先級
系統默認:
/etc/hosts?? >?? /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts:?? ?files dns?? ??? ?##/etc/hosts優先
vim /etc/nsswitch.conf
39 hosts:?????? dns files?? ??? ?##/etc/resolv.conf dns指向優先
####12.dhcp服務配置
在主機中使用景象找到dhcp-server的安裝包
rpm -ivh dhcp-server
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf?? ?##用模板生成配置文件
vim /etc/dhcp/dhcpd.conf
# dhcpd.conf
# #
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "westos.com";?? ??? ?##域名
option domain-name-servers 114.114.114.114;?? ?##dns
default-lease-time 600;?? ??? ??? ??? ?##默認租約
max-lease-time 7200;?? ??? ??? ??? ?##最長租約
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
#Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
27?? ?刪除
28?? ?刪除
# This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 {?? ??? ?##子網設定
? range 172.25.254.160 172.25.254.200;?? ??? ??? ?##IP地址池
??? option routers 172.25.254.250;?? ??? ??? ?##網關
??? }
35行后全刪除
systemctl restart dhcpd
systemctl stop firewalld
?
?
測試:
在網絡工作模式是dhcp的主機中重啟網絡
可以看到
ip
GW
dns
全部獲取成功
?
?
?
總結
以上是生活随笔為你收集整理的Linux 下的网络配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 系统之虚拟化部署
- 下一篇: Linux 系统的日志管理