At what point is the ~/.bashrc file created?

Solution 1:

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:

  • What is the difference between adduser and useradd?

  • What's the difference between "adduser" and "useradd"?

  • Home directory not being created

  • AddUsersHowto - Ubuntu Documentation Community Wiki

  • What is the *nix command to view a user's default login shell?

  • Terminal command for listing available shells