Skip to content

Commit b0fa7fd

Browse files
committed
script to enable sshd when it is not enabled by default
1 parent 619958c commit b0fa7fd

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# # Script to enable ssh connections,
4+
# # on EC2 instances where they are disabled by default
5+
# # (e.g., with JPL AMIs).
6+
# #
7+
# # Once this script runs successfully,
8+
# # it should be possible to login to the instance using ssh, e.g.:
9+
#
10+
# ssh -i "~/.ssh/your_key_pair.pem" jpluser@your_private_ip_address
11+
12+
13+
# Become root
14+
sudo -s
15+
16+
# Try to enable sshd
17+
systemctl enable sshd
18+
if [ $? -eq 0 ]; then
19+
echo "Enabled sshd successfully"
20+
else
21+
if [ $( readlink -f /etc/systemd/system/sshd.service) = "/dev/null" ]; then
22+
# Delete this /dev/null symlink
23+
rm -f /etc/systemd/system/sshd.service
24+
echo 'Deleted symlink to /dev/null'
25+
26+
# Re-try enabling sshd
27+
systemctl enable sshd
28+
if [ $? -eq 0] ; then
29+
echo "Enabled sshd successfully"
30+
else
31+
echo "Error: symlink deletion did not allow sshd to be enabled"
32+
exit 1
33+
else
34+
echo "Error: sshd not enabled successfully"
35+
fi
36+
37+
# Create symlink to the service (if it does not already exist)
38+
ln -s /etc/systemd/system/multi-user.target.wants/sshd.service /usr/lib/systemd/system/sshd.service
39+
echo "Created symlink to sshd.service"
40+
41+
# create new ssh keys
42+
ssh-keygen -q -N "" -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key
43+
echo "Created new ssh keys"
44+
45+
# start sshd service
46+
systemctl start sshd

0 commit comments

Comments
 (0)