Skip to content

Commit 11934b9

Browse files
KeitaWmhuguesaws
andauthored
Update gen-keypair-ubuntu.sh to update authorized_keys (#518)
* Update gen-keypair-ubuntu.sh to update authorized_keys IHAC who accidentally remove `authorized_keys` after cluster setup. That caused ``` 2024-12-26T09:09:03.929Z Generate a new keypair... 2024-12-26T09:09:04.180Z + ssh-keygen -t rsa -q -f id_rsa -N ‘’ 2024-12-26T09:09:04.180Z id_rsa already exists. 2024-12-26T09:09:04.430Z Overwrite (y/n)? Traceback (most recent call last): File “lifecycle_script.py”, line 232, in <module> main(args) File “lifecycle_script.py”, line 185, in main ExecuteBashScript(“./utils/gen-keypair-ubuntu.sh”).run() File “lifecycle_script.py”, line 31, in run result.check_returncode() File “/usr/lib/python3.8/subprocess.py”, line 448, in check_returncode raise CalledProcessError(self.returncode, self.args, self.stdout, 2024-12-26T09:09:08.979Z subprocess.CalledProcessError: Command ’[’sudo’, ‘bash’, ‘./utils/gen-keypair-ubuntu.sh’]' returned non-zero ``` when they tried to add a new node as `GENERATE_KEYPAIR=1` when we don't have the contents of `id_rsa.pub` in `authorized_keys`, even when the key pair does exist. * Update 1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/utils/gen-keypair-ubuntu.sh Co-authored-by: mhuguesaws <[email protected]> --------- Co-authored-by: mhuguesaws <[email protected]>
1 parent 08faf98 commit 11934b9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/utils/gen-keypair-ubuntu.sh

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ set -exuo pipefail
44

55
mkdir -p /fsx/ubuntu/.ssh
66
cd /fsx/ubuntu/.ssh
7-
{ test -f id_rsa && grep "^$(cat id_rsa.pub)$" authorized_keys &> /dev/null ; } && GENERATE_KEYPAIR=0 || GENERATE_KEYPAIR=1
7+
8+
# Check if id_rsa exists
9+
if [ ! -f id_rsa ]; then
10+
GENERATE_KEYPAIR=1
11+
else
12+
GENERATE_KEYPAIR=0
13+
# Check if id_rsa.pub exists in authorized_keys
14+
if ! grep -qF "$(cat id_rsa.pub)" authorized_keys 2>/dev/null; then
15+
# If not, add the public key to authorized_keys
16+
cat id_rsa.pub >> authorized_keys
17+
fi
818
if [[ $GENERATE_KEYPAIR == 1 ]]; then
919
echo Generate a new keypair...
10-
ssh-keygen -t rsa -q -f id_rsa -N ""
20+
ssh-keygen -t rsa -b 4096 -q -f id_rsa -N ""
1121
cat id_rsa.pub >> authorized_keys
1222
# Set permissions for the ssh keypair
1323
chmod 600 id_rsa
@@ -19,4 +29,4 @@ if [[ $GENERATE_KEYPAIR == 1 ]]; then
1929
chown ubuntu:ubuntu /fsx/ubuntu/.ssh
2030
else
2131
echo Use existing keypair...
22-
fi
32+
fi

0 commit comments

Comments
 (0)