By Wael

Posted :

How to assign a shell , using useradd

useradd is used to create a new user . It is also used , to set the default options , used , when creating a new user .

One of the options , that can be set , when using useradd , is the login shell .

assign a shell using useradd tutorial
The -s LOGIN_SHELL or --shell LOGIN_SHELL options

The login shell , for a new user , can be set by using the -s LOGIN_SHELL , or --shell LOGIN_SHELL options . LOGIN_SHELL , must be a path . Valid shell paths , can be gotten , from /etc/shells .

@root:~$ cat /etc/shells
 # Print the paths for valid 
 # shells  .
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash

If the account is supposed , to not to be able to login , then /sbin/nologin , can be used as a login shell , for the account .

In this case , if the account tries to login , /sbin/nologin will display a message , that the account is not available for login .

This can be used for example , with daemon accounts .

@root:~$ /sbin/useradd -s bash user-one
useradd: invalid shell 'bash'
# The operation fails , because 
# bash is not a  path 


@root:~$ /sbin/useradd --shell /bin/bash user-one
# Add a user named user-one , with a 
# login shell of : bash , located at 
# /bin/bash

@root:~$ cat /etc/passwd | grep user-one
# grep user-one , from /etc/passwd
user-one:x:1001:1001::/home/user-one:/bin/bash
# user-one has a login shell of /bin/bash


@root:~$ /sbin/useradd --shell /sbin/nologin daemon-one
# Add a user account , named daemon-one , with a 
# login shell of :  /sbin/nologin 

@root:~$ cat /etc/passwd | grep daemon-one
# grep daemon-one from /etc/passwd
daemon-one:x:1002:1002::/home/daemon-one:/sbin/nologin
# daemon-one has a login shell path , set to  :
# /sbin/nologin

@root:~$ su daemon-one
# change user to daemone-one
This account is currently not available.
# When daemon-one tries to login , the message
# that he will get , is : This account is currently 
# not available


@root:~$ /sbin/useradd --shell /bin/invalid_option user-invalid-shell
# useradd will not check , if the path is 
# a valid path .
# For example , here user-invalid-shell is added . 
# The provided shell path is not a valid one .

@root:~$  su user-invalid-shell
# The user will fail to login , because 
# the specified shell is not a valid one 
su: failed to execute /bin/invalid_option: No such file or directory
If no login shell is specified ?

If the -s LOGIN_SHELL or --shell LOGIN_SHELL options are not used , then useradd , will check the /etc/default/useradd file , for the variable named SHELL .

If it exists , and is not commented out , then the account is created with a login shell path , as the one pointed by the SHELL variable .

If the SHELL variable is not defined , or is commented out , then the default shell path , for the newly created user account , will be left empty . If the login shell path is empty , then the default shell will be : /bin/sh .

@root:~$ cat /etc/default/useradd | grep SHELL
# grep the value of SHELL  , from :
# /etc/default/useradd 
SHELL=/bin/sh
# If the -s LOGIN_SHELL or 
# --shell LOGIN_SHELL options , 
# are not used with useradd , then 
# the SHELL variable will be 
# checked . 
# If it exists , and is 
# not commented out , then the user 
# login shell , will be the one pointed 
# out , by the path set , in : SHELL .

@root:~$ /sbin/useradd user-two
# Create a user , named user-two . 
# No login shell was specified , as 
# such the SHELL variable in 
# /etc/default/useradd is checked . 
# It exists , and it is not commented
# out , as such the user will have a 
# login shell path , as the one pointed 
# by the SHELL variable . 

@root:~$ cat /etc/passwd | grep user-two
# grep user-two from /etc/passwd
user-two:x:1004:1004::/home/user-two:/bin/sh
# user-two has a login shell of /bin/sh


@root:~$ cat /etc/default/useradd | grep SHELL
# grep the value of SHELL  , from :
# /etc/default/useradd 

# SHELL=/bin/sh

# The SHELL variable is commented out , 
# as such , when useradd is used without 
# the -s LOGIN_SHELL or --shell LOGIN_SHELL
# options , the login shell path will be left
# empty . If the login shell path is empty  ,
# then the default shell is : /bin/sh 

@root:~$ /sbin/useradd user-three	
# Create a user named user-three 

@root:~$ cat /etc/passwd | grep user-three
# grep user-three from /etc/passwd
user-three:x:1005:1005::/home/user-three:
# user-three default login shell is  
# empty , because when creating 
# user-three , the -s LOGIN_SHELL or 
# --shell LOGIN_SHELL options , were not 
# used , and the SHELL variable , in 
# /etc/default/useradd was 
# commented out . 

@root:~$ su user-three
# Change user to user-three
$ echo $SHELL
# Print out the current shell
/bin/sh
# user-three default login 
# shell is /bin/sh
How to set the value of SHELL in /etc/default/useradd

useradd -D -s SHELL_PATH , or useradd -D --shell SHELL_PATH , can be used to set the value of SHELL , in : /etc/default/useradd .

@root:~$ cat /etc/default/useradd | grep SHELL
# grep the value of SHELL from /etc/default/useradd

# SHELL=/bin/sh

# The SHELL variable is commented out .

@root:~$ /sbin/useradd -D -s /bin/bash
# Set the SHELL variable value in
# /etc/default/useradd , to :
# /bin/bash
# Valid SHELL path values , can be gotten 
# from : /etc/shells 

@root:~$ cat /etc/default/useradd | grep SHELL
# grep the value of SHELL from /etc/default/useradd

# SHELL=/bin/sh
SHELL=/bin/bash

# The SHELL variable is now set to 
# /bin/bash 

@root:~$ /sbin/useradd -D 
# Display the default options 
# used with useradd 
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no