Skip to content

Commit fcf3ea2

Browse files
committed
feat: add --net-admin option to install script
This allows the install script to add `CAP_NET_ADMIN` to the installed binary with user consent. Combined with adding `CAP_NET_ADMIN` to the agent binary, we see an increase of >50% in networking speeds.
1 parent 4da1223 commit fcf3ea2

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

install.sh

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
set -eu
33

44
# Coder's automatic install script.
5-
# See https://github.com/coder/coder#installing-coder
5+
# See https://github.com/coder/coder#install
6+
#
7+
# To run:
8+
# curl -L https://coder.com/install.sh | sh
69

710
usage() {
811
arg0="$0"
912
if [ "$0" = sh ]; then
10-
arg0="curl -fsSL https://coder.com/install.sh | sh -s --"
13+
arg0="curl -fsSL https://coder.com/install.sh | sh"
1114
else
1215
not_curl_usage="The latest script is available at https://coder.com/install.sh
1316
"
@@ -61,6 +64,10 @@ Usage:
6164
just want it on your base system aswell.
6265
This supports most systems, however if you are unsure yours is supported you can check
6366
the link above.
67+
--net-admin
68+
Adds \`CAP_NET_ADMIN\` to the installed binary. This allows Coder to
69+
increase network speeds, but has security implications.
70+
See: https://man7.org/linux/man-pages/man7/capabilities.7.html
6471
6572
6673
The detection method works as follows:
@@ -230,7 +237,8 @@ main() {
230237
RSH_ARGS \
231238
EDGE \
232239
RSH \
233-
WITH_TERRAFORM
240+
WITH_TERRAFORM \
241+
CAP_NET_ADMIN
234242

235243
ALL_FLAGS=""
236244

@@ -290,6 +298,9 @@ main() {
290298
--with-terraform)
291299
WITH_TERRAFORM=1
292300
;;
301+
--net-admin)
302+
CAP_NET_ADMIN=1
303+
;;
293304
--)
294305
shift
295306
# We remove the -- added above.
@@ -362,7 +373,7 @@ main() {
362373
fi
363374

364375
# Start by installing Terraform, if requested
365-
if [ "${WITH_TERRAFORM-}" = 1 ]; then
376+
if [ "${WITH_TERRAFORM-}" ]; then
366377
with_terraform
367378
fi
368379

@@ -398,6 +409,26 @@ main() {
398409
install_standalone
399410
;;
400411
esac
412+
413+
if [ "${CAP_NET_ADMIN:-}" ]; then
414+
cap_net_admin
415+
fi
416+
}
417+
418+
cap_net_admin() {
419+
if ! command_exists setcap && command_exists capsh; then
420+
echo "Package 'libcap' not found. See install instructions for your distro: https://command-not-found.com/setcap"
421+
return
422+
fi
423+
424+
# Make sure we'e allowed to add CAP_NET_ADMIN.
425+
if sudo_sh_c capsh --has-p=CAP_NET_ADMIN; then
426+
sudo_sh_c setcap CAP_NET_ADMIN=+ep "$(command -v coder)" || true
427+
428+
# Unable to escalate perms, notify the user.
429+
else
430+
echo "Unable to setcap agent binary. Ensure the root user has CAP_NET_ADMIN permissions."
431+
fi
401432
}
402433

403434
parse_arg() {
@@ -697,10 +728,10 @@ sh_c() {
697728
sudo_sh_c() {
698729
if [ "$(id -u)" = 0 ]; then
699730
sh_c "$@"
700-
elif command_exists doas; then
701-
sh_c "doas $*"
702731
elif command_exists sudo; then
703732
sh_c "sudo $*"
733+
elif command_exists doas; then
734+
sh_c "doas $*"
704735
elif command_exists su; then
705736
sh_c "su - -c '$*'"
706737
else

0 commit comments

Comments
 (0)