CentOS 网络配置和远程连接(详细步骤)

小聊:记录,CentOS网络配置和远程连接操作。CentOS7、8、9都差不多。本次操作以 CentOS8 为例。

目录


1. 问题1:网络突然无法 ping 通

[root@baiyiyu ~]# ping baidu.com
ping: baidu.com: Name or service not known
  • ifconfig 查看网络配置
[root@baiyiyu ~]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 16  bytes 960 (960.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 960 (960.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ec:4c:76  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

1.1. 解决操作

需要 ens 网络,重启网络服务试试

centos 7:
service network restart
centos 8:
systemctl restart NetworkManager

再次 ifconfig 查看网络:出现 ens33ens160

[root@baiyiyu network-scripts]# systemctl restart NetworkManager
[root@baiyiyu network-scripts]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ......
        
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 16  bytes 960 (960.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 960 (960.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ec:4c:76  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

再次尝试可 ping


2. 问题2:无法连接外网

2.1. 连接外网配置

  • 进入配置文件目录 /etc/sysconfig/network-scripts 修改文件 ifcfg-ens160
[root@baiyiyu ~]# cd /etc/sysconfig/network-scripts
[root@baiyiyu network-scripts]# ll
total 4
-rw-r--r--. 1 root root 248 Jan 12  2022 ifcfg-ens160

里面有一个配置文件,不同 CentOS 版本名字可能不同,大致都是 ifcfg-xxx,找到并编辑它,原文件如下

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=6e461031-afc4-4f70-a574-9f669dff9520
DEVICE=ens160
ONBOOT=yes

我们需要修改和添加的:

BOOTPROTO=static  # dhcp(自动获取)改为static,动态获取ip地址协议改为静态(固定)

NETMAST=255.255.255.0  # 添加子网掩码(一般都用这个)
GATEWAY=192.168.2.1   # 添加网关(网关一般都是用192.168.*.1。如果IP网段是1,默认网关就是192.168.1.1,IP设置就是192.168.1.N (N=2~254)。局域网常用的网段是0和1,正常用1网段,我这里用2,习惯了)
IPADDR=192.168.2.130  # 添加你想要的ip地址,不要乱配,根据你的网关配
DNS1=114.114.114.114  # 添加DNS1域名(国内推荐使用的dns地址是这个:114 DNS:114.114.114.114)
DNS1=8.8.8.8  # 添加DNS2域名备用(国外推荐使用的地址是这个:Google DNS:8.8.8.8)

最后修改的样子:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static  # dhcp(自动获取)改为static,动态获取ip地址协议改为静态(固定)
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=6e461031-afc4-4f70-a574-9f669dff9520
DEVICE=ens160
ONBOOT=yes
NETMAST=255.255.255.0  # 添加子网掩码(一般都用这个)
GATEWAY=192.168.2.1   # 添加网关(网关一般都是用192.168.*.1。如果IP网段是1,默认网关就是192.168.1.1,IP设置就是192.168.1.N (N=2~254)。局域网常用的网段是0和1,正常用1网段,我这里用2,习惯了)
IPADDR=192.168.2.130  # 添加你想要的ip地址,不要乱配,根据你的网关配
DNS1=114.114.114.114  # 添加DNS1域名(国内推荐使用的dns地址是这个:114 DNS:114.114.114.114)
DNS2=8.8.8.8  # 添加DNS2域名备用(国外推荐使用的地址是这个:Google DNS:8.8.8.8)

:wq 保存退出,ok了

尝试 ping 自己本机电脑的 ip

[root@baiyiyu network-scripts]# ping XXXXXX
PING 192.168.159.1 (192.168.159.1) 56(84) bytes of data.
64 bytes from 192.168.159.1: icmp_seq=1 ttl=128 time=10.6 ms
64 bytes from 192.168.159.1: icmp_seq=2 ttl=128 time=0.437 ms
64 bytes from 192.168.159.1: icmp_seq=3 ttl=128 time=1.40 ms
......

如果还不行,重启一下网络服务就ok了

centos 7:
service network restart
centos 8:
systemctl restart NetworkManager

3. 问题3:远程连接CentOS

3.1. 关闭你的CentOS防火墙

#关闭防火墙
systemctl stop firewalld
#查看防火墙状态
systemctl status firewalld
firewall-cmd --state
#启动防火墙
systemctl start firewalld
firewall-cmd --reload
#设置开机启动
systemctl enable firewalld
#停止并禁用开机启动
systemctl disable firewalld

打开远程连接工具,我用 Xshell 演示:

扫描二维码关注公众号,回复: 14821896 查看本文章

在这里插入图片描述

之后输入用户名和密码就可以了。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


4. 拓展:关于防火墙命令

在早期的 Linux 系统中,默认使用的是 iptables 防火墙管理服务来配置防火墙。尽管新型的 firewalld 防火墙管理服务已经被投入使用多年,但是大量的企业在生产环境中依然出于各种原因而继续使用 iptables

所以,可能你的电脑里只有 firewalld 而没有 iptables 或者相反,那就只能使用已有服务的命令操作防火墙了。比如 CentOS7 版本是安装的 firewalld ,如果需要使用 iptables ,也可以安装该服务。

# 先关闭自带防火墙
systemctl stop firewalld
# 安装或更新服务
yum install iptables-services
# 启动iptables服务
systemctl enable iptables
# 打开iptables
systemctl start iptables
# 开启防火墙
service iptables start

使用 iptables 来管理防火墙

# 查看防火墙状态
service iptables status	
# 关闭防火墙
service iptables stop
# 启动防火墙
service iptables start
# 禁止防火墙自启
chkconfig iptables off	


随笔

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_48489737/article/details/127186164