From charlesreid1

Revision as of 04:49, 25 October 2018 by Admin (talk | contribs)

Syntax

Defining functions

function dostuff() {
    if [[ -f "${DIR}" ]]; then
        echo "${DIR} exists!"
    fi
}

dostuff


DevOps

Creating a new user

The quick and dirty guide to creating a new user:

Use -s to specify shell

Use -d to specify home directory

Use -G to specify groups

useradd creates the user, usermod modifies the user

USER="florence"

# create new user with specified shell and home dir
useradd -s /bin/bash -d /home/$USER $USER

# add to group1 and group2
usermod -a -G group1,group2 $USER

# add to sudo group
usermod -a -G sudo $USER

Now the user will need to log out and log back in to be able to use sudo command.

To make it passwordless (as on AWS nodes), you can add the following line to a new file in the directory /etc/sudoers.d/, which is sourced by the sudoers file:

echo "${USER} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USER}

Patterns

Check if root user

Use $(id -u) to get the UID. the UID will be 0 if the user is root.

if [ "$(id -u)" != "0" ]; then
    echo ""
    echo ""
    echo "This script should be run as root."
    echo ""
    echo ""
    exit 1;
fi



Flags