Write bash script to check output of "ifconfig" command for one of two ip addresses and run sshd with specified config file. I mean different config file for each ip address
Assistant
Certainly! Below is a sample bash script that checks the output of the ifconfig command for two specific IP addresses. Depending on which IP address is found, it will start the sshd service using a specified configuration file.
You can replace 192.168.1.100 and with your desired IP addresses and also specify different SSH configuration files for each IP.
User
How to use ssh keygen to generate public and private key and put to authorized_keys
Assistant
User
ssh-keygen specify output ?
Assistant
When using ssh-keygen, you can specify the output file for the generated SSH key pair using the -f option. Below is the command format and an example of how to do this:
To generate an Elliptic Curve (EC) SSH key pair using ssh-keygen, you can specify the key type as ecdsa or ed25519, which is a modern and recommended elliptic curve algorithm. Here's how to do it for both types.
192.168.1.101
bash
#!/bin/bash
# Define the two IP addresses and their corresponding SSH config files
IP1="192.168.1.100"
CONFIG_FILE1="/etc/ssh/sshd_config_ip1"
IP2="192.168.1.101"
CONFIG_FILE2="/etc/ssh/sshd_config_ip2"
# Get the current IP addresses from ifconfig
CURRENT_IPS=$(ifconfig | grep 'inet ' | awk '{print $2}')
# Check for the first IP address
if echo "$CURRENT_IPS" | grep -q "$IP1"; then
echo "Starting sshd with config file for $IP1"
/usr/sbin/sshd -f $CONFIG_FILE1
# Check for the second IP address
elif echo "$CURRENT_IPS" | grep -q "$IP2"; then
echo "Starting sshd with config file for $IP2"
/usr/sbin/sshd -f $CONFIG_FILE2
else
echo "Neither $IP1 nor $IP2 found in ifconfig output."
exit 1
fi
Explanation of the Script
Definition of Variables: The script defines two IP addresses (IP1 and IP2) and their corresponding SSH config files (CONFIG_FILE1 and CONFIG_FILE2).
Retrieve Current IPs: It uses ifconfig to get the list of IP addresses on the machine. The output is filtered to extract the IP addresses.
Conditional Checks: It checks whether CURRENT_IPS contains IP1 or IP2 using grep.
Run sshd: Depending on which IP address is found, it starts the SSH daemon (sshd) with the specified configuration file.
Error Handling: If neither IP is found, it prints a message and exits with a status of 1.
Usage
Make sure to give execution permissions to your script:
bash
chmod +x your_script.sh
Run the script:
bash
./your_script.sh
Note
The command might require superuser privileges, so you might need to run the script with sudo, especially to start sshd.
Ensure the specified configuration files exist and are correctly set up for your needs.
To generate a public and private SSH key pair using ssh-keygen, and then add the public key to the authorized_keys file on a remote server, follow these steps:
-t rsa: Specifies the type of key to create (RSA).
-b 4096: Specifies the number of bits in the key (4096 bits is considered secure).
-C "your_email@example.com": Adds a comment (optional) for identifying the key, often your email address.
When prompted, you can choose to specify a different file location for your key files, or just press Enter to accept the default location (usually ~/.ssh/id_rsa for the private key and ~/.ssh/id_rsa.pub for the public key).
You will also have the option to create a passphrase for your key. This adds an extra layer of security. If you do not want to set a passphrase, just press Enter.
Step 2: Copy the Public Key to the Remote Server
Option 1: Using ssh-copy-id
If you have SSH access to the remote server, you can easily copy your public key using the ssh-copy-id command:
bash
ssh-copy-id user@remote_host
Replace user with your username on the remote server and remote_host with the server's IP address or hostname. You will be prompted to enter your password for the remote server.
Option 2: Manual Copying
If you prefer or need to do it manually, you can follow these steps:
Display the contents of your public key file:
bash
cat ~/.ssh/id_rsa.pub
Log in to your remote server using SSH:
bash
ssh user@remote_host
Once you are logged in, create the .ssh directory (if it does not exist) and set proper permissions:
bash
mkdir -p ~/.ssh
chmod 700 ~/.ssh
Append your public key to the authorized_keys file:
Then paste your public key from your local terminal, and press Ctrl-D to save.
Set the permissions for the authorized_keys file:
bash
chmod 600 ~/.ssh/authorized_keys
Exit the remote server:
bash
exit
Step 3: Test the SSH Key Authentication
Now you can test the connection to the remote server without a password:
bash
ssh user@remote_host
If everything is set up correctly, you should be able to log in without being prompted for the remote user's password. If you set a passphrase when generating the key, you will need to provide that passphrase.
Summary
To recap:
Use ssh-keygen to create your SSH key pair.
Use ssh-copy-id or manual copying to place your public key in the authorized_keys file.
Adjust permissions appropriately for SSH to work correctly.
Test the setup to ensure password-less login works.
-t <key_type>: Specifies the type of key to create (e.g., rsa, dsa, ecdsa, ed25519).
-b <key_size>: Specifies the number of bits in the key (especially for RSA; commonly 2048 or 4096).
-f <output_file>: Specifies the file path where the private key will be saved. The public key will be saved with the same name but with a .pub extension.
-C "<comment>": An optional comment to help identify the key (often used for your email).
Example
Let's say you want to generate an RSA key pair and save it in a specific file path:
The private key will be saved as ~/.ssh/my_custom_key.
The public key will be saved as ~/.ssh/my_custom_key.pub.
The comment "my_email@example.com" will be added to the public key for identification.
Important Note
If the specified file already exists, ssh-keygen will prompt you to confirm whether you want to overwrite it. Be cautious with overwriting existing keys, as this could result in loss of access if the previous keys were in use.
After Generation
After running the ssh-keygen command with your specified output file:
Use cat to display the contents of the public key, or use ssh-copy-id to copy it to a remote server:
bash
cat ~/.ssh/my_custom_key.pub
To copy the public key automatically to the remote server, you can do:
Replace user with your username and remote_host with the server's address.
This allows you to effectively control where your SSH key files are saved and manage multiple key pairs easily.
Using ecdsa
Generate an ECDSA Key Pair
You can specify the curve size using the -b option. Common sizes are 256, 384, or 521 bits. Here's a command to generate an ECDSA key pair with a 256-bit key:
-t ecdsa: Specifies that you want to create an ECDSA key.
-b 256: Specifies the size of the key (you can also use 384 or 521).
-f ~/.ssh/id_ecdsa: Specifies the output file for the private key.
-C "your_email@example.com": Adds a comment to the key for identification.
Using the Key
After generating the key pair, the private key will be stored in ~/.ssh/id_ecdsa, and the corresponding public key will be in ~/.ssh/id_ecdsa.pub. You can copy it to a remote server as explained in previous messages, either using ssh-copy-id or manually.
Using ed25519
The Ed25519 algorithm is an alternative to ECDSA and is generally recommended for new deployments due to its security and performance properties.
Generate an Ed25519 Key Pair
To create an Ed25519 key pair, you can use the following command:
-t ed25519: Specifies that you want to create an Ed25519 key.
-f ~/.ssh/id_ed25519: Specifies the output file for the private key.
-C "your_email@example.com": Adds a comment to the key.
Using the Key
The resulting keys will be stored in ~/.ssh/id_ed25519 (private key) and ~/.ssh/id_ed25519.pub (public key). You can similarly manage the public key according to your needs.
Summary
Use ecdsa for ECDSA keys and specify the desired bit length (256, 384, or 521).
Use ed25519 for Ed25519 keys, which are generally recommended for new SSH key deployments because they are considered faster and more secure.
Remember to secure your private keys and manage the public keys appropriately when setting up SSH access to remote servers.