Valheim Linux Dedicated Server : How to define the Interface / public IP address to be used ?

Valheim Linux Dedicated Server : How to define the Interface / public IP address to be used ? 1 - steamsplay.com
Valheim Linux Dedicated Server : How to define the Interface / public IP address to be used ? 1 - steamsplay.com
Lazy devs binding on 0.0.0.0 and no option to set network configuration ???
Workaround : If your server has many network interfaces and/or public IP addresses, you can use Network Namespaces & iptables

 

Lazy devs binding on 0.0.0.0 ???

No way at the moment to define the @IP address to be used by the game..

Workaround : If your server has many network interfaces and/or public IP addresses, you can use Network Namespaces & iptables

Example of script that should work

#!/bin/bash
# !!! RUN ME AS ROOT USER !!!

steamUser=steam
steamGroup=${steamUser}
steamHome=/home/${steamUser}
valheimHome=${steamHome}/valheim
valheimName=”My Server”
valheimWorld=”Dedicated”
valheimPass=”changeme!”
publicIpAddr=w.x.y.z
serverIfName=”eth0″
netNsName=”valheim_ns”
vethNetwork=10.0.0.0/24
vethRootName=”valheim_veth”
vethRootAddr=10.0.1.1
vethNsName=”valheim_ns_veth”
vethNsAddr=10.0.1.2

echo “*** steam install (if required) ***”

if [[ $(grep “${steamUser}” /etc/passwd | sed -e ‘s/:.*//’) = “${steamUser}” ]]; then
echo -e “User ${steamUser} exists”
else
useradd -m ${steamUser}
fi

if [[ ! -f ${steamHome}/steamcmd.sh ]]; then
cd ${steamHome}
curl -sqL http://”https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz” | tar zxvf –
chown ${steamUser}.${steamGroup} -R ${steamHome}
fi

echo “*** valheim install (if required) ***”

if [[ ! -d ${valheimHome} ]]; then
cat << VALHEIM_INSTALL > ${steamHome}/valheim.install.sh
#!/bin/bash
# !!! RUN ME AS ${steamUser} USER !!!

mkdir -p ${valheimHome}

cd ${steamHome}

${steamHome}/steamcmd.sh +login anonymous +force_install_dir ${valheimHome} +app_update 896660 validate +exit

VALHEIM_INSTALL
chmod 755 ${steamHome}/valheim.install.sh
chown ${steamUser}.${steamGroup} ${steamHome}/valheim.install.sh
su – ${steamUser} -c ${valheimHome}/valheim.install.sh
fi

echo “*** valheim start script ***”

cat << VALHEIM_START > ${steamHome}/valheim.start.sh
#!/bin/bash
# !!! RUN ME AS ${steamUser} USER !!!

export templdpath=\$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./linux64:\$LD_LIBRARY_PATH
export SteamAppId=892970

echo “Updating”

cd ${steamHome}
${steamHome}/steamcmd.sh +login anonymous +force_install_dir ${valheimHome} +app_update 896660 +quit > ${steamHome}/valheim.update.log 2>&1

echo “Workaround”

cp -Rvvf ${steamHome}/linux64/steamclient.so ${valheimHome}/linux64/

echo “Starting”

cd ${valheimHome}
# NOTE: Minimum password length is 5 characters & Password cant be in the server name.
# NOTE: You need to make sure the ports 2456-2458 is being forwarded to your server through your local router & firewall.
./valheim_server.x86_64 -name “${valheimName}” -port 2456 -world “${valheimWorld}” -password “${valheimPass}” -public 1 > ${steamHome}/valheim.server.log 2>&1

export LD_LIBRARY_PATH=\$templdpath

VALHEIM_START
chmod 755 ${steamHome}/valheim.start.sh
chown ${steamUser}.${steamGroup} ${steamHome}/valheim.start.sh

echo “*** network config ***”

if [[ ! -d /sys/devices/virtual/net/${vethRootName} ]]; then
ip netns add ${netNsName}
ip link add ${vethNsName} type veth peer name ${vethRootName}
ip link set ${vethRootName} up
ip address add ${vethRootAddr}/24 dev ${vethRootName}
ip link set ${vethNsName} netns ${netNsName}
ip netns exec ${netNsName} ip link set dev lo up
ip netns exec ${netNsName} ip link set dev ${vethNsName} up
ip netns exec ${netNsName} ip address add ${vethNsAddr}/24 dev ${vethNsName}
ip netns exec ${netNsName} route add default gw ${vethRootAddr}

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -d ${publicIpAddr} -p tcp -m multiport –dports 2456:2458 -j DNAT –to-destination ${vethNsAddr}
iptables -t nat -A PREROUTING -d ${publicIpAddr} -p udp -m multiport –dports 2456:2458 -j DNAT –to-destination ${vethNsAddr}
#iptables -A FORWARD -i ${serverIfName} -o ${vethRootName} -p tcp -m multiport –dports 2456:2458 -j ACCEPT
#iptables -A FORWARD -i ${serverIfName} -o ${vethRootName} -p udp -m multiport –dports 2456:2458 -j ACCEPT
iptables -A FORWARD -i ${serverIfName} -o ${vethRootName} -j ACCEPT
iptables -A FORWARD -i ${vethRootName} -o ${serverIfName} -j ACCEPT
iptables -t nat -A POSTROUTING -s ${vethNsAddr} ! -d ${vethNsAddr} -o ${serverIfName} -j SNAT –to-source ${publicIpAddr}
fi

echo “*** public address ***”
ip netns exec ${netNsName} curl -s http://ipv4.icanhazip.com

echo “*** starting server ***”
ip netns exec ${netNsName} su – ${steamUser} -c ${valheimHome}/valheim.start.sh

Usage & Notes

You should install first the following packages : lib32gcc1 tmux screen

And add something like :

/etc/systemd/system/valheim.service
–8<————————————————–
[Unit]
Description=Valheim service
Wants=network.target
After=syslog.target network-online.target

[Service]
User=root
Type=simple
Restart=on-failure
RestartSec=10
KillMode=process
WorkingDirectory=${valheimHome}
ExecStart=${theSampleScriptFullName}

[Install]
WantedBy=multi-user.target
–8<————————————————–
Do not forget to update the values of ${valheimHome} and ${theSampleScriptFullName} to the real ones

Before running :

systemctl daemon-reload
systemctl enable valheim.service

Written by Yurg

Here we come to an end for Valheim Linux Dedicated Server : How to define the Interface / public IP address to be used ? hope you enjoy it. If you think we forget something to include or we should make an update to the post let us know via comment, and we will fix it asap! Thanks and have a great day!


Be the first to comment

Leave a Reply

Your email address will not be published.


*