Created
June 11, 2025 20:30
-
-
Save eznix86/4b562aeafd99a0ad4479bba13e6dee31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [[ $EUID -ne 0 ]]; then | |
echo "[ERROR] This script must be run as root (use sudo)." >&2 | |
exit 1 | |
fi | |
SUBNET="192.168.42." | |
echo "[INFO] Detecting host IP in subnet $SUBNET using arp -a..." | |
HOST_IP=$(arp -a | grep "$SUBNET" | awk '{print $2}' | tr -d '()' | head -n1) | |
if [[ -z "$HOST_IP" ]]; then | |
echo "[ERROR] Could not find host IP in subnet $SUBNET." >&2 | |
exit 1 | |
fi | |
echo "[INFO] Host IP detected as $HOST_IP" | |
echo "[INFO] SSH into host ($HOST_IP) to enable IP forwarding and iptables rules..." | |
ssh root@"$HOST_IP" bash -s <<EOF | |
set -e | |
echo "[HOST] Enabling IP forwarding..." | |
sysctl -w net.ipv4.ip_forward=1 >/dev/null | |
if [[ \$(sysctl -n net.ipv4.ip_forward) -ne 1 ]]; then | |
echo "[HOST ERROR] Failed to enable IP forwarding." >&2 | |
exit 1 | |
fi | |
OUT_IF=\$(ip route get 8.8.8.8 2>/dev/null | awk '{print \$5; exit}') | |
if [[ -z "\$OUT_IF" ]]; then | |
echo "[HOST ERROR] Could not detect outgoing interface." >&2 | |
exit 1 | |
fi | |
echo "[HOST] Outgoing interface detected: \$OUT_IF" | |
echo "[HOST] Setting iptables rules..." | |
iptables -P FORWARD ACCEPT | |
iptables -t nat -C POSTROUTING -s 192.168.42.0/24 -o "\$OUT_IF" -j MASQUERADE 2>/dev/null \ | |
|| iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o "\$OUT_IF" -j MASQUERADE | |
if ! iptables -t nat -L POSTROUTING -n | grep -q MASQUERADE; then | |
echo "[HOST ERROR] iptables MASQUERADE rule was not set correctly." >&2 | |
exit 1 | |
fi | |
echo "[HOST] IP forwarding and NAT setup complete." | |
EOF | |
echo "[INFO] Setting default route on Milk-V Duo to host $HOST_IP..." | |
ip route replace default via "$HOST_IP" | |
echo "[INFO] Setting DNS on Milk-V Duo..." | |
echo "nameserver 8.8.8.8" >> /etc/resolv.conf | |
echo "[INFO] Testing connectivity from Milk-V Duo..." | |
if ping -c 2 -W 2 8.8.8.8 >/dev/null; then | |
echo "[SUCCESS] Milk-V Duo can reach 8.8.8.8" | |
if ping -c 2 -W 2 google.com >/dev/null; then | |
echo "[SUCCESS] DNS resolution works (google.com reachable)" | |
else | |
echo "[WARNING] DNS resolution failed, but IP connectivity works." | |
fi | |
else | |
echo "[ERROR] Milk-V Duo cannot reach the internet via host." >&2 | |
exit 1 | |
fi |
if time is an issue append the script with:
echo "Syncing time with NTP..."
ntpd -d -q -n -p pool.ntp.org
echo "Time sync completed"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do it on boot
This only applies for alpine version
You can also set the static ip by looking at, but it is different.
https://xyzdims.com/3d-printers/misc-hardware-notes/iot-milk-v-duo-risc-v-esbc-running-linux/#Static_IP_for_Host_with_RNDIS
Setup Mac address
create
/opt/milkv/rndis-macs.sh
and dochmod +x /opt/milkv/rndis-macs.sh
Afterwards
Edit
/etc/init.d/usb-rndis
it should be like thisEdit
/etc/dnsmasq.conf
so it looks like thisNow to automatically setup internet on boot:
Things to do before:
ssh-keygen
without a password on the milkv-duoiptables
is setup correctly./usr/local/bin/milkv-network-setup.sh
to know if it is working correctly2. Create an OpenRC service
Create a file
/etc/init.d/milkv-network-setup
:3. Enable the service
4. Test it now (optional)
Ensure that the host recognize the milkv-duo via ssh
Example doing
ssh-keygen
and add the public key to the rootHow it works
if the hosts reboots:
follow this: https://gist.github.com/eznix86/26f1547fcbfb7a0e599c08b72463fc62