作为IT工程师,管理Linux网络服务是核心技能之一。以下是一份全面的配置与管理指南:
# 编辑网络配置文件(根据发行版选择)
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
iface eth0 inet dhcp
sudo systemctl restart networking # Debian/Ubuntu
sudo systemctl restart NetworkManager # RHEL/CentOS
# 查看规则
sudo iptables -L -n -v
# 允许SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 保存规则
sudo iptables-save > /etc/iptables.rules
# 查看状态
sudo firewall-cmd --state
# 开放端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload
sudo ufw enable
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
sudo vi /etc/resolvconf/resolv.conf.d/head
nameserver 8.8.8.8
sudo resolvconf -u
# 基本连接测试
ping google.com
# 路由追踪
traceroute google.com
# 网络接口信息
ifconfig # 或 ip a
# 路由表
route -n # 或 ip route
# 网络连接状态
netstat -tulnp
ss -tulnp # 更现代的替代品
# DNS查询
nslookup google.com
dig google.com
# 安装
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
# 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
sudo apt install isc-dhcp-server
sudo vi /etc/dhcp/dhcpd.conf
# 配置子网、IP范围等
sudo systemctl restart isc-dhcp-server
# 编辑网络配置
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接口
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
# iftop (需安装)
sudo apt install iftop
sudo iftop -i eth0
# nload
sudo apt install nload
nload eth0
# vnstat (流量统计)
sudo apt install vnstat
vnstat -i eth0
# 查看服务状态
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服务器上的各种网络服务。根据具体需求和环境,可能需要调整和组合这些配置选项。