Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    How can I create a new user with custom home directory?


    Hi Sir,

    I would like to create a new user with custom home directory?
    I want to auto adding a folder (a letter) before the user folder

    For example:
    A new user "Peter", the home folder will be /home/p/peter
    A new user "Panda", the home folder will be /home/p/panda
    A new user "Mary", the home folder will be /home/m/mary
    A new user "Ken", the home folder will be /home/k/ken

    How can I do this?

    Thanks
    Ken Lok

  2. #2
    hi,

    you can use the -d switch to useradd to specify the home dir. so in a script, you could grab the first letter of the username, e.g.:

    Code:
    for user in job bob mary; do
      x=$(echo $user|cut -c1)
      useradd -d /home/$x/$user $user
    done

  3. #3
    Quote Originally Posted by atreyu View Post
    hi,

    you can use the -d switch to useradd to specify the home dir. so in a script, you could grab the first letter of the username, e.g.:

    Code:
    for user in job bob mary; do
      x=$(echo $user|cut -c1)
      useradd -d /home/$x/$user $user
    done
    Would it be possible to make the home dir automatic? (without any special command?)
    Thanks

  4. $spacer_open
    $spacer_close
  5. #4
    Quote Originally Posted by kenlok View Post
    Would it be possible to make the home dir automatic? (without any special command?)
    i'm not sure what you mean by automatic. do you mean you want "useradd joeblow" to create /home/j/joeblow? i don't know of a way to do that.

    you could change the basedir instead (usually "/home"):

    Code:
    useradd -b /home/$x $user
    which would give the same resultant home dir, but you'd still need to determine $x first, as in my original code. default settings for useradd are found in /etc/default/useradd, btw.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •