/ tools / ensure-ssh-boot.sh
ensure-ssh-boot.sh
 1  #!/bin/bash
 2  # Ensure SSH starts on boot and configure swap
 3  
 4  echo "=== Ensuring SSH service starts on boot ==="
 5  
 6  # Check which SSH service is installed (ssh or sshd)
 7  if systemctl list-unit-files | grep -q "^ssh.service"; then
 8      SSH_SERVICE="ssh"
 9  elif systemctl list-unit-files | grep -q "^sshd.service"; then
10      SSH_SERVICE="sshd"
11  else
12      echo "ERROR: No SSH service found"
13      exit 1
14  fi
15  
16  echo "SSH service: $SSH_SERVICE"
17  
18  # Enable SSH service
19  sudo systemctl enable $SSH_SERVICE
20  echo "✓ $SSH_SERVICE enabled to start on boot"
21  
22  # Check current status
23  sudo systemctl is-enabled $SSH_SERVICE
24  sudo systemctl status $SSH_SERVICE --no-pager | head -3
25  
26  echo ""
27  echo "=== Configuring Swap Memory ==="
28  
29  # Check if swap already exists
30  if swapon --show | grep -q /swapfile; then
31      echo "✓ Swap already configured"
32      swapon --show
33      free -h | grep -E "Mem:|Swap:"
34      exit 0
35  fi
36  
37  # Create 32GB swap file
38  echo "Creating 32GB swap file..."
39  sudo fallocate -l 32G /swapfile
40  sudo chmod 600 /swapfile
41  sudo mkswap /swapfile
42  sudo swapon /swapfile
43  
44  # Make swap persistent
45  if ! grep -q "/swapfile" /etc/fstab; then
46      echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
47  fi
48  
49  # Set swappiness to 10
50  sudo sysctl vm.swappiness=10
51  if ! grep -q "vm.swappiness" /etc/sysctl.conf; then
52      echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
53  fi
54  
55  echo "✓ Swap configured: 32GB, swappiness=10"
56  echo ""
57  echo "Status:"
58  swapon --show
59  free -h | grep -E "Mem:|Swap:"