Left4Dead 2 Dedicated Server on CentOS7

by mkjacksontech
8 deployments · 6 still active · last rev. 7 days ago

Disposable Lead4Dead 2 server. Ready to play out of the box.
- Uses steam server installer automation from https://gameservermanagers.com/lgsm/l4d2server/
- Configures key server parameters via stackscripts prompts

Compatible with: CentOS 7
						#!/bin/bash
#
# Installs, configures and launches a Left 4 Dead 2 dedicated server
# 2016-09 mkjacksontech@gmail.com
#
# UDF Variables required by stackscript include #39161
# <UDF name="ss_whitelistip" Label="Firewall Whitelist IP" example="Your IP so you can bypass the firewall" />
#
# UDF Variables to configure L4D Server
# <UDF name="l4d2_hostname" Label="Title displayed as server's name (not actual hostname)" default="l4d_dedserv" />
# <UDF name="l4d2_search_key" Label="Keyword to directly identify server during manual connect" default="" />
# <UDF name="l4d2_rcon_password" Label="Server console password" default="changeme" />
# <UDF name="l4d2_sv_contact" Label="Email Address of server administrator" default="fake@email.gov" />
# <UDF name="l4d2_sv_region" Label="Choose server region" OneOf="0,1,3,4,255" default="1" />
# <UDF name="l4d2_sv_steamgroup" Label="Associate server with a steam user group (recommended)" default="0" />
# <UDF name="l4d2_sv_steamgroup_exclusive" Label="Only allow initial connection from a user in the steam group.  Once a game is in progress, others may join." oneOf="0,1" default="0" />
# <UDF name="l4d2_sv_gametypes" Label="Choose available game types" manyOf="coop,versus,survival,realism,teamversus,teamscavenge" default="coop,versus,survival,realism,teamversus,teamscavenge" />
# <UDF name="l4d2_sm_cvar_fps_max" Label="Server rate limit" default="0" />

# Generally configure and secure
# Update system
yum -y update

# Install dependencies specified at https://gameservermanagers.com/lgsm/l4d2server/
yum -y install \
    mailx \
    postfix \
    curl \
    wget \
    gzip \
    bzip2 \
    python \
    tmux \
    glibc.i686 \
    libstdc++ \
    libstdc++.i686

yum clean all

# Open steam ports in firewall,
# close SSH except for whitelist ip
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --zone=public --add-port=27015/tcp
firewall-cmd --zone=public --add-port=27015/tcp --permanent
firewall-cmd --zone=public --add-port=27015/udp
firewall-cmd --zone=public --add-port=27015/udp --permanent
firewall-cmd --zone=public  \
             --add-rich-rule="rule family=\"ipv4\" source address=\"$SS_WHITELISTIP\" accept"
firewall-cmd --zone=public --permanent \
             --add-rich-rule="rule family=\"ipv4\" source address=\"$SS_WHITELISTIP\" accept"
firewall-cmd --zone=public --remove-service=ssh
firewall-cmd --zone=public --remove-service=ssh --permanent

# Configure the steam user account with a random password
useradd steam
date +%s | sha256sum | base64 | head -c 32 | passwd steam --stdin

# As user steam, download and run l4d2 dedicated server installer
cd /home/steam
runuser -l steam -c 'mkdir ~/l4d2server'
cd /home/steam/l4d2server
runuser -l steam -c 'wget https://gameservermanagers.com/dl/l4d2server -O ~/l4d2server/l4d2server.sh'
runuser -l steam -c 'cd ~/l4d2server; bash ./l4d2server.sh auto-install'

# Update the server.cfg
cat << EOF >> /home/steam/l4d2server/serverfiles/left4dead2/cfg/l4d2-server.cfg
// Title displayed as server's name (not actual hostname)
hostname "$L4D2_HOSTNAME"

// Keyword to directly identify server during manual connect
sv_search_key "$L4D2_SEARCH_KEY"

// Server console password
rcon_password "$L4D2_RCON_PASSWORD"

// Email Address of server administrator
sv_contact "$L4D2_SV_CONTACT"

// Disable MOTD
// If someone knows how to set up an MOTD,
//  they'll know how to update this parameter
motd_enabled 0

// Set server region
// Linode servers are US, UK, SG or DE
// 0=US-East
// 1=US-West
// 3=Europe
// 4=Asia
// 255=World
sv_region $L4D2_SV_REGION

// Disable autokicking players
mp_disable_autokick 1

// Allow direct connections, or browsed lobby connections
sv_allow_lobby_connect_only 0

// Associate server with a steam user group (recommended)
// Use http://steamidfinder.com to find group id
sv_steamgroup $L4D2_SV_STEAMGROUP

// Only allow initial connection from a user in the steam group
sv_steamgroup_exclusive $L4D2_SV_STEAMGROUP_EXCLUSIVE

// Open up all voice chat between players
sv_alltalk 1

// Allowed game types
// sv_gametypes "$L4D2_SV_GAMETYPES"

// server rate limit
sm_cvar_fps_max $L4D2_SM_CVAR_FPS_MAX

// Specify this is not a local lan server
sv_lan 0

// Check for player disconnects
// heartbeat

// What does this do?  Is this required for a server to be
// available in the browser list?
// sv_flags "Browser" # what does this do?

// Disallow cheats
sv_cheats 0

// Check client integrety
sv_consistency 1

EOF

# Write a bland MOTD
runuser -l steam cat << EOF > /home/steam/l4d2server/serverfiles/left4dead2/motd.txt
<html>
<head><title>Left 4 Dead 2 / Fun with Linode StackScripts</title></head>
<body>
<h1>Powered by linode.com and stackscripts</h1>
<p>This dedicated Left 4 Dead 2 server was started with a few click.  This
server can be created at any time, instantly.  When you're done playing,
you can destroy it just as quickly.</p>
<p>For directions on starting your own server, visit 
<a href="https://mitchjacksontech.com/left-4-dead-2-server/">https://mitchjacksontech.com/left-4-dead-2-server/</a>
</body>
</html>

EOF

# Start the l4d2 server
runuser -l steam -c 'cd ~/l4d2server; bash ./l4d2server.sh start'