-7

Please help with this question :

Create a new user called hos with password 123. In the home directory of this user, create a file called xyz, containing a list of all files and directories from /etc that start with a.

Make sure the newly created user owns the file. I am using this but it says permission denied target ‘/home/hos/xyz’ is not a directory

sudo adduser hos password 123
mkdir /home/hos/xyz
sudo cp -r /etc/a* /home/hos/xyz
2
  • sudo adduser hos password 123 mkdir /home/hos/xyz sudo cp -r /etc/a* /home/hos/xyz chown -R hos /home/hos/xyz Jun 8, 2016 at 14:19
  • mkdir is for making directories. You should be trying to make a file that contains a list of all files and directories starting with a in /etc. You shouldn't be trying to make a directory called xyz with a copy of every folder and file starting with a inside. Jun 8, 2016 at 14:25

1 Answer 1

2

To answer your question you need a few steps to solve that. As first you create a user and his username, add a group for him and add him to this group.

This you can do with one command or multiple commands, first the multiple approach where I add in comment lines for explanation:

# adding a user hos with password 123 which you have provide by hand
# you can add the password into the line with the -p flag but you have
# provide the password in encrypted format then.
sudo adduser hos

# adding a group with the same name as the user
sudo addgroup hos

# adding the newly created user to this newly created group
sudo usermod -aG hos hos

Same you can achieve with only the adduser command like in the following line. For options on the adduser command see its man page.

adduser --ingroup hos hos

Now to the second part of that question, creating a file containing the names of files in /etc which begin with a. This is simply achieved by listing the files with ls /etc/a*.

ls /etc/a* | sudo -u hos tee ~hos/xyz

This should be all.

4
  • 1
    You didn't create the group hos though?
    – Ziazis
    Jun 8, 2016 at 14:30
  • You don't need to create the deault homedir, adduser usually creates a homedir in /home/ automatically using /etc/skel as it's template. Unless you specify --no-create-home.
    – Ziazis
    Jun 8, 2016 at 14:46
  • 1
    It is almost never a good idea to parse the output of ls. In any case, it is not needed at all here, all you want is ls /etc/a*.
    – terdon
    Jun 18, 2016 at 17:08
  • You should not need to take care of the primary group when you use adduser It automatically creates a group of the same name as the user and adds the user to it. You also don't need the --ingroup.
    – Byte Commander
    Jun 18, 2016 at 18:55

Not the answer you're looking for? Browse other questions tagged .