I’m newbie to linux and I’m trying to make an initial configuration script for basic users passwords, SSH rules and system config of Hostname & timezone as below,
1 #!/bin/bash 2 # Login as Root 3 sudo su - 4 # Set Root password 5 echo -e "dumppasswordndumppassword" | passwd root 6 # Set default user ubuntu password 7 echo -e "dumppasswordsndumppassword" | passwd ubuntu 8 # SSH configuration 9 # Permit SSH password Authentication 10 # Make a copy of sshd_config file with suffix in _backup_YYYY-MM-DD format 11 # Comment out PasswordAuthentication no 12 sudo sed -i_backup_`date +%F` -e '/^PasswordAuthentication no/ s/^#*/#/' /etc/ssh/sshd_config 13 # Add PasswordAuthentication yes after the commented line #PasswordAuthentication no 14 sudo sed -i '/^#PasswordAuthentication no/a PasswordAuthentication yes' /etc/ssh/sshd_config 15 # Permit SSH Root login 16 # Comment out PermitRootLogin prohibit-password 17 sudo sed -i -e '/^PermitRootLogin prohibit-password/ s/^#*/#/' /etc/ssh/sshd_config 18 # Add PermitRootLogin yes after the commented line #PermitRootLogin prohibit-password 19 sudo sed -i '/^#PermitRootLogin prohibit-password/a PermitRootLogin yes' /etc/ssh/sshd_config 20 # Restart SSh service 21 sudo service ssh restart 22 # Set timezone 23 sudo timedatectl set-timezone Africa/Cairo 24 # Set Hostname 25 # Set the current hostname variable 26 CUR_HOSTNAME=$(cat /etc/hostname) 27 # Set the desired new hostname 28 NEW_HOSTNAME="DufDuf" 29 # Make changes 30 sudo sed -i 's/^"$CUR_HOSTNAME"/"$NEW_HOSTNAME"/' /etc/hostname 31 sudo sed -i 's/^"$CUR_HOSTNAME"/"$NEW_HOSTNAME"/' /etc/hosts 32 # Update Repositories 33 sudo apt-get update 34 sudo apt update
But I’m getting below errors, I couldn’t figure it out.
'u: invalid option -- ' Usage: su [options] [LOGIN] Options: -c, --command COMMAND pass COMMAND to the invoked shell -h, --help display this help message and exit -, -l, --login make the shell a login shell -m, -p, --preserve-environment do not reset environment variables, and keep the same shell -s, --shell SHELL use SHELL instead of the default in passwd ' does not existot ' does not existuntu : No such file or directoryhd_config : No such file or directoryhd_config : No such file or directoryhd_config : No such file or directoryhd_config * Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart|status} Failed to set time zone: Invalid time zone 'Africa/Cairo ' : No such file or directoryme : No such file or directory E: Invalid operation update E: Invalid operation update
Anyone could help me plz
Thanks,
Advertisement
Answer
I’ve found this out after long search. The problem is, executing the script with root won’t work with
sudo su -
Instead, below code force script to run in a privileged mode
# Force script to run as Root if [ $EUID != 0 ]; then sudo bash "$0" "$@"; exit "$?"; fi # For test, run whoami, expected result is root. echo `whoami`