Bash/Port/Forward

From Chorke Wiki
Jump to navigation Jump to search

Check Port is not used

# check port 22200
fuser 22200/tcp

# check port 55500
fuser 55500/tcp

Enable Specific Port

firewall-cmd --zone=public --permanent --add-port=22200/tcp
firewall-cmd --zone=public --permanent --add-port=55500/tcp
firewall-cmd --reload
netstat

Enable Range of Port

firewall-cmd --zone=public --permanent --add-port=22200-22299/tcp
firewall-cmd --zone=public --permanent --add-port=55500-55599/tcp
firewall-cmd --reload
netstat

Forwarding Parameters

# destination ip & port
CKI_LAN_ADDR=100.43.0.20
CKI_LAN_PORT=22211

# wan interface ip & port
CKI_WAN_ADDR=139.59.51.80
CKI_WAN_PORT=22200

Forwarding rule add

iptables -t nat -A PREROUTING -p tcp -m tcp -d $CKI_WAN_ADDR --dport $CKI_WAN_PORT -j DNAT --to-destination $CKI_LAN_ADDR:$CKI_LAN_PORT
iptables -A FORWARD -m state -p tcp -d $CKI_LAN_ADDR --dport $CKI_LAN_PORT --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -p tcp -m tcp -s $CKI_LAN_ADDR --sport $CKI_LAN_PORT -j SNAT --to-source $CKI_WAN_ADDR

Forwarding rule remove

iptables -t nat -D PREROUTING  -p tcp -m tcp -d $CKI_WAN_ADDR --dport $CKI_WAN_PORT -j DNAT --to-destination $CKI_LAN_ADDR:$CKI_LAN_PORT
iptables -D FORWARD -m state -p tcp -d $CKI_LAN_ADDR --dport $CKI_LAN_PORT --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -D POSTROUTING -p tcp -m tcp -s $CKI_LAN_ADDR --sport $CKI_LAN_PORT -j SNAT --to-source $CKI_WAN_ADDR

References