CentOS network configuration and remote connection (detailed steps)

Small chat : record, CentOS network configuration and remote connection operation. CentOS7, 8, and 9 are almost the same. This operation takes CentOS8 as an example.

Table of contents


1. Problem 1: The network cannot be pinged suddenly

[root@baiyiyu ~]# ping baidu.com
ping: baidu.com: Name or service not known
  • ifconfigView network configuration
[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. Resolving operations

ensNetwork is required , try restarting the network service

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

ifconfigCheck the network again : appears ens33orens160

[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

Try again to pingpass


2. Problem 2: Unable to connect to the external network

2.1. Connect to external network configuration

  • Enter the configuration file directory /etc/sysconfig/network-scriptsto modify the fileifcfg-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

There is a configuration file in it, the name of different CentOSversions may be different, roughly the same ifcfg-xxx, find and edit it, the original file is as follows

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

What we need to modify and add:

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)

What the last modification looked like:

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)

:wqSave and exit, ok

Try pingyour own computer'sip

[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
......

If it still doesn't work, restart the network service and it will be ok

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

3. Question 3: Remotely connect to CentOS

3.1. Turn off your CentOS firewall

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

Open the remote connection tool, I use Xshelldemo :

insert image description here

Then enter your username and password.

insert image description here
insert image description here
insert image description here


4. Expansion: about firewall commands

In earlier Linuxsystems , iptablesthe firewall management service was used by default to configure the firewall. Although newer firewalldfirewall management services have been in use for many years, a large number of enterprises continue to use them in production environments for various reasons iptables.

Therefore, maybe your computer only has firewalldbut not iptablesor vice versa, then you can only use the commands of existing services to operate the firewall. The e.g. CentOS7version is installed firewalld, and the service can also be installed if needed iptables.

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

Use iptablesto manage the firewall

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


essay

insert image description here

Guess you like

Origin blog.csdn.net/m0_48489737/article/details/127186164