Bash/Quick: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
==Defining functions== | ==Syntax== | ||
===Defining functions=== | |||
<pre> | <pre> | ||
| Line 9: | Line 11: | ||
dostuff | dostuff | ||
</pre> | |||
==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 | |||
<pre> | |||
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 | |||
</pre> | |||
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 <code>/etc/sudoers.d/</code>, which is sourced by the sudoers file: | |||
<pre> | |||
echo "${USER} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USER} | |||
</pre> | </pre> | ||
Revision as of 04:49, 25 October 2018
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
| GNU/Linux/Unix the concrete that makes the foundations of the internet.
Compiling Software · Upgrading Software Category:Build Tools · Make · Cmake · Gdb Bash Bash · Bash/Quick (Quick Reference) · Bash Math Text Editors Text Manipulation Command Line Utilities Aptitude · Diff · Make · Patch · Subversion · Xargs Security SSH (Secure Shell) · Gpg (Gnu Privacy Guard) · Category:Security Networking Linux/SSH · Linux/Networking · Linux/File Server Web Servers
|