Automating SSH Service With Bash Script

Recently, I set up a new virtual machine on my laptop and, as usual, encountered the hassle of enabling the SSH server. Tired of repeating this process, I decided to craft a Bash script that would handle everything from installing the server to starting or restarting it. To achieve this, I used vi editor to create a script that simplifies the entire procedure.

First, check if ssh is running with sudo systemctl status ssh command.

To kick off our script, it’s crucial to include the shebang at the script’s beginning. This small but significant line informs the system that the script should be executed using the Bash shell.

Using if statement, the script will first check if the SSH server is already installed on the system. If not, it will proceed to install it using the following command:

if ! command -v ssh &> /dev/null; then
sudo apt-get install -y openssh-server
fi

Once it is done. Make bash script to be executable with chmod command.

Once it is executable, run the bash script. In order to excute, the path should be in the same directory.

To confirm the SSH server is up and running, perform a quick status check.

Leave a Reply

Your email address will not be published. Required fields are marked *