Linux shell script to change you ssh port

Tutorial willing 14Views 0 comment

Our vps’s ssh port always scaned by some bad gay and Fail2ban is not enought.
You can change you default port for the security.
But there is many new gay doesn’t know how to change it.
We provide you a one key shell script to change you ssh default now.

# Use default SSH port 22. If you use another SSH port on your server
if [ -e "/etc/ssh/sshd_config" ];then
[ -z "`grep ^Port /etc/ssh/sshd_config`" ] && ssh_port=22 || ssh_port=`grep ^Port /etc/ssh/sshd_config | awk '{print $2}'`
while :; do echo
read -p "Please input SSH port(Default: $ssh_port): " SSH_PORT
[ -z "$SSH_PORT" ] && SSH_PORT=$ssh_port
if [ $SSH_PORT -eq 22 >/dev/null 2>&1 -o $SSH_PORT -gt 1024 >/dev/null 2>&1 -a $SSH_PORT -lt 65535 >/dev/null 2>&1 ];then
echo "${CWARNING}input error! Input range: 22,1025~65534${CEND}"

if [ -z "`grep ^Port /etc/ssh/sshd_config`" -a "$SSH_PORT" != '22' ];then
sed -i "[email protected]^#Port.*@&\nPort [email protected]" /etc/ssh/sshd_config
elif [ -n "`grep ^Port /etc/ssh/sshd_config`" ];then
sed -i "[email protected]^Port.*@Port [email protected]" /etc/ssh/sshd_config

Fill the command in a shell like, and run it.
After this, don’t forget to restart you ssh service.

service sshd restart
This shell script if from OneinStack.

The last thing is don’t forget to add you new port to the firewall to allow it.

Keep link to this when copy:VPS List Agent » Linux shell script to change you ssh port

You must Login to submit a comment