-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-ssh.sh
More file actions
executable file
·53 lines (46 loc) · 1.53 KB
/
setup-ssh.sh
File metadata and controls
executable file
·53 lines (46 loc) · 1.53 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
generate_rsa_key() {
# Set default values for key options
local key_dir="$HOME/.ssh"
local key_name="id_rsa"
local key_bits=4096
local key_comment="Generated by $(whoami)@$(hostname)"
# Prompt the user for input
echo "RSA Key Generation"
read -p "Enter directory to save the key (default: $key_dir): " user_dir
read -p "Enter key name (default: $key_name): " user_name
read -p "Enter key size (default: $key_bits): " user_bits
read -p "Enter a comment for the key (default: '$key_comment'): " user_comment
# Override defaults with user input if provided
key_dir=${user_dir:-$key_dir}
key_name=${user_name:-$key_name}
key_bits=${user_bits:-$key_bits}
key_comment=${user_comment:-$key_comment}
# Full key path
local key_path="$key_dir/$key_name"
# Ensure the directory exists
if [ ! -d "$key_dir" ]; then
echo "Creating directory $key_dir..."
mkdir -p "$key_dir"
chmod 700 "$key_dir"
fi
# Check if a key already exists
if [ -f "$key_path" ]; then
echo "A key already exists at $key_path."
read -p "Do you want to overwrite it? (y/n): " overwrite
if [[ "$overwrite" != "y" ]]; then
echo "Aborting key generation."
return 1
fi
fi
# Generate the RSA key
echo "Generating RSA key..."
ssh-keygen -t rsa -b "$key_bits" -C "$key_comment" -f "$key_path" -q -N ""
if [ $? -eq 0 ]; then
echo "RSA key pair generated successfully!"
echo "Private key: $key_path"
echo "Public key: ${key_path}.pub"
else
echo "Failed to generate RSA key."
return 1
fi
}