Skip to main content

Linux Scripts - Local & SSH Server Setup: Copy SSH Key, Set Hostname & FQDN, Append Hosts Entries, Roate Host Keys, Change IP Address

870 words·
Linux Scripts Bash
Table of Contents

SSH Scripts
#

Copy SSH Key
#

Create SSH Key
#

# Create SSH key with comment
ssh-keygen -t rsa -b 4096 -C "node1"

Shell Execution
#

One line version:

host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13"); for IP in "${host_ips[@]}"; do ssh-copy-id ubuntu@${IP}; done

Multi line version:

# Define the array
host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13")

# Loop through the array
for IP in "${host_ips[@]}"; do
  ssh-copy-id ubuntu@${IP}
done

Script Version
#

#!/bin/bash

host_ips=(
  "192.168.30.11"
  "192.168.30.12"
  "192.168.30.13"
)

# Loop through the array
for IP in "${host_ips[@]}"; do
  ssh-copy-id ubuntu@${IP}
done

Verify SSH Connection via SSH Key
#

Verify Connection
#

# Verify the SSH connection via SSH key
host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13"); for IP in "${host_ips[@]}"; do ssh -o BatchMode=yes ubuntu@${IP} "echo 'Connected to $(hostname)'"; done

Verify Hostnames & FQDNs
#

One line version:

# List hostname
host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13"); for IP in "${host_ips[@]}"; do ssh -o BatchMode=yes ubuntu@${IP} "hostname"; done

# List hostname & FQDN
host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13"); for IP in "${host_ips[@]}"; do ssh -o BatchMode=yes ubuntu@${IP} "hostname; hostname --fqdn"; done
  • BatchMode=yes SSH will not prompt for passwords or passphrase authentication. The SSH session fails immediately if any interactive authentication is required, rather than hanging and waiting for user input.

Multi line version:

# List hostname & FQDN
host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13");

for IP in "${host_ips[@]}"; do 
  ssh -o BatchMode=yes ubuntu@${IP} "hostname; hostname --fqdn";
done



Set Hostname
#

Shell Execution
#

server_details=(
  "192.168.30.11 server1"
  "192.168.30.12 server2"
  "192.168.30.13 server3"
)


for details in "${server_details[@]}"; do
  read IP HOST <<< "$details"

  CMD="sudo sed -i 's/^127.0.1.1.*/127.0.1.1\t${HOST}/' /etc/hosts"
  ssh -n ubuntu@${IP} "$CMD"

  ssh -n ubuntu@${IP} "sudo hostnamectl set-hostname ${HOST}"
done

Script Version
#

None root user
#!/bin/bash

server_details=(
  "192.168.30.11 server1"
  "192.168.30.12 server2"
  "192.168.30.13 server3"
)

# Loop through the array
for details in "${server_details[@]}"; do
  # Parse the details
  read IP HOST <<< "$details"

  # Update /etc/hosts
  CMD="sudo sed -i 's/^127.0.1.1.*/127.0.1.1\t${HOST}/' /etc/hosts"
  ssh -n ubuntu@${IP} "$CMD"

  # Set the hostname
  ssh -n ubuntu@${IP} "sudo hostnamectl set-hostname ${HOST}"
done
Root user
#!/bin/bash

server_details=(
  "192.168.30.11 server1"
  "192.168.30.12 server2"
  "192.168.30.13 server3"
)

# Loop through the array
for details in "${server_details[@]}"; do
  # Parse the details
  read IP HOST <<< "$details"

  # Update /etc/hosts
  CMD="sed -i 's/^127.0.1.1.*/127.0.1.1\t${HOST}/' /etc/hosts"
  ssh -n root@${IP} "$CMD"

  # Set the hostname
  ssh -n root@${IP} "hostnamectl set-hostname ${HOST}"
done

Set Hostname & FQDN
#

Shell Execution
#

server_details=(
  "192.168.30.11 server1.example.com server1"
  "192.168.30.12 server2.example.com server2"
  "192.168.30.13 server3.example.com server3"
)


for details in "${server_details[@]}"; do
  read IP FQDN HOST <<< "$details"

  CMD="sudo sed -i 's/^127.0.1.1.*/127.0.1.1\t${FQDN} ${HOST}/' /etc/hosts"
  ssh -n ubuntu@${IP} "$CMD"

  ssh -n ubuntu@${IP} "sudo hostnamectl set-hostname ${HOST}"
done

Script Version
#

None root user
#!/bin/bash

