Last active
March 29, 2023 12:38
-
-
Save WillSams/e8e45ab95f4f10b3075b48d2e3fc94df to your computer and use it in GitHub Desktop.
Configuring SSH & Hostname on Virtual Box VM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# **************** CONFIGURATION FOR LOGGING INTO VM FROM HOST VIA SSH ****************** | |
# | |
# Download and execute this script on your VM, not your host machine | |
# | |
# Pre-req: | |
# - Ensure you have openssh-server installed on the VM. | |
# - Go to File-> Host Network Manager -> Create. DO NOT enable `DHCP Server`. | |
# | |
# In your VM's settings, go to Network tab, add a 2nd network adapter and do the following: | |
# - set `Attached to` as `Host-only adapter` | |
# - set `Name` to the profile you created in Network Manager (for example, vboxnet0) | |
# - set `Promisuous Mode` to `Allow VMs` | |
# On network Adapter 1, set up port forwarding: | |
# Name Protocol Host IP Host Port Guest IP Guest Port | |
# ---- ---------- -------- --------- -------- ---------- | |
# ssh TCP 3022 22 | |
# | |
# To SSH into the VM via port #, ssh -vvvv <username>@localhost -p 3022. | |
# | |
# *************************************************************************************** | |
set -o nounset # unset variables are errors | |
SCRIPTVERSION="2019.01.25-Debian" | |
SCRIPTNAME="ssh_on_vbox_mv.sh" | |
SCRIPTFULLNAME="$0" | |
NEWHOSTNAME="$HOSTNAME-02" | |
NEWIP='192.168.56.101/24' | |
echoerror() { printf "\033[1;31m * ERROR\033[0m: %s\\n" "$@" 1>&2; } | |
usage() { | |
cat << EOT | |
Usage : ${SCRIPTNAME} [options] | |
Options: | |
-h Display this message | |
-v Display script version | |
-n hostname desired for the VM. Default will be current name, $HOSTNAME-02 | |
-i Static IP address assigned. Default is 192.168.56.101. | |
EOT | |
} # ---------- end of function usage ---------- | |
while getopts ':hvn:i:' opt | |
do | |
case "${opt}" in | |
h ) usage; exit 0 ;; | |
v ) echo "$0 -- Version $SCRIPTVERSION"; exit 0 ;; | |
n ) NEWHOSTNAME=$OPTARG ;; | |
i ) NEWIP=$OPTARG ;; | |
\?) echo | |
echoerror "Option does not exist : $OPTARG" | |
usage | |
exit 1 | |
;; | |
esac # --- end of case --- | |
done | |
shift $((OPTIND-1)) | |
# What you'll need to do to get your VMs to see each other if you have Netplan. | |
sudo bash -c "rm /etc/netplan/custom-vbox-init.yaml" | |
sudo bash -c "echo 'network: | |
ethernets: | |
enp0s3: | |
addresses: [] | |
dhcp4: true | |
optional: true | |
enp0s8: | |
addresses: [$NEWIP/24] | |
dhcp4: no | |
dhcp6: no | |
nameservers: | |
addresses: [8.8.8.8, 8.8.4.4] | |
version: 2' >> /etc/netplan/custom-vbox-init.yaml" | |
sudo bash -c "netplan apply custom-vbox-init.yaml" | |
sudo bash -c "rm /etc/hostname" | |
sudo bash -c "echo '$NEWHOSTNAME' >> /etc/hostname" | |
sudo bash -c "echo '127.0.0.1 $NEWHOSTNAME' >> /etc/hosts" | |
sudo bash -c "ufw allow ssh && ufw enable" | |
echo "$SCRIPTFULLNAME ($SCRIPTVERSION) complete. Rebooting server in 5 seconds." | |
echo "If host name did not change after reboot, verify 'preserve_hostname' is set to 'true' in /etc/cloud/cloud.cfg." | |
sleep 5s | |
sudo bash -c "reboot now" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment