1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash

# Script to set up network sharing with static IP



# Define network interfaces and IP addresses

LAN_INTERFACE="eth0"

WIFI_INTERFACE="wlan0"

LAN_IP="192.168.0.1"

DHCP_RANGE="192.168.0.10,192.168.0.50,12h"



# Enable IP forwarding

sysctl net.ipv4.ip_forward=1



# Set up iptables rules

iptables -A POSTROUTING -t nat -o $WIFI_INTERFACE -j MASQUERADE

iptables -A FORWARD -i $WIFI_INTERFACE -o $LAN_INTERFACE -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -A FORWARD -i $LAN_INTERFACE -o $WIFI_INTERFACE -j ACCEPT



# Start dnsmasq

service dnsmasq start

service dnsmasq restart



# Save iptables rules

iptables-save | tee /etc/iptables.rules



# Configure static IP for LAN interface

ifconfig $LAN_INTERFACE $LAN_IP

echo "interface $LAN_INTERFACE" >> /etc/dnsmasq.conf

echo "dhcp-range=$LAN_IP,$DHCP_RANGE" >> /etc/dnsmasq.conf

注意端口占用,需要kill