插件窝 干货文章 如何配置和管理Linux上的网络服务

如何配置和管理Linux上的网络服务

sudo 配置 systemctl bash 403    来源:    2025-04-10

Linux网络服务配置与管理指南

作为IT工程师,管理Linux网络服务是核心技能之一。以下是一份全面的配置与管理指南:

1. 网络接口配置

静态IP配置

# 编辑网络配置文件(根据发行版选择)
sudo vi /etc/network/interfaces  # Debian/Ubuntu
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0  # RHEL/CentOS

# 示例配置
auto 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

动态IP (DHCP)

iface eth0 inet dhcp

应用配置

sudo systemctl restart networking  # Debian/Ubuntu
sudo systemctl restart NetworkManager  # RHEL/CentOS

2. 防火墙管理

iptables (传统)

# 查看规则
sudo iptables -L -n -v

# 允许SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 保存规则
sudo iptables-save > /etc/iptables.rules

firewalld (RHEL/CentOS)

# 查看状态
sudo firewall-cmd --state

# 开放端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload

ufw (Ubuntu)

sudo ufw enable
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp

3. DNS配置

编辑resolv.conf

sudo vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

永久配置(Debian/Ubuntu)

sudo vi /etc/resolvconf/resolv.conf.d/head
nameserver 8.8.8.8
sudo resolvconf -u

4. 网络诊断工具

# 基本连接测试
ping google.com

# 路由追踪
traceroute google.com

# 网络接口信息
ifconfig  # 或 ip a

# 路由表
route -n  # 或 ip route

# 网络连接状态
netstat -tulnp
ss -tulnp  # 更现代的替代品

# DNS查询
nslookup google.com
dig google.com

5. 常见网络服务

SSH服务

# 安装
sudo apt install openssh-server  # Debian/Ubuntu
sudo yum install openssh-server  # RHEL/CentOS

# 配置
sudo vi /etc/ssh/sshd_config
# 修改端口、禁用root登录等

# 重启服务
sudo systemctl restart sshd

Web服务器 (Apache/Nginx)

# Apache安装
sudo apt install apache2
sudo systemctl enable apache2
sudo systemctl start apache2

# Nginx安装
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx

DHCP服务器

sudo apt install isc-dhcp-server
sudo vi /etc/dhcp/dhcpd.conf
# 配置子网、IP范围等
sudo systemctl restart isc-dhcp-server

6. 高级网络配置

网络绑定 (Bonding)

# 编辑网络配置
sudo vi /etc/network/interfaces

# 添加绑定配置
auto bond0
iface bond0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    slaves eth0 eth1
    bond-mode 4  # 802.3ad (LACP)
    bond-miimon 100
    bond-lacp-rate 1

VLAN配置

# 创建VLAN接口
sudo ip link add link eth0 name eth0.100 type vlan id 100

# 永久配置
auto eth0.100
iface eth0.100 inet static
    address 192.168.100.1
    netmask 255.255.255.0
    vlan-raw-device eth0

7. 网络监控

实时流量监控

# iftop (需安装)
sudo apt install iftop
sudo iftop -i eth0

# nload
sudo apt install nload
nload eth0

长期监控

# vnstat (流量统计)
sudo apt install vnstat
vnstat -i eth0

8. 网络服务管理命令

# 查看服务状态
sudo systemctl status <service>

# 启动/停止/重启服务
sudo systemctl start <service>
sudo systemctl stop <service>
sudo systemctl restart <service>

# 启用/禁用开机启动
sudo systemctl enable <service>
sudo systemctl disable <service>

通过掌握这些配置和管理技术,您将能够有效地维护Linux服务器上的各种网络服务。根据具体需求和环境,可能需要调整和组合这些配置选项。