quick setup vm (docker container running debian) and setup ssh key passwordless login
### SETUP DOCKER AS VM (ONLY 30MB IN SIZE)
#https://www.youtube.com/watch?v=GicWz2OF0sk
#create docker file
nano Dockerfile #add the following within the ==== to the file
========================
FROM bitnami/minideb
# Install required system packages
RUN apt-get update
RUN apt-get -y install net-tools inetutils-ping curl software-properties-common nano vim ssh sudo
# Add a new user "ansible" with user id 8877
RUN useradd -u 8877 ansible
# Set defualt user
#USER ansible
USER root
# Set root password
RUN echo 'root:root' | chpasswd
# Comfirm password is set
RUN cat /etc/shadow | grep root
# Add trush authorized_keys (to enable passwordless login)
ADD ./authorized_keys /root/.ssh/
RUN chmod 600 /root/.ssh/authorized_keys
# Allow ssh as root
RUN sed -i 's/#PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
RUN sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
#RUN service ssh restart #didnt work on this level, runs direct in CMD level
# Copy script file
#ADD start.sh ~/
#RUN chmod +x ~/start.sh
#CMD ["~/start.sh"]
#CMD ["watch", "-n", "5000", "date", ">", "/dev/null"]
CMD ["sh", "-c", "service ssh restart; watch -n 5000 date > /dev/null"]
#expose ssh port
EXPOSE 22 80 443
=======================
# create and run the docker vm
docker run --rm -dit -p 22:22 --name vm1 $(docker build -q .)
docker run --rm -dit -p 23:22 --name vm2 $(docker build -q .)
docker run --rm -dit -p 24:22 --name vm3 $(docker build -q .)
docker run --rm -dit -p 25:22 --name vm4 $(docker build -q .)
### PASSWORDLESS LOGIN
#https://www.youtube.com/watch?v=GicWz2OF0sk
#https://linuxize.com/post/how-to-setup-passwordless-ssh-login/
#https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
#Setup SSH Passwordless Login #
#To set up a passwordless SSH login in Linux all you need to do is to generate a public authentication key and append it to the remote hosts ~/.ssh/authorized_keys file.
# Generate a new SSH key pair.
ssh-keygen -t rsa -b 4096 -C "[email protected]"
#on remote vm, set root password
sudo passwd root
#on remote vm, configure ssh to allow root login and restart ssh
nano /etc/ssh/sshd_config
...
service ssh restart
#Copy the public key
ssh-copy-id remote_username@server_ip_address
#If by some reason the ssh-copy-id utility is not available on your local computer you can use the following command to copy the public key:
cat ~/.ssh/id_rsa.pub | ssh remote_username@server_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
# comfirm your ssh public key(source machine) is copied and add to authorized_keys(target vm)
ssh remote_username@server_ip_address "cat ~/.ssh/authorized_keys"
#on remote vm, configure ssh to disable root login, disable password login, enable publickey login and restart ssh
nano /etc/ssh/sshd_config
...
service ssh restart
# Login to your remote vm using SSH keys
ssh remote_username@server_ip_address
## CREATE SYMBOLIC LINK YOUR SSH KEYS (ON WIN HOST) TO WSL
ln -s /mnt/c/Users/<USER>/.ssh ~/
#USAGE
#ON REMOTE MASTER MACHINE
#ADD "192.168.0.17 vm1 vm2 vm3 vm4 vm5", ensure ip is the same ip as your docker gateway ip
#
#ssh root@localhost -p 22 #vm1
#ssh root@localhost -p 23 #vm2
#ssh root@localhost -p 24 #vm3
#ssh root@localhost -p 25 #vm4
#
#IF ANSIBLE IS INSTALLED YOU CAN USE THAT TO RUN ADHOC COMMANDS TOWARDS ALL THE VMs ALL AT ONCE
#https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
#ansible all -m ping
#ansible all -m ansible.builtin.setup #this facts returns system info
#ansible atlanta -a "/sbin/reboot" -f 10 -u username
#ansible atlanta -a "/sbin/reboot" -f 10 -u username --become [--ask-become-pass]
#ansible webservers -m ansible.builtin.yum -a "name=acme state=present"
#...
#
#install and configure fial2ban:
#https://www.linode.com/docs/guides/using-fail2ban-to-secure-your-server-a-tutorial/
#One love from leonard avevor, enjoy!!!