0

I need to create a new user, and it needs to have a custom file named "config.sh".

what I do when creating a new user is:

sudo useradd –c "New User" –md /home/NU newuser –e 20/12/2018 –s /bin/bash –u 2000

where -md /home/NU creates the directory for the new account. How do I add, in this diretory, a file named "config.sh"?

  • Creating a file is a very basic action in UNIX. Have you tried googling? You will get hundreds of results. – Peschke Dec 1 '18 at 16:07
1

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.

  • could you provide me an example? I can't make it work. Thanks – WhiteGlove Dec 1 '18 at 16:33
  • @WhiteGlove Please see the edit. – Haxiel Dec 1 '18 at 16:48
  • it's weird that when I enter into root and do the su - [username] I'm still at my base home directory from the acc I'm currently running. – WhiteGlove Dec 1 '18 at 17:02
  • @WhiteGlove It may be a problem with the environment variables. If you're used to sudo, perhaps you can try sudo su - newuser from the original admin account instead. For testing, you could also log in separately with the new user instead of trying to switch to it. My command outputs are taken from a throwaway VM, which is why I used root throughout. – Haxiel Dec 1 '18 at 17:24
  • I work with a VM too. Thanks for the help, mate! – WhiteGlove Dec 2 '18 at 10:12

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.