Skip to content

Commit a6abf47

Browse files
yuhuyoyoYu Hu
and
Yu Hu
authored
remove sudo pw requirement for vscode user abc (#172)
* remove pw * comment out default * unbound variable * not generalize * Update .devcontainer.json * eof * array to string * Update .devcontainer.json * double quote --------- Co-authored-by: Yu Hu <[email protected]>
1 parent a6ce549 commit a6abf47

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/vscode/.devcontainer.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
"service": "app",
55
"shutdownAction": "none",
66
"workspaceFolder": "/workspace",
7-
"postCreateCommand": [
8-
"./startupscript/post-startup.sh",
9-
"abc",
10-
"/config",
11-
"${templateOption:cloud}",
12-
"${templateOption:login}"
13-
],
7+
"postCreateCommand":
8+
"./startupscript/post-startup.sh abc /config \"${templateOption:cloud}\" \"${templateOption:login}\"; ./sudo-passwordless.sh abc",
149
// re-mount bucket files on container start up
1510
"postStartCommand": [
1611
"./startupscript/remount-on-restart.sh",

src/vscode/sudo-passwordless.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# This script is used to set up passwordless sudo for the core user on the VM.
4+
# It requires to be run with root priviledges and USER_NAME to be set in the environment.
5+
# It is typically called from post-startup.sh.
6+
7+
USER_NAME="${1}"
8+
9+
if [[ -z "${USER_NAME}" ]]; then
10+
echo "Usage: $0 <username>"
11+
exit 1
12+
fi
13+
14+
sudoers_file="/etc/sudoers"
15+
sudoers_d_file="/etc/sudoers.d/${USER_NAME}"
16+
17+
# Make sure user exists
18+
if ! id "${USER_NAME}" &>/dev/null; then
19+
echo "User ${USER_NAME} does not exist."
20+
exit 1
21+
fi
22+
23+
# Check if there's an old rule in the main sudoers file that requires a password
24+
if grep -q "^${USER_NAME} ALL=(ALL:ALL) ALL" "${sudoers_file}"; then
25+
echo "Found password-requiring rule for ${USER_NAME} in /etc/sudoers. Commenting it out."
26+
27+
# Comment out the old rule in /etc/sudoers
28+
sed -i "s/^${USER_NAME} ALL=(ALL:ALL) ALL/# ${USER_NAME} ALL=(ALL:ALL) ALL/" "${sudoers_file}"
29+
fi
30+
31+
echo "${USER_NAME} ALL=(ALL) NOPASSWD:ALL" > "${sudoers_d_file}"
32+
chmod 440 "${sudoers_d_file}"
33+
34+
echo "User ${USER_NAME} has been given passwordless sudo access."

0 commit comments

Comments
 (0)