One Response to “Restrict user to Run one remote Server command only via SSH authorized key passwordless authentication on Linux / UNIX / BSD”

  1. admin says:
    Google Chrome 133.0.0.0 Google Chrome 133.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition
    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

    Smal script to easify ssh key generation

     

    #!/bin/bash
     
    # Define the user and group
    USER="user"
    GROUP="group"
    HOME_DIR="/home/$USER"
     
    # Navigate to the user's home directory
    cd "$HOME_DIR" || { echo "Failed to navigate to $HOME_DIR"; exit 1; }
     
    # Create the .ssh directory if it does not exist
    if [ ! -d “.ssh” ]; then
                mkdir .ssh
                    echo ".ssh directory created."
                mkdir .ssh
                    echo ".ssh directory created."
            else
                        echo ".ssh directory already exists."
                fi

                # Change ownership of the .ssh directory
                chown "$USER:$GROUP" .ssh
                echo "Ownership of .ssh directory changed to $USER:$GROUP."

                # Navigate to the .ssh directory
                cd .ssh || { echo "Failed to navigate to .ssh directory"; exit 1; }

                # Create the authorized_keys file if it does not exist
                if [ ! -f “authorized_keys” ]; then
                            touch authorized_keys
                                echo "authorized_keys file created."
                        else
                                    echo "authorized_keys file already exists."
                            fi


                            # Change ownership of the authorized_keys file
                            chown "$USER:$GROUP" authorized_keys
                            echo "Ownership of authorized_keys changed to $USER:$GROUP."
    echo "Generating new DSA key with ECDSA encryption algorithm";
                            ssh-keygen -t ecdsa -f id_dsa
    echo "Copying id_dsa.pub to ~/.ssh/ directory";
    cp -vrpf id_dsa ~/.ssh/
    echo "Copy the id_dsa.pub content to remote server's directory \$HOME/.ssh/authorized_keys and run chown 600 ~/.ssh/authorized_keys";
            else
                        echo ".ssh directory already exists."
                fi
                 
                # Change ownership of the .ssh directory
                chown "$USER:$GROUP" .ssh
                echo "Ownership of .ssh directory changed to $USER:$GROUP."
                 
                # Navigate to the .ssh directory
                cd .ssh || { echo "Failed to navigate to .ssh directory"; exit 1; }
                 
                # Create the authorized_keys file if it does not exist
                if [ ! -f “authorized_keys” ]; then
                            touch authorized_keys
                                echo "authorized_keys file created."
                        else
                                    echo "authorized_keys file already exists."
                            fi
                             
                             
                            # Change ownership of the authorized_keys file
                            chown "$USER:$GROUP" authorized_keys
                            echo "Ownership of authorized_keys changed to $USER:$GROUP."
    echo "Generating new DSA key with ECDSA encryption algorithm";
                            ssh-keygen -t ecdsa -f id_dsa
    echo "Copying id_dsa.pub to ~/.ssh/ directory";
    cp -vrpf id_dsa ~/.ssh/
    echo "Copy the id_dsa.pub content to remote server's directory \$HOME/.ssh/authorized_keys and run chown 600 ~/.ssh/authorized_keys";

    View CommentView Comment

Leave a Reply

CommentLuv badge