Skip to content

Instantly share code, notes, and snippets.

@KAnggara75
Created February 9, 2025 10:33
Show Gist options
  • Save KAnggara75/f1f6e6a951680119eef607ee7eecc52f to your computer and use it in GitHub Desktop.
Save KAnggara75/f1f6e6a951680119eef607ee7eecc52f to your computer and use it in GitHub Desktop.
Change SSH Port
#!/bin/bash
abort() {
echo "$@"
exit 1
}
check_user() {
if [ "$(id -u)" -ne 0 ]; then
abort "Harap jalankan script sebagai root atau dengan sudo."
fi
}
check_port() {
read -p "Masukkan port SSH baru: " NEW_PORT
if ! [[ "$NEW_PORT" =~ ^[0-9]+$ ]] || [ "$NEW_PORT" -lt 1024 ] || [ "$NEW_PORT" -gt 65535 ]; then
abort "Port tidak valid. Gunakan angka antara 1024 - 65535."
fi
if ss -tuln | grep -q ":$NEW_PORT "; then
abort "Port $NEW_PORT sudah digunakan oleh layanan lain. Pilih port lain."
fi
}
change_port() {
sudo apt install openssh-server -y
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo chmod a-w /etc/ssh/sshd_config.bak
sed -i "s/^#Port 22/Port $NEW_PORT/" /etc/ssh/sshd_config
sed -i "s/^Port [0-9]*/Port $NEW_PORT/" /etc/ssh/sshd_config
}
apply() {
sudo systemctl restart ssh
sudo service ssh restart
sudo systemctl status ssh
}
main() {
check_user
check_port
change_port
apply
}
main || abort "Fail"
@KAnggara75
Copy link
Author

/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/KAnggara75/f1f6e6a951680119eef607ee7eecc52f/raw/e2b50daeb18b88dff81552b50218803262dda44f/change_ssh_port.sh)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment