2 Added example.
source | link

The useradd utility uses a number of default values when creating a new user. One of those defaults is a skeleton directory, which is used as a base for user's home directories.

The skeleton directory is configured inside /etc/default/useradd:

SKEL=/etc/skel

You can add the config.sh file to this directory so that it is automatically added for all new users as part of their home directory.


Example: Let's create a simple script file named config.sh:

[root@testvm ~]# cat config.sh
#!/bin/bash
echo "Hello World!"

We'll give the script execute permissions:

[root@testvm ~]# chmod +x config.sh

Next, we copy the script over to the skeleton directory, /etc/skel.

[root@testvm ~]# cp -a ~/config.sh /etc/skel/

Now, let's add the new user*:

[root@testvm ~]# useradd -c "New User" -md /home/NU -e 2018-12-20 -s /bin/bash -u 2000 newuser

Finally, we'll switch to the new user and verify that the file has been placed in the user's home directory:

[root@testvm ~]# su - newuser
[newuser@testvm ~]$ ls -l
total 4
-rwxr-xr-x. 1 newuser newuser 32 Dec  1 22:08 config.sh
[newuser@testvm ~]$ ./config.sh
Hello World!

*I have modified the useradd command from the question. The username has to be specified last, after all the options, and the expiry date uses the YYYY-MM-DD format.

The useradd utility uses a number of default values when creating a new user. One of those defaults is a skeleton directory, which is used as a base for user's home directories.

The skeleton directory is configured inside /etc/default/useradd:

SKEL=/etc/skel

You can add the config.sh file to this directory so that it is automatically added for all new users as part of their home directory.

The useradd utility uses a number of default values when creating a new user. One of those defaults is a skeleton directory, which is used as a base for user's home directories.

The skeleton directory is configured inside /etc/default/useradd:

SKEL=/etc/skel

You can add the config.sh file to this directory so that it is automatically added for all new users as part of their home directory.


Example: Let's create a simple script file named config.sh:

[root@testvm ~]# cat config.sh
#!/bin/bash
echo "Hello World!"

We'll give the script execute permissions:

[root@testvm ~]# chmod +x config.sh

Next, we copy the script over to the skeleton directory, /etc/skel.

[root@testvm ~]# cp -a ~/config.sh /etc/skel/

Now, let's add the new user*:

[root@testvm ~]# useradd -c "New User" -md /home/NU -e 2018-12-20 -s /bin/bash -u 2000 newuser

Finally, we'll switch to the new user and verify that the file has been placed in the user's home directory:

[root@testvm ~]# su - newuser
[newuser@testvm ~]$ ls -l
total 4
-rwxr-xr-x. 1 newuser newuser 32 Dec  1 22:08 config.sh
[newuser@testvm ~]$ ./config.sh
Hello World!

*I have modified the useradd command from the question. The username has to be specified last, after all the options, and the expiry date uses the YYYY-MM-DD format.

1
source | link

The useradd utility uses a number of default values when creating a new user. One of those defaults is a skeleton directory, which is used as a base for user's home directories.

The skeleton directory is configured inside /etc/default/useradd:

SKEL=/etc/skel

You can add the config.sh file to this directory so that it is automatically added for all new users as part of their home directory.