linux下的网络配置

1.什么是IP ADDRESS

ip是由32个0 1组成
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.2/24
172.25.0.1/16

4.网络设定工具

ping ##检测网络是否通畅
ifconfig ##查看或设定网络接口
ifconfig ##查看
linux下的网络配置
ifconfig (device) ip/24 ##设定
ifconfig (device) down ##关闭
ifconfig (device) up ##开机

ip addr ##检测或这顶网络接口
linux下的网络配置
ip addr show ##检测
ip addr add (ip)/24 dev (device) ##设定

ip addr flush eth0 ##更改ip步骤
ip addr add (ip)/24 dev (device网络名称)

5.图形方式设定ip

1.nm-connection-editor
systemctl stop NetworkManager ##停止网络默认(记忆)
systemctl restart network ##重启网络
systemctl start NetworkManager
linux下的网络配置
2.nmtui
linux下的网络配置

6.管理网络配置文件

网络配置目录
cd /etc/sysconfig/network-scripts/
ifconfig ##查看名称及信息(eth0)
linux下的网络配置
网络配置文件的命名规划
(静态设置)
vim ifcfg-eth0 ##打开文件
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDRO=172.25.254.177 ##IP地址
NETMASK=255.255.255.0 ##子网掩码
systemctl restart network ##重启网络
linux下的网络配置
(动态设置)
vim ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
systemctl restart network ##重启网络

7.lo回环接口

回环接口-------人的神经—-----127.0.0.1
 1) LO接口是在LINUX系统中,除了网络接口eth0,还可以有别的接口,比如lo(本地环路接口)。
 2) LO接口的作用是:假如包是由一个本地进程为另一个本地进程产生的, 它们将通过外出链的’lo’接口,然后返回进入链的’lo’接口.具体参考包过滤器的相关内容。

8.dhcp服务配置

cd /etc/dhcp/
vim dhcpd.conf(若文件内只有几行,复制文件内/usr/share…)
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/decp/dhcpd.conf
2 #
3 # Sample configuration file for ISC dhcpd
4 #
5
6 # option definitions common to all supported networks…
7 option domain-name “westos.com”; ##域名
8 option domain-name-servers 114.114.114.114; ##dns
9
10 default-lease-time 600; ##默认租约
11 max-lease-time 7200; ##最长租约
12
13 # Use this to enble / disable dynamic dns updates globally.
14 #ddns-update-style none;
15
16 # If this DHCP server is the official DHCP server for the local
17 # network, the authoritative directive should be uncommented.
18 #authoritative;
19
20 # Use this to send dhcp log messages to a different log file (you also
21 # have to hack syslog.conf to complete the redirection).
22 log-facility local7;
23
24 # No service will be given on this subnet, but declaring it helps the
25 # DHCP server to understand the network topology.
26
27 ##27.28删除
28
29 subnet 172.25.254.0 netmask 255.255.255.0 { ##子网设定
30 range 172.25.254.160 172.25.254.200; ##IP地址池
31 option routers 172.25.254.124; ##网关
32 }
33 ##35行以后全部删除
systemctl restart network ##重启网络

cat /var/lib/dhcpd/dhcpd.leases ##显示使用网络的用户

9.设定DNS给客户

关闭防火墙
firewall-cmd-list-all

vim /etc/hosts ##本地解析
直接ping www.baidu.com不能连接,ping 119.75.213.61则可以连接,所以需在文件中加入119.75.213.61 www.baidu.com

vim /etc/resolv.conf ##外部解析 (nameserver+IP)
本地解析优先于外部解析。
文件内加入nameserver xxx.xxx.xxx.xxx表示要解析ip,但无法解析,还需进入
linux下的网络配置
vim /etc/sysconfig/network-scripts/ifcfg-westos编辑。
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
IPADDR=添加(ip地址)
PREFIX=24
GATEWAY=添加(ip地址) ## 确定网关
PEERDNS=yes|no ##运行时不更改外部解析文件

vim /etc/nsswitch.conf 39行 显示本地优先于外部。
linux下的网络配置

systemctl start firewalld ##开启火墙

7.命令方式设定网络

要先打开systemctl status NetworkManager
linux下的网络配置
nmcli device connect eth0 ##启动eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
linux下的网络配置
nmcli device syayus eth0 ##查看网卡服务接口信息
linux下的网络配置
nmcli connection add type ethernet con-name (westos)ifname eth0 autoconnect yes
##创建动态地址
nmcli connection add con-name (12345) type ethernet ifname eth0 ip4 172.25.254.177/24
##创建静态地址
linux下的网络配置
nmcli connection modify (12345) ipv4.addresses 172.25.254.111/24
##更改地址(更改完需重启)
-----systemctl restart network
linux下的网络配置
当从动态地址更改为静态地址时,要先nmcli connection modify 12345 ipv4.addresses 172.25.254.177/24,再输入nmcli connection modify westos ipv4method manual,重启网络,修改成功。
当从静态地址更改位动态地址时,直接输入mncli connection modify 12345 ipv4.method auto,重启网络,修改成功。

nmcli connection show ##查看网络
linux下的网络配置
nmcli connection down (12345) ##关闭12345网络
nmcli connection up (12345) ##打开12345网络
nmcli connection delete(12345) ##删除12345网络