Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to disable root login

Tags:

bash

ubuntu

I'm trying to automate as much as possible in the deployment of servers, the overall goal is to login as a user, upload a bashscript and have it configure my server how I need it configured.

To start I'm trying to disable root login, I know I have to edit /etc/ssh/sshd_config to PermitRootLogin no

I was wondering if there was a way to do this via a bash script?

like image 673
nadermx Avatar asked Feb 15 '26 22:02

nadermx


1 Answers

If you want to just append your text to the sshd_config file, you could use printf like this:

printf '\n%s\n' 'PermitRootLogin no' >>/etc/ssh/sshd_config

or if you want to change a file that already have a value set, but you want to set it to no, could use sed like this:

sed -i '/^PermitRootLogin[ \t]\+\w\+$/{ s//PermitRootLogin no/g; }' /etc/ssh/sshd_config
like image 113
Vikyboss Avatar answered Feb 19 '26 06:02

Vikyboss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!