|
1 | 1 | #!/bin/bash |
2 | 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.: |
| 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 | 9 | # |
10 | | -# ssh -i "~/.ssh/your_key_pair.pem" jpluser@your_private_ip_address |
| 10 | +# $ ssh -i "~/.ssh/key_pair.pem" jpluser@private_ip_address |
| 11 | + |
11 | 12 |
|
12 | 13 |
|
13 | | -# Become root |
14 | | -sudo -s |
15 | | - |
16 | 14 | # Try to enable sshd |
17 | 15 | systemctl enable sshd |
18 | 16 | if [ $? -eq 0 ]; then |
|
25 | 23 |
|
26 | 24 | # Re-try enabling sshd |
27 | 25 | systemctl enable sshd |
28 | | - if [ $? -eq 0] ; then |
| 26 | + if [ $? -eq 0 ] ; then |
29 | 27 | echo "Enabled sshd successfully" |
30 | 28 | else |
31 | 29 | echo "Error: symlink deletion did not allow sshd to be enabled" |
32 | 30 | exit 1 |
| 31 | + fi |
33 | 32 | else |
34 | 33 | echo "Error: sshd not enabled successfully" |
| 34 | + fi |
35 | 35 | fi |
36 | 36 |
|
37 | 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" |
| 38 | +if [ ! -f /usr/lib/systemd/system/sshd.service ]; then |
| 39 | + ln -s /etc/systemd/system/multi-user.target.wants/sshd.service /usr/lib/systemd/system/sshd.service |
| 40 | + echo "Created symlink to sshd.service" |
40 | 41 |
|
41 | 42 | # create new ssh keys |
42 | 43 | ssh-keygen -q -N "" -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key |
43 | 44 | echo "Created new ssh keys" |
44 | 45 |
|
45 | 46 | # start sshd service |
46 | 47 | systemctl start sshd |
| 48 | +echo "Started sshd" |
| 49 | +echo "Now you can login to your instance using ssh, e.g.:" |
| 50 | +echo '$ ssh -i "~/.ssh/your_key_pair.pem" jpluser@private_ip_address' |
| 51 | + |
| 52 | + |
| 53 | +# move git repo to ssh user's directory and change ownership (if requested) |
| 54 | +read -p 'Move ECCO-v4-Python-Tutorial repo to different user? [Y/N] ' move_opt |
| 55 | +if [ $move_opt == "Y"] || [ $move_opt == "y" ]; then |
| 56 | + read -p 'User name of new owner [jpluser for JPL]: ' ssh_user |
| 57 | + cd /home |
| 58 | + mv ./ssm-user/ECCO-v4-Python-Tutorial ./${ssh_user}/ |
| 59 | + echo "Moved ECCO-v4-Python-Tutorial repo to /home/${ssh_user}/" |
| 60 | + chown -R ${ssh_user}:${ssh_user} ./{ssh_user}/ECCO-v4-Python-Tutorial |
| 61 | + echo "Changed owner and group of git repo to ${ssh_user}" |
| 62 | +fi |
0 commit comments