-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_host_setup.sh
More file actions
56 lines (41 loc) · 1.44 KB
/
ssh_host_setup.sh
File metadata and controls
56 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Author: Michael Brown
# Description: Configures SSH access on the host machine using a transferred public key.
# Install OpenSSH server if not already installed
# sudo apt install openssh-server
set -e # Exit on error
set -u # Treat unset variables as errors
echo "🔍 Checking for required tools..."
for cmd in ssh cat chmod mkdir; do
if ! command -v $cmd &> /dev/null; then
echo "❌ Required command '$cmd' not found. Please install it before proceeding."
exit 1
fi
done
echo "✅ All required tools are available."
KEY_DIR="$HOME/.ssh"
AUTHORIZED_KEYS="$KEY_DIR/authorized_keys"
TEMP_KEY="$HOME/temp_key.pub"
echo "🔐 Setting up authorized_keys..."
# Ensure .ssh directory exists with correct permissions
mkdir -p "$KEY_DIR"
chmod 700 "$KEY_DIR"
# Validate temp_key.pub exists
if [ ! -f "$TEMP_KEY" ]; then
echo "❌ Public key file '$TEMP_KEY' not found. Make sure it was transferred correctly."
exit 1
fi
# Create authorized_keys if it doesn't exist
touch "$AUTHORIZED_KEYS"
chmod 600 "$AUTHORIZED_KEYS"
# Append key safely
if grep -q -F "$(cat "$TEMP_KEY")" "$AUTHORIZED_KEYS"; then
echo "⚠️ Key already exists in authorized_keys. Skipping append."
else
cat "$TEMP_KEY" >> "$AUTHORIZED_KEYS"
echo "✅ Public key added to authorized_keys."
fi
# Clean up
rm "$TEMP_KEY"
echo "🧹 Removed temporary key file."
echo "✅ SSH key installed. You can now connect from the client machine."