21

For instance, if I were to run the command adduser test, is the .bashrc created automatically upon creation of the home directory for the user test? If it's not created upon user creation, under what conditions is it created?

Also, if I add a .bashrc file to the home directory of a user, will it be overwritten by the OS?

Thanks in advance.

  • 2
    If you just tried it, you would find out much faster than by asking here. – Stig Hemmer Nov 2 '17 at 9:01
30

In most cases, the initial user files (including .bashrc) are created when the user is created. They are copies of the files stored into the 'skeletal' directory, named /etc/skel. There are two main commands:

  • useradd is the back-end command, if you using it directly (without any options) just a system user (and its group) will be created:

    $ sudo useradd new-user
    $ ls -la /home/new-user/
    ls: cannot access '/home/new-user/': No such file or directory
    
    • useradd will create a home directory and populate it with a copy of /etc/skel if the -m --create-home option is supplied. Thanks for this comment to @jwodder.

    • The option -s --shell will change the name of the new user's login shell. No matter what the new user's login shell is, the entire content of the 'skeletal' directory /etc/skel will be populated into the new user's home directory.

    • A different 'skeletal' directory cold be defined with the -k --create-home option.

    • The configuration file of this command is /etc/default/useradd.

  • adduser is user-friendly and interactive front-end of useradd. This command will copy the files from /etc/skel to the user's $HOME directory by default:

    $ sudo adduser new-user
    Adding user `new-user' ...
    Adding new group `new-user' (1002) ...
    Adding new user `new-user' (1002) with group `new-user' ...
    Creating home directory `/home/new-user' ...
    Copying files from `/etc/skel' ...
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    Changing the user information for new-user
    Enter the new value, or press ENTER for the default
            Full Name []: New User
            Room Number []:
            Work Phone []:
            Home Phone []:
            Other []:
    Is the information correct? [Y/n]
    
    $ ls -a /home/new-user/
    .   ..   .bash_logout   .bashrc   .config   examples.desktop   .profile   .Xdefaults
    
    • The configuration file of this command is /etc/adduser.conf.

    • The default value for the new user's login shell is deteminated by the variable DSHELL=. It could be specified also via the option --shell. No matter what the new user's login shell is, the entire content of the 'skeletal' directory /etc/skel will be populated into the new user's home directory.

    • The SKEL= variable in this file specifies the default directory containing 'skeletal' user files.

    • The option --no-create-home will force the command adduser to do not create a user home directory, respectively the content of the 'skeletal' directory will not be copied.

    • If called with one non-option argument and the --system option, adduser will add a system user... A home directory is created by the same rules as for normal users. The new system user will have the shell /bin/false (unless overridden with the --shell option), and have logins disabled. Skeletal configuration files are not copied. ...from the manual page.


Additionally, when the user is created with any GUI tool as User Accounts, the result is identical to that of the command adduser.


References:

  • 2
    useradd will also create a home directory and populate it with a copy of /etc/skel if the -m option is supplied. – jwodder Nov 1 '17 at 21:41
  • 4
    Your second code block still uses useradd in the command; was that supposed to be adduser? – David Z Nov 1 '17 at 22:04
  • 2
    Also, User Accounts makes use of adduser, doesn't it? – Gunnar Hjalmarsson Nov 1 '17 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.