7

This may sound odd.

My username is ubuntu and my machine is called ubuntu.

What I want is when I create a file in some folder that it has a signature

user      group
ubuntu    www-data

at the moment whatever i create has a signature

ubuntu    ubuntu

Can I simply remove my user from group ubuntu and add it to group www-data?

0

5 Answers 5

10

sudo chgrp www-data *yourfile* will do it for individual files.

to do it for all files within a specific directory, change the group for that directory with the same command

sudo chgrp www-data /path/to/your/dir

then use the chmod command to make all files created within that directory belong to the group the directory belongs to with

sudo chmod g+s /path/to/your/dir

3
  • i don't want to change the group of the file. i want that file to have www-data group at the moment of creation
    – lewis4u
    Apr 11, 2017 at 21:48
  • @lewis4u I had already added an option to do that. Refresh the page to see the edit.
    – Will
    Apr 11, 2017 at 21:52
  • Tanks @Will , you answer is really usefull to me, a few months in seach for this.
    – rpaillao
    Dec 16, 2020 at 15:39
7

The command sg can execute a command under a different group ID. If you are a member of the group newgroup, this should create newfile within that group :

sg newgroup "touch newfile"

(Source: https://unix.stackexchange.com/questions/18796/how-to-apply-changes-of-newly-added-user-groups-without-needing-to-reboot)

2

We can create a simple function, based on touch and chown commands, which will create new empty files and will change their permissions simultaneously. Or when the file exists it just will change its permissions. For this purpose type in the terminal:

function touch-www { touch $1; chown $USER:www-data $1; }
export -f touch-www

Now we have a new command, called touch-www, and we can use it in this way:

touch-www /path/to/file

To be possible to use this new command everywhere in the file system let's modify the function in this way:

function touch-www { sudo touch $1; sudo chown $USER:www-data $1; }
export -f touch-www

Once the file have enough permissions we can edit it with the current user. So let's assume we want to use and nano in the way described here. Let's create new function:

function nano-www { sudo touch $1; sudo chown $USER:www-data $1; nano $1; }
export -f nano-www

To be these new commands permanently available we can add these lines in the bottom of the ~/.bashrc file:

function touch-www { sudo touch $1; sudo chown $USER:www-data $1; }
export -f touch-www

function nano-www { sudo touch $1; sudo chown $USER:www-data $1; nano $1; }
export -f nano-www
2
  • Sorry but I think you didn't read the question carefully. I don't want to change the group of the files. The keyword is "create" and not "change" what you are proposing is changing the files group after they are created.
    – lewis4u
    Apr 12, 2017 at 4:29
  • @lewis4u Yes, you are right, I've got this idea and just decided to share it.
    – pa4080
    Apr 12, 2017 at 6:06
0

Assuming you only want to change the group and retain the user value, try

sudo chown newuser:newgroup sample.txt

or in your case:

sudo chown ubuntu:www-data sample.txt

Then run

sudo chmod g+w sample.txt

2
  • 1
    sorry maybe i wasn't clear enough...i want the NEW files that i create to have already the www-data group!
    – lewis4u
    Apr 11, 2017 at 21:47
  • My apologies. I did find this answer, but it only pertains to files within your home directory
    – drewburr
    Apr 11, 2017 at 21:51
0

You could create a www-data user, with www-data group as their primary [see useradd, adduser, or eg kuser], change to that user [su www-data] and then all files made would be owned by that user:group.

Backup /etc/group (or the whole of /etc) first before messing with groups. Also lookup what to do if you're locked out because of group permissions.

Or, you could alter the primary group of the user named "ubuntu" to be www-data:

sudo usermod --gid www-data ubuntu.

There may be security issues with the later as it might at some point (eg combined with other bugs) expose all files owned by that group to your web server. The point of www-data group is that only those files owned by it are allowed to be sent externally by the server (apache, nginx, whatever). Having all your standard user login files belong to that group thus creates a risk.

You must log in to answer this question.

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