bash Bash Shell Script
Download Add System and Samba UserDownload Add System and Samba User
Download Count: 323
Date Added: Sunday, 03-Jul-11 15:51:32 CDT
Tags: bash shell script, add user, samba, samba user, add samba user, linux, command line

Bash shell script to add a system user and Samba user from the command line.

This is a scaled down bash script that I have found useful over the years.

Script will prompt you for the user name, check if it already exists, and if not, will ask for password, then add the system user and samba user to the machine. Also creates home directory.

Must be run as root!!

Can easily be extended to do other initial user setup chores.

Make sure the file is executable (chmod 755 add_user.sh).

File Name: add_user.sh - Code Type: Bash
#!/bin/bash
 
# Script to add a user to Linux system and set up samba user.
 
basedir=/home
usershell=/bin/bash
 
## Declare all the functions first
 
## function to check if user already exists on system
ifUserExits(){
        grep $1 /etc/passwd > /dev/null
        [ $? -eq 0 ] && return $TRUE || return $FALSE
}
 
## function to create the user with default shell and home directory passed from below
createNewUser(){
        if /usr/sbin/useradd "$@"
        then echo "User $6 Added"
        fi
}
 
## function to set the user password
createPassword(){
        if echo -e "$1\n$1\n" | /usr/bin/passwd $2
        then echo "Password Created for User $2"
        fi
}
 
## function to setup samba user
createNewSambaUser(){
        if (echo $1; echo $1) | /usr/bin/smbpasswd -as $2
        then echo "Samba Account for User $2 Added" 
        fi
}
 
## make sure root is running script
if [ $(id -u) -ne 0 ] 
then
    echo "You must be root to run this script!"
    exit 2
fi
 
## get username from input
read -p "Enter username : " username
 
## if user doesn't exist, add user
if ( ! ifUserExits $username )
   
then 
        ## get password from input
        read -p "Enter password : " password
        createNewUser -m -b $basedir -s $usershell $username
        createPassword $password $username
        createNewSambaUser $password $username
else
       ## oops, username already being used
        echo "Username \"$username\" already exists"
       exit 3
fi
Parsed in 0.061 seconds - Rate: 21.65 KB/s - GeSHi version: 1.0.8.10

Hope you find this helpful! If you have any questions, please leave a comment.


  • PHP
  • Scripts
  • MS
  • Office
  • Templates
  • and
  • Files
  • Assorted
  • Linux
  • Bash
  • Scripts
  • Perl
  • Scripts
  • PDF
  • FIles
  • MS
  • Excel
  • 2003
  • Template
  • employment
  • tools
  • job
  • search
  • tools
  • spreadsheet
  • Job
  • Search
  • Spreadsheet
  • Template
  • bash
  • shell
  • script
  • file
  • backup
  • linux
  • gzip
  • tar
  • File
  • Backup
  • Script
  • Transmission
  • shell
  • script
  • public
  • torrent
  • private
  • torrent
  • Bit
  • Torrent
  • Transmission
  • BitTorrent
  • Completion
  • Script
  • Transmission
  • shell
  • script
  • torrent
  • information
  • Bit
  • Torrent
  • Transmission
  • Bit
  • Torrent
  • Get
  • Torrent
  • Info
  • bash
  • shell
  • script
  • add
  • user
  • samba
  • samba
  • user
  • add
  • samba
  • user
  • linux
  • command
  • line
  • Add
  • System
  • and
  • Samba
  • User
  • geshi
  • pyrocms
  • php
  • codeigniter
  • PyroCMS/CodeIgniter
  • Geshi
  • Helper
  • File
  • geshi
  • pyrocms
  • php
  • codeigniter
  • GeSHi
  • Code
  • Highlighter
  • plugin
  • for
  • PyroCMS
  • perl
  • cgi
  • hit
  • counter
  • Simple
  • Perl
  • Hit
  • Counter
  • pyrocms
  • codeigniter
  • php
  • ip
  • address
  • session
  • userdata
  • PyroCMS
  • IP
  • Address
  • Plugin
  • Soyo
  • SY5SSM
  • Socket
  • 7
  • Motherboard
  • Quick
  • Start
  • Guide
  • Soyo
  • SY5SSM
  • Socket
  • 7
  • Motherboard
  • Quick
  • Start
  • Guide
  • Home
  • web
  • designit
  • supportlinuxphpbash
  • PRIVACY
  • Statement
  • LEGAL
  • Disclaimer
  • Ebay
  • Listings
  • Soyo
  • Motherboard
  • PDF
  • Why
  • I
  • Like
  • the
  • HoneyPi
  • Brand
  • IR
  • Receiver
  • Module
  • for
  • the
  • Raspberry
  • Pi
  • Using
  • GeSHi
  • Highlighter
  • with
  • PyroCMS/CodeIgniter
  • Easy
  • as
  • 1
  • 2
  • 3
  • PHP
  • Stuff
  • Raspberry
  • PI