BinaryONe commited on
Commit
dc8b795
·
1 Parent(s): 1a4023b
Files changed (2) hide show
  1. start.sh +9 -3
  2. start_upgrade.sh +112 -0
start.sh CHANGED
@@ -13,9 +13,6 @@ if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
13
  ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
14
  fi
15
 
16
- echo "* Contents of id_rsa:"
17
- cat /home/admin/.ssh/id_rsa
18
-
19
  # Start SSH service if not running
20
  if ! pgrep -x "sshd" >/dev/null; then
21
  echo "* Starting SSH server on port 2222 *"
@@ -32,6 +29,11 @@ if ! id -u admin >/dev/null 2>&1; then
32
  echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
33
  fi
34
 
 
 
 
 
 
35
  # Generate SSH keys for the 'admin' user if missing
36
  if [ ! -f /home/admin/.ssh/id_rsa ]; then
37
  echo "* Generating SSH keys for 'admin' user *"
@@ -51,6 +53,10 @@ if [ ! -f /home/admin/.ssh/authorized_keys ]; then
51
  chown admin:admin /home/admin/.ssh/authorized_keys
52
  fi
53
 
 
 
 
 
54
  # Print SSH server status
55
  echo "* Status of SSH service:"
56
  netstat -tuln
 
13
  ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
14
  fi
15
 
 
 
 
16
  # Start SSH service if not running
17
  if ! pgrep -x "sshd" >/dev/null; then
18
  echo "* Starting SSH server on port 2222 *"
 
29
  echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
30
  fi
31
 
32
+ # Ensure 'admin' home directory permissions
33
+ echo "* Fixing permissions for 'admin' user *"
34
+ chown -R admin:admin /home/admin
35
+ chmod -R 700 /home/admin
36
+
37
  # Generate SSH keys for the 'admin' user if missing
38
  if [ ! -f /home/admin/.ssh/id_rsa ]; then
39
  echo "* Generating SSH keys for 'admin' user *"
 
53
  chown admin:admin /home/admin/.ssh/authorized_keys
54
  fi
55
 
56
+
57
+ echo "* Contents of id_rsa:"
58
+ cat /home/admin/.ssh/id_rsa
59
+
60
  # Print SSH server status
61
  echo "* Status of SSH service:"
62
  netstat -tuln
start_upgrade.sh ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Function to print the current hostname and user details
4
+ print_host_details() {
5
+ echo "* The hostname of this container is: $(cat /etc/hostname)"
6
+ echo "* The host of this container is: $(cat /etc/hosts)"
7
+ echo "* ID of the user running the script: $(id -u) * Group: $(id -g) * Status of Admin: $(id admin 2>/dev/null || echo 'Admin user not found')"
8
+ }
9
+
10
+ # Function to generate SSH host keys if missing
11
+ generate_ssh_keys() {
12
+ if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
13
+ echo "* Generating SSH host keys *"
14
+ ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" || { echo "Failed to generate RSA key"; exit 1; }
15
+ ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" || { echo "Failed to generate ECDSA key"; exit 1; }
16
+ ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" || { echo "Failed to generate ED25519 key"; exit 1; }
17
+ fi
18
+ }
19
+
20
+ # Function to start SSH service if not running
21
+ start_ssh_service() {
22
+ if ! pgrep -x "sshd" >/dev/null; then
23
+ echo "* Starting SSH server on port 2222 *"
24
+ /usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
25
+ else
26
+ echo "* SSH server is already running *"
27
+ fi
28
+ }
29
+
30
+ # Function to create 'admin' user if missing
31
+ create_admin_user() {
32
+ if ! id -u admin >/dev/null 2>&1; then
33
+ echo "* Creating 'admin' user *"
34
+ useradd -m -s /bin/bash -G sudo admin || { echo "Failed to create admin user"; exit 1; }
35
+ echo "admin:password" | chpasswd || { echo "Failed to set admin password"; exit 1; }
36
+ echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers || { echo "Failed to update sudoers"; exit 1; }
37
+ fi
38
+ }
39
+
40
+ # Function to fix permissions for 'admin' user
41
+ fix_admin_permissions() {
42
+ echo "* Fixing permissions for 'admin' user *"
43
+ chown -R admin:admin /home/admin || { echo "Failed to change ownership"; exit 1; }
44
+ chmod -R 700 /home/admin || { echo "Failed to set permissions"; exit 1; }
45
+ }
46
+
47
+ # Function to generate SSH keys for 'admin' user if missing
48
+ generate_admin_ssh_keys() {
49
+ if [ ! -f /home/admin/.ssh/id_rsa ]; then
50
+ echo "* Generating SSH keys for 'admin' user *"
51
+ sudo -u admin mkdir -p /home/admin/.ssh || { echo "Failed to create .ssh directory"; exit 1; }
52
+ sudo -u admin ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N "" || { echo "Failed to generate admin SSH key"; exit 1; }
53
+ chmod 700 /home/admin/.ssh || { echo "Failed to set .ssh directory permissions"; exit 1; }
54
+ chmod 600 /home/admin/.ssh/id_rsa || { echo "Failed to set private key permissions"; exit 1; }
55
+ chmod 644 /home/admin/.ssh/id_rsa.pub || { echo "Failed to set public key permissions"; exit 1; }
56
+ fi
57
+ }
58
+
59
+ # Function to add public key to authorized_keys for 'admin' user
60
+ add_admin_authorized_keys() {
61
+ if [ ! -f /home/admin/.ssh/authorized_keys ]; then
62
+ echo "* Adding public key to authorized_keys for 'admin' user *"
63
+ sudo -u admin bash -c "cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys" || { echo "Failed to add public key to authorized_keys"; exit 1; }
64
+ chmod 600 /home/admin/.ssh/authorized_keys || { echo "Failed to set authorized_keys permissions"; exit 1; }
65
+ fi
66
+ }
67
+
68
+ # Function to activate virtual environment
69
+ activate_virtual_env() {
70
+ if [ -d "/app/WebSSHEnv" ]; then
71
+ echo "* Activating virtual environment *"
72
+ source /app/WebSSHEnv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
73
+ else
74
+ echo "* Virtual environment not found, please check setup *"
75
+ fi
76
+ }
77
+
78
+ # Function to set working directory
79
+ set_working_directory() {
80
+ cd /app || { echo "Failed to change directory to /app"; exit 1; }
81
+ }
82
+
83
+ # Function to print the contents of the working directory
84
+ print_working_directory_contents() {
85
+ echo "* Contents of /app directory: *"
86
+ ls -la /app
87
+ }
88
+
89
+ # Function to run the WebSSH application
90
+ run_webssh_application() {
91
+ echo "* Starting WebSSH application *"
92
+ python3 -u -m WebSSH || { echo "Failed to start WebSSH application"; exit 1; }
93
+ }
94
+
95
+ # Function to keep the container running
96
+ keep_container_running() {
97
+ tail -f /dev/null
98
+ }
99
+
100
+ # Main script execution
101
+ print_host_details
102
+ generate_ssh_keys
103
+ start_ssh_service
104
+ create_admin_user
105
+ fix_admin_permissions
106
+ generate_admin_ssh_keys
107
+ add_admin_authorized_keys
108
+ activate_virtual_env
109
+ set_working_directory
110
+ print_working_directory_contents
111
+ run_webssh_application
112
+ keep_container_running