Skip to content

Commit 6e396be

Browse files
authored
Create setup_ssh.sh
1 parent b420dcb commit 6e396be

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

Linux/setup_ssh.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env bash
2+
3+
#----------------------------------------------------------------------------#
4+
#Usage: bash <(curl -qfsSL "https://raw.githubusercontent.com/pkgforgde/devscripts/main/misc/Linux/setup_ssh.sh")
5+
#Force: curl -qfsSL "https://raw.githubusercontent.com/pkgforgde/devscripts/main/misc/Linux/setup_ssh.sh" | bash -s -- -f
6+
#----------------------------------------------------------------------------#
7+
8+
#----------------------------------------------------------------------------#
9+
# For debug
10+
#set -x
11+
#A bit of Styling
12+
RED='\033[31m'
13+
GREEN='\033[32m'
14+
DGREEN='\033[38;5;28m'
15+
GREY='\033[37m'
16+
BLUE='\033[34m'
17+
YELLOW='\033[33m'
18+
PURPLE='\033[35m'
19+
PINK='\033[38;5;206m'
20+
VIOLET='\033[0;35m'
21+
RESET='\033[0m'
22+
NC='\033[0m'
23+
#----------------------------------------------------------------------------#
24+
25+
#----------------------------------------------------------------------------#
26+
#ENV:VARS
27+
export BINARY_ROOT_DIR="/usr/local/bin"
28+
export BINARY_HOME_DIR="${HOME}/bin"
29+
sudo mkdir -p "${BINARY_ROOT_DIR}" || mkdir -p "${BINARY_HOME_DIR}"
30+
#----------------------------------------------------------------------------#
31+
32+
#----------------------------------------------------------------------------#
33+
# Parse command line arguments
34+
#def
35+
force_setup=
36+
#if
37+
while [[ $# -gt 0 ]]; do
38+
key="$1"
39+
case $key in
40+
-f|--force)
41+
force_setup=1
42+
shift
43+
;;
44+
esac
45+
done
46+
#----------------------------------------------------------------------------#
47+
#Auxiliaries
48+
#scp
49+
install_scp()
50+
{
51+
sudo curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/scp" -o "${BINARY_ROOT_DIR}/scp" ||\
52+
curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/scp" -o "${BINARY_HOME_DIR}/ssh"
53+
sudo chmod +xwr "${BINARY_ROOT_DIR}/scp" || chmod +xwr "${BINARY_HOME_DIR}/scp"
54+
}
55+
export -f install_scp
56+
if ! command -v scp >/dev/null 2>&1; then
57+
echo -e "\n [+]${DGREEN}Installing ${PURPLE}scp${NC}\n"
58+
install_scp
59+
elif [ -n "${force_setup}" ] ; then
60+
echo -e "\n [+]${PINK}Force ${DGREEN}Installing ${PURPLE}scp${NC}\n"
61+
install_scp
62+
fi
63+
#sftp
64+
install_sftp()
65+
{
66+
sudo curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/sftp" -o "${BINARY_ROOT_DIR}/sftp" ||\
67+
curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/sftp" -o "${BINARY_HOME_DIR}/sftp"
68+
sudo chmod +xwr "${BINARY_ROOT_DIR}/sftp" || chmod +xwr "${BINARY_HOME_DIR}/sftp"
69+
}
70+
export -f install_sftp
71+
if ! command -v sftp >/dev/null 2>&1; then
72+
echo -e "\n [+]${DGREEN}Installing ${PURPLE}sftp${NC}\n"
73+
install_sftp
74+
elif [ -n "${force_setup}" ] ; then
75+
echo -e "\n [+]${PINK}Force ${DGREEN}Installing ${PURPLE}sftp${NC}\n"
76+
install_sftp
77+
fi
78+
#SSH
79+
install_ssh()
80+
{
81+
sudo curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/ssh" -o "${BINARY_ROOT_DIR}/ssh" ||\
82+
curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/ssh" -o "${BINARY_HOME_DIR}/ssh"
83+
sudo chmod +xwr "${BINARY_ROOT_DIR}/ssh" || chmod +xwr "${BINARY_HOME_DIR}/ssh"
84+
}
85+
export -f install_ssh
86+
if ! command -v ssh >/dev/null 2>&1; then
87+
echo -e "\n [+]${DGREEN}Installing ${PURPLE}ssh${NC}\n"
88+
install_ssh
89+
elif [ -n "${force_setup}" ] ; then
90+
echo -e "\n [+]${PINK}Force ${DGREEN}Installing ${PURPLE}ssh${NC}\n"
91+
#Remove old
92+
sudo rm "$(which ssh)" 2>/dev/null ; sudo rm "$(which ssh)" 2>/dev/null
93+
install_ssh
94+
fi
95+
#ssh-keyscan
96+
install_ssh_keyscan()
97+
{
98+
sudo curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/ssh-keyscan" -o "${BINARY_ROOT_DIR}/ssh-keyscan" ||\
99+
curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/ssh-keyscan" -o "${BINARY_HOME_DIR}/ssh-keyscan"
100+
sudo chmod +xwr "${BINARY_ROOT_DIR}/ssh-keyscan" || chmod +xwr "${BINARY_HOME_DIR}/ssh-keyscan"
101+
}
102+
export -f install_ssh_keyscan
103+
if ! command -v ssh-keyscan >/dev/null 2>&1; then
104+
echo -e "\n [+]${DGREEN}Installing ${PURPLE}ssh-keyscan${NC}\n"
105+
install_ssh_keyscan
106+
elif [ -n "${force_setup}" ] ; then
107+
echo -e "\n [+]${PINK}Force ${DGREEN}Installing ${PURPLE}ssh-keyscan${NC}\n"
108+
install_ssh_keyscan
109+
fi
110+
#----------------------------------------------------------------------------#
111+
112+
#----------------------------------------------------------------------------#
113+
#Get Config
114+
sudo mkdir -p "/etc/ssh/" || mkdir -p "${HOME}/.ssh"
115+
sudo curl -qfsSL "https://raw.githubusercontent.com/pkgforge/devscripts/refs/heads/main/Linux/sshd_config_passwordless" -o "/etc/ssh/sshd_config" ||\
116+
curl -qfsSL "https://raw.githubusercontent.com/pkgforge/devscripts/refs/heads/main/Linux/sshd_config_passwordless" -o "${HOME}/.ssh/sshd_config"
117+
#Enable X11Forwarding
118+
sudo sed -e 's/^X11Forwarding no/X11Forwarding yes/' -i "/etc/ssh/sshd_config" ||\
119+
sed -e 's/^X11Forwarding no/X11Forwarding yes/' -i "${HOME}/.ssh/sshd_config"
120+
##PasswordAuthentication yes
121+
# sudo sed -e '/^#PasswordAuthentication/s/^#//' -i "/etc/ssh/sshd_config" || sed -e '/^#PasswordAuthentication/s/^#//' -i "${HOME}/.ssh/sshd_config"
122+
#----------------------------------------------------------------------------#
123+
124+
#----------------------------------------------------------------------------#
125+
#ssh-keygen
126+
create_host_keys()
127+
{
128+
sudo curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/ssh-keygen" -o "${BINARY_ROOT_DIR}/ssh-keygen" ||\
129+
curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/ssh-keygen" -o "${BINARY_HOME_DIR}/ssh-keygen"
130+
sudo chmod +xwr "${BINARY_ROOT_DIR}/ssh-keygen" || chmod +xwr "${BINARY_HOME_DIR}/ssh-keygen"
131+
#Generate-Keys
132+
# dsa
133+
echo "yes" | sudo ssh-keygen -N "" -t dsa -f "/etc/ssh/ssh_host_dsa_key" ||\
134+
echo "yes" | ssh-keygen -N "" -t dsa -f "${HOME}/.ssh/ssh_host_dsa_key"
135+
# ecdsa
136+
echo "yes" | sudo ssh-keygen -N "" -t ecdsa -b 521 -f "/etc/ssh/ssh_host_ecdsa_key" ||\
137+
echo "yes" | ssh-keygen -N "" -t ecdsa -b 521 -f "${HOME}/.ssh/ssh_host_ecdsa_key"
138+
# ed25519
139+
echo "yes" | sudo ssh-keygen -N "" -t ed25519 -f "/etc/ssh/ssh_host_ed25519_key" ||\
140+
echo "yes" | ssh-keygen -N "" -t ed25519 -f "${HOME}/.ssh/ssh_host_ed25519_key"
141+
# creates id_rsa (ssh_host_rsa_key) & id_rsa.pub (ssh_host_rsa_key.pub)
142+
echo "yes" | sudo ssh-keygen -N "" -t rsa -b 4096 -f "/etc/ssh/ssh_host_rsa_key" ||\
143+
echo "yes" | ssh-keygen -N "" -t rsa -b 4096 -f "${HOME}/.ssh/ssh_host_rsa_key"
144+
}
145+
export -f create_host_keys
146+
if ! command -v ssh-keygen >/dev/null 2>&1; then
147+
echo -e "\n [+]${DGREEN}Installing ${PURPLE}ssh-keygen${NC}\n"
148+
create_host_keys
149+
elif [ -n "${force_setup}" ] ; then
150+
echo -e "\n [+]${PINK}Force ${DGREEN}Installing ${PURPLE}ssh-keygen${NC}\n"
151+
create_host_keys
152+
fi
153+
#----------------------------------------------------------------------------#
154+
155+
#----------------------------------------------------------------------------#
156+
#sshd
157+
install_sshd(){
158+
#Install
159+
sudo curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/sshd" -o "${BINARY_ROOT_DIR}/sshd" ||\
160+
curl -qfsSL "https://bin.pkgforge.dev/$(uname -m)/sshd" -o "${BINARY_HOME_DIR}/sshd"
161+
sudo chmod +xwr "${BINARY_ROOT_DIR}/sshd" || chmod +xwr "${BINARY_HOME_DIR}/sshd"
162+
#Symlink
163+
sudo ln -s "/usr/local/bin/ssh" "/usr/bin/ssh" 2>/dev/null ; sudo ln -s "/usr/local/bin/sshd" "/usr/bin/sshd" 2>/dev/null
164+
}
165+
export -f install_sshd
166+
if ! command -v sshd >/dev/null 2>&1; then
167+
echo -e "\n [+]${DGREEN}Installing ${PURPLE}sshd${NC}\n"
168+
install_sshd
169+
elif [ -n "${force_setup}" ] ; then
170+
echo -e "\n [+]${PINK}Force ${DGREEN}Installing ${PURPLE}sshd${NC}\n"
171+
#Remove old
172+
sudo rm "$(which sshd)" 2>/dev/null ; sudo rm "$(which sshd)" 2>/dev/null
173+
install_sshd
174+
fi
175+
#----------------------------------------------------------------------------#
176+
# Ref: https://linux.die.net/man/8/sshd
177+
# -4 --> Forces sshd to use IPv4 addresses only.
178+
# -6 --> Forces sshd to use IPv6 addresses only.
179+
# -D --> Doesn't detach to become a daemon
180+
# -d --> debug mode
181+
# -f --> $PATH_TO_sshd_config [ Default: /etc/ssh/sshd_config ]
182+
# -h --> $PATH_TO_host_key_file [ Default: /etc/ssh/ssh_host_key | /etc/ssh/ssh_host_rsa_key | /etc/ssh/ssh_host_dsa_key ]
183+
# -o --> Directly specify config, formatted as sshd config file format
184+
# -q --> Quiet Mode,, no logs
185+
#Only needed for root
186+
sudo mkdir -p "/var/empty" 2>/dev/null
187+
#Start
188+
sudo "$(which sshd)" -f "/etc/ssh/sshd_config" -h "/etc/ssh/ssh_host_rsa_key" -p "22" ||\
189+
"$(which sshd)" -f "${HOME}/.ssh/sshd_config" -h "${HOME}/.ssh/ssh_host_rsa_key" -p "22"
190+
#Echo
191+
echo -e "\n [+] ${BLUE}SSHD${NC}\n"
192+
ps -aux | grep -i "sshd"
193+
pgrep -f "sshd"
194+
#Kill
195+
# sudo pgrep -f "sshd" | xargs sudo kill -9 2>/dev/null
196+
#----------------------------------------------------------------------------#
197+
#EOF
198+
#----------------------------------------------------------------------------#

0 commit comments

Comments
 (0)