0

I have made extensive research about how to setup and secure correctly our new VPS with a Ubuntu 22.04 OS for three weeks, and I have currently a good idea of what has to be done.

However, I would like to build a bash script to make all these numerous and complex tasks automatically with a global script and sub-scripts by subject.

For example, the global script VPS_Setup.sh will launch successively the sub-scripts below:

- VPS_Setup_01_Update_OS.sh
- VPS_Setup_02_Create_New_Sudo_Group.sh
- VPS_Setup_03_Create_New_Sudo_User.sh
- VPS_Setup_04_Adjust_Timezone.sh
- VPS_Setup_05_Adjust_Swapfile.sh
...

Inside VPS_Setup_01_Update_OS.sh, i have simply write the following commands:

apt update -y
apt upgrade -y
apt autoremove -y
apt autoclean -y

After many attempts mainly based on these sources:

I'm launching sub-scripts from the global script by this way:

SSHPASS='mypassword' sshpass -e ssh -tt -o StrictHostKeyChecking=no root@XX.XX.XX.XX 'bash -s' < ./VPS_Setup_01_Update_OS.sh

The sub-script is launched, and I receive no error messages.
However, only the first command is executed.
Then, it stops and display the command prompt.

Here is what is displayed:

Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.2.0 x86_64)
  • Documentation: https://help.ubuntu.com
  • Management: https://landscape.canonical.com
  • Support: https://ubuntu.com/advantage

Last login: Thu Feb 9 19:54:37 2023 from XX.XX.XX.XX root@XXXXXX:~# apt update -y apt upgrade -y apt autoremove -y apt autoclean -y Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease Hit:3 http://archive.canonical.com/ubuntu jammy InRelease Hit:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date. root@XXXXXX:~#

This is strange, as the script displays the four commands before executing only the first one.

Additionally, if I try to execute another command manually from the root prompt, nothing happens, until I press ^C.
Then, it returns to my local prompt.

I've also tried the second way : executing commands directly from the global script, by this way:

SSHPASS='mypassword' sshpass -e ssh -tt -o StrictHostKeyChecking=no root@XX.XX.XX.XX <<ENDSSH1
  apt update -y
  apt upgrade -y
  apt autoremove -y
  apt autoclean -y
ENDSSH1

Exactly same result!

Question 1: What am I doing wrong and how should I proceed ?
Question 2: Do you know a place where I could find this kind of scripts, that I could adapt to our needs ? I've searched for, but didn't find anything...

1 Answers1

0

Ok, I found my mistake: the script file was created under Windows, so end of lines weren't interpreted correctly! Bash scripting files should ALWAYS be created on a Linux system...