IT袋

当前位置:主页 > 经验教程 > 系统教程 >

Linux网络配置与故障排除

Linux网络配置与故障排除 Linux网络配置与故障解决(2)

时间:2024-01-31 15:00:19 来源:IT袋 作者:马勇
导读:Linux网络配置与故障排除,/etc/sysconfig/network-scripts/ 目录下,并以 ifcfg-eth0 命名: sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0 配置静态IP地址 假设我们要为 eth0 接口配置一个静态IP地址

Linux网络配置与故障排除

/etc/sysconfig/network-scripts/目录下,并以ifcfg-eth0命名:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
  1. 配置静态IP地址

假设我们要为eth0接口配置一个静态IP地址192.168.1.100,子网掩码255.255.255.0,网关192.168.1.1,DNS服务器8.8.8.88.8.4.4。在Debian系统中,配置可能如下所示:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

在Red Hat系统中,配置可能如下所示:

# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
  1. 重启网络服务

保存配置文件后,我们需要重启网络服务以使更改生效。在Debian系统上,可以使用以下命令:

sudo systemctl restart networking

在Red Hat系统上,可以使用:

sudo systemctl restart network

或者,如果您使用的是NetworkManager:

sudo systemctl restart NetworkManager

配置路由

假设我们需要为网络添加一个静态路由,我们可以使用ip命令。

例如,要添加一个通过网关192.168.1.1到达目标网络10.0.0.0/8的路由,我们可以执行:

sudo ip route add 10.0.0.0/8 via 192.168.1.1

要永久保存此路由,您需要将其添加到网络配置文件中,或在系统启动时通过脚本添加。

配置防火墙

使用iptables配置防火墙时,我们需要定义规则链和规则。例如,要允许所有来自本地网络(192.168.1.0/24)的SSH连接(端口22),我们可以执行:

sudo iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -j ACCEPT

如果您使用的是firewalld,您可以使用firewall-cmd命令。例如,要允许公共区域的SSH服务,您可以执行:

sudo firewall-cmd --zone=public --add-service=ssh --permanent
sudo firewall-cmd --reload

网络故障排除

当网络出现问题时,ping是一个很好的起点。例如,要测试与8.8.8.8的连接,您可以执行:

ping 8.8.8.8

相关阅读