server_details=(
  "192.168.30.11 server1.example.com server1"
  "192.168.30.12 server2.example.com server2"
  "192.168.30.13 server3.example.com server3"
)

# Loop through the array
for details in "${server_details[@]}"; do
  # Parse the details
  read IP FQDN HOST <<< "$details"

  # Update /etc/hosts
  CMD="sudo sed -i 's/^127.0.1.1.*/127.0.1.1\t${FQDN} ${HOST}/' /etc/hosts"
  ssh -n ubuntu@${IP} "$CMD"

  # Set the hostname
  ssh -n ubuntu@${IP} "sudo hostnamectl set-hostname ${HOST}"
done
Root user
#!/bin/bash

server_details=(
  "192.168.30.11 server1.example.com server1"
  "192.168.30.12 server2.example.com server2"
  "192.168.30.13 server3.example.com server3"
)

# Loop through the array
for details in "${server_details[@]}"; do
  # Parse the details
  read IP FQDN HOST <<< "$details"

  # Update /etc/hosts
  CMD="sed -i 's/^127.0.1.1.*/127.0.1.1\t${FQDN} ${HOST}/' /etc/hosts"
  ssh -n root@${IP} "$CMD"

  # Set the hostname
  ssh -n root@${IP} "hostnamectl set-hostname ${HOST}"
done

Append Hosts Entries
#

Script Version
#

#!/bin/bash

# Define the hosts
hosts_block=$(cat <<-EOF
# Some hosts
192.168.30.11 server1.example.com server1
192.168.30.12 server2.example.com server2
192.168.30.13 server3.example.com server3
192.168.30.14 server4.example.com server4
192.168.30.15 server5.example.com server5
EOF
)


# Define server IPs
host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13")


# Loop through the array
for IP in "${host_ips[@]}"; do
  # Append the hosts block to /etc/hosts
  ssh -o BatchMode=yes ubuntu@${IP} "echo \"${hosts_block}\" | sudo tee -a /etc/hosts"
done

Roate Host Keys
#

Shell Execution
#

host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13")

for IP in "${host_ips[@]}"; do
  ssh -o BatchMode=yes ubuntu@${IP} "sudo rm -f /etc/ssh/ssh_host_* && sudo ssh-keygen -A && sudo systemctl restart ssh"
done

Script Version
#

#!/bin/bash

host_ips=("192.168.30.11" "192.168.30.12" "192.168.30.13")

# Loop through the array
for IP in "${host_ips[@]}"; do
  # Remove old host keys and regenerate them
  ssh -o BatchMode=yes ubuntu@${IP} "sudo rm -f /etc/ssh/ssh_host_* && sudo ssh-keygen -A && sudo systemctl restart ssh"
done

Remove Host Key
#

# Remove host key: Current user
ssh-keygen -R 192.168.30.11

# Remove host key: Specific user
ssh-keygen -f "/home/username/.ssh/known_hosts" -R "192.168.30.11"



Local Scripts
#

Set Hostname, Rotate Host Keys, Change IP
#

The following script sets a new hostname, rotates the host keys and define a new IP address. This can be very handy when a VM copied or deployed from a template.

Ubuntu 24
#

sudo hostnamectl set-hostname new-hostname &&
sudo sed -i 's/^127.0.1.1.*/127.0.1.1\t new-hostname/' /etc/hosts &&
sudo rm /etc/ssh/ssh_host_* &&
sudo ssh-keygen -A &&
sudo sed -i 's/192.168.30.11/192.168.30.22/' /etc/netplan/50-cloud-init.yaml
sudo reboot

Debian 12
#

sudo hostnamectl set-hostname new-hostname &&
sudo sed -i 's/^127.0.1.1.*/127.0.1.1\t new-hostname/' /etc/hosts &&
sudo rm /etc/ssh/ssh_host_* &&
sudo ssh-keygen -A &&
sudo sed -i 's/192.168.30.11/192.168.30.22/' /etc/network/interfaces &&
sudo reboot

RHEL 8 / Rocky Linux
#

sudo hostnamectl set-hostname new-hostname &&
sudo nmcli con mod ens160 ipv4.addresses 'new-ip/24' &&
sudo rm /etc/ssh/ssh_host_* &&
sudo ssh-keygen -A &&
sudo reboot



Other Useful Scripts
#

Change File Extensions
#

# Rename all ".yml" file extensions to ".yaml" in current directory
for file in *.yml
do mv -- "$file" "${file%.*}.yaml"
done