feat(sshd-setup): multi-distro support and verification steps
- sshd-setup.sh: Auto-detect OS (Debian/Ubuntu/Fedora/RHEL/CentOS) - Use appropriate package manager (apt-get vs dnf) - Add verification steps after each major phase - Exit with error if sshd installation fails - Exit with error if sshd doesn't start successfully - Add troubleshooting section in output - kugetsu-install.sh: Add verification that kugetsu binary exists - kugetsu-setup.md: Document multi-distro installation commands
This commit is contained in:
@@ -7,12 +7,44 @@ echo "=== kugetsu SSH Setup ==="
|
||||
echo "Target user: $USERNAME"
|
||||
echo ""
|
||||
|
||||
detect_os() {
|
||||
if [ -f /etc/os-release ]; then
|
||||
. /etc/os-release
|
||||
case "$ID" in
|
||||
debian|ubuntu|"noble"|"jammy"|"focal"|"bionic"|"bullseye"|"bookworm"|"trixie"|"sid")
|
||||
echo "debian"
|
||||
;;
|
||||
fedora|rhel|centos|rocky|alma)
|
||||
echo "fedora"
|
||||
;;
|
||||
*)
|
||||
echo "unknown"
|
||||
;;
|
||||
esac
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
OS_TYPE=$(detect_os)
|
||||
echo "Detected OS: $OS_TYPE"
|
||||
|
||||
if ! command -v systemctl &> /dev/null; then
|
||||
echo "ERROR: systemd not found."
|
||||
echo ""
|
||||
echo "This script requires systemd to be installed and running inside the container."
|
||||
echo "Please install systemd first:"
|
||||
echo " apt-get update && apt-get install -y systemd"
|
||||
case "$OS_TYPE" in
|
||||
debian)
|
||||
echo " apt-get update && apt-get install -y systemd"
|
||||
;;
|
||||
fedora)
|
||||
echo " dnf install -y systemd"
|
||||
;;
|
||||
*)
|
||||
echo " Install systemd using your package manager"
|
||||
;;
|
||||
esac
|
||||
echo ""
|
||||
echo "If you are running in a container that doesn't support systemd, consider:"
|
||||
echo " - Using a container image with systemd support"
|
||||
@@ -20,13 +52,36 @@ if ! command -v systemctl &> /dev/null; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "[1/6] Updating package lists..."
|
||||
apt-get update -qq
|
||||
echo ""
|
||||
echo "=== Step 1: Install openssh-server ==="
|
||||
case "$OS_TYPE" in
|
||||
debian)
|
||||
echo "Using apt-get (Debian/Ubuntu)..."
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq openssh-server sudo
|
||||
;;
|
||||
fedora)
|
||||
echo "Using dnf (Fedora/RHEL)..."
|
||||
dnf install -y -q openssh-server sudo
|
||||
;;
|
||||
*)
|
||||
echo "ERROR: Unsupported OS. Please install openssh-server and sudo manually."
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
echo "[2/6] Installing openssh-server..."
|
||||
apt-get install -y -qq openssh-server sudo
|
||||
echo ""
|
||||
echo "=== Step 2: Verify installation ==="
|
||||
if ! command -v sshd &> /dev/null; then
|
||||
echo "ERROR: sshd installation failed."
|
||||
echo "Please verify openssh-server was installed correctly."
|
||||
exit 1
|
||||
fi
|
||||
echo "sshd binary: $(which sshd)"
|
||||
echo "sshd version: $(sshd -V 2>&1 | head -1)"
|
||||
|
||||
echo "[3/6] Creating user '$USERNAME' if not exists..."
|
||||
echo ""
|
||||
echo "=== Step 3: Create user '$USERNAME' ==="
|
||||
if ! id "$USERNAME" &> /dev/null; then
|
||||
useradd -m -s /bin/bash "$USERNAME"
|
||||
echo "User '$USERNAME' created."
|
||||
@@ -34,27 +89,40 @@ else
|
||||
echo "User '$USERNAME' already exists."
|
||||
fi
|
||||
|
||||
echo "[4/6] Configuring SSH for key-only authentication..."
|
||||
echo ""
|
||||
echo "=== Step 4: Configure SSH for key-only authentication ==="
|
||||
SSHD_CONFIG="/etc/ssh/sshd_config"
|
||||
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' "$SSHD_CONFIG"
|
||||
sed -i 's/^#*PubkeyAuthentication.*/PubkeyAuthentication yes/' "$SSHD_CONFIG"
|
||||
sed -i 's/^#*PermitRootLogin.*/PermitRootLogin no/' "$SSHD_CONFIG"
|
||||
echo "SSH configured: key-only auth, root login disabled."
|
||||
|
||||
echo "[5/6] Configuring sudo for passwordless access..."
|
||||
echo ""
|
||||
echo "=== Step 5: Configure sudo for passwordless access ==="
|
||||
SUDOERS_FILE="/etc/sudoers.d/$USERNAME"
|
||||
echo "$USERNAME ALL=(ALL) NOPASSWD: ALL" > "$SUDOERS_FILE"
|
||||
chmod 0440 "$SUDOERS_FILE"
|
||||
echo "Sudo configured: $USERNAME can run sudo without password."
|
||||
|
||||
echo "[6/6] Enabling and starting sshd..."
|
||||
echo ""
|
||||
echo "=== Step 6: Enable and start sshd ==="
|
||||
systemctl enable sshd
|
||||
systemctl restart sshd
|
||||
|
||||
sleep 1
|
||||
|
||||
echo ""
|
||||
echo "=== Step 7: Verify sshd is running ==="
|
||||
if systemctl is-active --quiet sshd; then
|
||||
echo "sshd is running."
|
||||
echo "SUCCESS: sshd is running."
|
||||
echo "Status:"
|
||||
systemctl status sshd --no-pager | head -5
|
||||
else
|
||||
echo "WARNING: sshd may not have started correctly. Check with: systemctl status sshd"
|
||||
echo "ERROR: sshd is not running."
|
||||
echo "Debug info:"
|
||||
systemctl status sshd --no-pager
|
||||
journalctl -u sshd -n 10 --no-pager
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
@@ -72,8 +140,14 @@ echo ""
|
||||
echo "2. Connect from remote:"
|
||||
echo " ssh -p 2222 $USERNAME@<container-host-ip>"
|
||||
echo ""
|
||||
echo " (Requires host-side port forwarding - see docs/kugetsu-setup.md)"
|
||||
echo ""
|
||||
echo "3. Verify SSH access:"
|
||||
echo " ssh -p 2222 $USERNAME@<container-host-ip> sudo systemctl status sshd"
|
||||
echo ""
|
||||
echo "=== Troubleshooting ==="
|
||||
echo ""
|
||||
echo "If SSH connection fails:"
|
||||
echo " - Check sshd is running: systemctl status sshd"
|
||||
echo " - Check sshd logs: journalctl -u sshd -n 20"
|
||||
echo " - Verify user exists: id $USERNAME"
|
||||
echo " - Verify SSH key was added: cat /home/$USERNAME/.ssh/authorized_keys"
|
||||
echo ""
|
||||
Reference in New Issue
Block a user