Skip to content

Commit 653cceb

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 2a19b46 commit 653cceb

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

install.sh

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
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"
@@ -61,6 +64,11 @@ 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
71+
This only works on Linux based systems.
6472
6573
6674
The detection method works as follows:
@@ -230,7 +238,8 @@ main() {
230238
RSH_ARGS \
231239
EDGE \
232240
RSH \
233-
WITH_TERRAFORM
241+
WITH_TERRAFORM \
242+
CAP_NET_ADMIN
234243

235244
ALL_FLAGS=""
236245

@@ -290,6 +299,9 @@ main() {
290299
--with-terraform)
291300
WITH_TERRAFORM=1
292301
;;
302+
--net-admin)
303+
CAP_NET_ADMIN=1
304+
;;
293305
--)
294306
shift
295307
# We remove the -- added above.
@@ -362,7 +374,7 @@ main() {
362374
fi
363375

364376
# Start by installing Terraform, if requested
365-
if [ "${WITH_TERRAFORM-}" = 1 ]; then
377+
if [ "${WITH_TERRAFORM-}" ]; then
366378
with_terraform
367379
fi
368380

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

403435
parse_arg() {
@@ -697,10 +729,10 @@ sh_c() {
697729
sudo_sh_c() {
698730
if [ "$(id -u)" = 0 ]; then
699731
sh_c "$@"
700-
elif command_exists doas; then
701-
sh_c "doas $*"
702732
elif command_exists sudo; then
703733
sh_c "sudo $*"
734+
elif command_exists doas; then
735+
sh_c "doas $*"
704736
elif command_exists su; then
705737
sh_c "su - -c '$*'"
706738
else

0 commit comments

Comments
 (0)