Linux中的网络配置

1,IP

ip是网络进程的地址,由32个01组成。

网络位                                   主机位

1111110.11111110.11111110.11111110 =254.254.254.254

2,子网掩码
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里某台主机

例如:子网掩码:255.255.255.0

 3,网络通信判定
网络位相同主机位不相同的两个IP可以相互通信
例如:192.168.155.70和192.168.155.80可以相互通信
192.168.155.70/24      中的24=255.255.255.0

4,网络设定工具
ping                                           检测网络是否通畅
ping -c 1                                    ping 1 次
ping -w 1                                   等待1秒
ping -c1 -w1 ip                          ping1次等待1秒

ifconfig                                      查看IP信息

 

ifconfig device ip/24                设定IP

 

ifconfig device down               关闭

ifconfig device up                    开启

ip addr                                         检测或这顶网络接口


ip addr show                               检测
ip addr add ip/24 dev device     设定


ip addr del dev ens160 ip/24     删除

5.图形方式设定ip

使用这个 nm-connection-editor(图形)必须保证 systemctl status NetworkManageret必须开启

 设定完成后

nmcli connection down westos

nmcli connection up westos

 无图形模式设定

Ctrl alt f2  或     Nmtui     进入无图形模式

6.命令方式设定网络

 

nmcli device connect ens160                              启用ens160网卡

nmcli device show ens160                                   查看网卡信息

nmcli device connect ens160                              启用ens160网卡

ifconfig                                                               查看

nmcli device disconnect ens160                          关闭ens160网卡

nmcli device show ens160                                   查看网卡信息

nmcli device status ens160                                  查看网卡服务接口信息

nmcli connection show                                        查看连接

nmcli connection delete ens160                             删除连接

nmcli connection add con-name westos ifname ens160 ipv4.method manual ipv4.addresses 192.168.155.90/24 type ethernet                         添加连接

nmcli connection up westos                                                         启动

ifconfig ens160                                                                              查看ens160

 

nmcli connection show

[root@westos_student50 Desktop]# nmcli connection delete uuid 66250b3c-62ea-4f29-a56c-83b89653a755

nmcli connection show

nmcli connection down westos                            关闭连接

nmcli connection up westos                                开启连接

nmcli connection modify westos ipv4.addresses 192.168.155.70/24             更改

7.管理网络配置文件

[root@westos_student50 Desktop]# cd /etc/sysconfig/network-scripts/    网络配置目录

 

DEVICE=ens160                     设备名称

BOOTPROTO=none               设备工作方式

ONBOOT=yes                        网络服务开启时自动激活网卡

IPADDR=192.168.155.70         IP地址

PREFIX=24                             子网掩码

NAME= ens160                      接口名称

[root@westos_student50 network-scripts]# nmcli connection reload

[root@westos_student50 network-scripts]# nmcli connection up ens160  重启网卡,

Dhcp网络设定及dhcp服务配置

root@westos_student50 network-scripts]# vim ifcfg-ens160

[root@westos_student50 network-scripts]# nmcli connection reload

[root@westos_student50 network-scripts]# nmcli connection up ens160

[root@westos_student50 network-scripts]# ifconfig

[root@westos_student50 Desktop]#  mount /iso/rhel-8.2-x86_64-dvd.iso /westos

mount: /westos: WARNING: device write-protected, mounted read-only.

[root@westos_student50 Desktop]# cd /etc/yum.repos.d

[root@westos_student50 yum.repos.d]# vim westos.repo

以上是搭配软件仓库的命令

[root@westos_student50 yum.repos.d]# cd

[root@westos_student50 ~]# dnf search dhcp   查找dhcp

[root@westos_student50 ~]# dnf install dhcp-server.x86_64     安装

[root@westos_student50 ~]# rpm -qc dhcp-server     查询配置文件

[root@westos_student50 ~]# cat /etc/dhcp/dhcpd.conf      查看主配置文件

[root@westos_student50~]#cp/usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf                        用模板生成配置文件

[root@westos_student50 ~]# vim /etc/dhcp/dhcpd.conf

[root@westos_student50 ~]# systemctl enable --now dhcpd        启动

在另一台单网卡主机上进行测试

[root@westos_student50 ~]# cd /etc/sysconfig/network-scripts/

[root@westos_student50 ~]# ls

[root@westos_student50 ~]# rm -rf ifcfg-ens160

[root@westos_student50 ~]# vim ifcfg-ens160

DEVICE=ens160

ONBOOT=yes

BOOTPROTO=dhcp

NAME=ens160

[root@westos_student50 ~]# nmcli connection reload

[root@westos_student50 ~]# nmcli connection up ens160

[root@westos_student50 ~]# ifconfig

8.lo回环接口

 9.网关

在双网卡主机中做如下设定

[root@westos_student50 ~]# firewall-cmd --permanent --add-masquerade   打开火枪地址

[root@westos_student50 ~]# firewall-cmd –reload            让火墙地址生效

在单网卡中做如下设定

[root@westos_student50 network-scripts]# route -n                                    查看网关

[root@westos_student50 network-scripts]# ip route add default via 1.1.1.100      将1.1.1.100设置为默认网关。(临时)

[root@westos_student50 network-scripts]# ping 192.168.155.70

[root@westos_student50 network-scripts]# vim /etc/sysconfig/network(永久设定网关)

GATEWAY=1.1.1.100

[root@westos_student50 network-scripts]# nmcli connection reloate

[root@westos_student50 network-scripts]# nmcli connection up ens160

 10.设定dns及设定解析的优先级

dns即是地址解析,系统网络通信需要通过ip地址这个数字,当操作者输入网址www.baidu.com,这个网址不是一个可以通信的IP地址,于是必须要在系统中把www.baidu.com变成百度服务器的IP地址,这就是地址解析。

1),[root@westos_student50 Desktop]# vim /etc/hosts                       本地解析文件,此文件中直接提供地址解析

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

14.215.177.38 www.baidu.com

[root@westos_student50 Desktop]# ping www.baidu.com                  检测

2),[root@westos_student50 Desktop]# vim /etc/resolv.conf              dns的指向文件 不需要重新启动网络立即生效

# Generated by NetworkManager

search westos.org

nameserver 114.114.114.114

[root@westos_student50 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens160#需要重启网络,当网络重新启动 ##此参数会修改

DEVICE=ens160

BOOTPROTO=none

IPADDR=192.168.155.70

NAME=ens160

DNS1=218.30.19.50

[root@westos_student50 Desktop]# nmcli connection reload

[root@westos_student50 Desktop]# nmcli connection up  ens160

[root@westos_student50 Desktop]# cat /etc/resolv.conf

[root@westos_student50 Desktop]# ping www.baidu.com                         检测

[root@westos_student50 Desktop]# vim /etc/nsswitch.conf                      在这个里面host哪一行可以修改本地解析的优先级

[root@westos_student50 ~]# mv /root/.ssh/id_rsa* /mnt

档网络工作模式为dhcp时 系统会自动获得ip 网关 dns 那么/etc/resolv.conf会被获得到的信息修改 如果不需要获得dns信息 在网卡配置文件中加入 PEERDNS=no

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑 哲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值