Linux add user to group www data

Grant a user permissions on www-data owned /var/www

I have a simple web server setup for some websites, with a layout something like: site1: /var/www/site1/public_html/ site2: /var/www/site2/public_html/ I have previously used the root user to manage files, and then given them back to www-data when I was done (WordPress sites, needed for WP Uploads to work). This probably isn’t the best way. I’m trying to find a way to create another user (lets call it user1) that has permission to edit files in site1, but not site2, and doesn’t stop the files being ‘owned’ by www-data . Is there any way for me to do this?

6 Answers 6

If we check ownership of site1, we will find something like this,

ls -ld /var/www/site1/ drwxr-xr-x 2 root root 4096 Oct 24 21:06 site1/ 

This means that the directory is owned by user root, group root. While user root has write permission (plus read and execute permissions) to the directory, group root has only read and execute permissions.

We will want to change the group ownership to another (new) group and add user1 to that particular group. We will give write permission to that particular group as well.

Add user1 to the newly created group,

Check that user1 is really in that group,

The output should be a list something like,

Now we can change the group ownership of your intended directory.

sudo chown -vR :site1 /var/www/site1/ changed ownership of `/var/www/site1/' from root:root to :site1 

Grant write permission to this new group owner,

sudo chmod -vR g+w /var/www/site1/ mode of `/var/www/site1/' changed from 0755 (rwxr-xr-x) to 0775 (rwxrwxr-x) 

Check that all the changes are indeed there,

ls -ld /var/www/site1/ drwxrwxr-x 2 root site1 4096 Oct 24 21:06 /var/www/site1/ 

So, the directory now is owned by user root, group site1. Both user root and group site1 have write permission (plus read and execute permissions) to the directory. Any user belonging to group site1 will enjoy all the privileges granted to that group.

Читайте также:  Linux ports and services

Now login as user1, move to site1 directory and try to create a file in that directory,

echo "My User1 Site" > index.html bash: index.html: Permission denied 

This failed since most likely the primary group of user1 is not site1. So, change to that group.

Try to recreate the file (beware that you have been moved to the home directory of user1 after changing group), this should work now. Since the created files will have world read permission, apache (or your web server) should not face any problem accessing them.

Also, as pointed out by dan08 in comment, you need to add www-data to site1 group.

sudo adduser www-data site1 

On many (not all) distributions, www-data is the user under which the Apache web server runs. This also means that everything done by Apache (especially including PHP scripts) will be done with the permissions of user www-data (and also group www-data) by default. WordPress uses the user www-data to write files.

If you want to see how apache web server is running, issue the command,

ps aux | grep apache2 | less 

Источник

Ubuntu Linux Add a User to Group www-data (Apache Group)

How do I add a user to the Apache group called www-data under Ubuntu or Debian Linux server operating systems using the command line option?

You need to use the useradd command to add a user to the group called www-data under Ubuntu or Debian Linux system. This page shows how to add a user to an existing group named www-data .

Ubuntu Linux Add a User to Group Command

The procedure is as follows:

  1. Open the terminal application.
  2. Login to Ubuntu server using ssh.
  3. Add a new user named foo to www-data group by running useradd -g www-data foo command.
  4. Add an existing user bar to Apache’s www-data group on Ubuntu using usermod -a -G www-data bar command.
  5. Verify new modification on Linux by using the id command.

Let us see all commands in details.

How To See a List of www-data Members

To see list of all members of a group named www-data , run the following grep command on /etc/group file:

How Do I Add an Existing User to Apache Group www-data?

Simply run the following command to add an existing user to an existing group:

sudo adduser USER-NAME-HERE> GROUP-NAME-HERE> sudo adduser john www-data 

Verify it with the help of id command:

Add a New User to the www-data Group

In this example, add a new user called john to the www-data group, enter:

sudo useradd -g www-data john ### set the password for john user ### sudo passwd john 

To verify new settings, enter:

The group name www-data is the user’s initial login group i.e. john has been added to www-data primary group only.

www-data Supplementary Groups Membership

To add a new user called john to supplementary groups called www-data and ftp, enter:

sudo groupadd john sudo useradd -g john -G www-data,ftp john sudo passwd john 

To verify new settings, enter:

Add a Existing User to www-data Group

Type the following command to add an existing user called mat to the www-data group, enter:

sudo usermod -a -G www-data mat id mat groups mat 

Above command will add the user mat to the supplementary group called www-data . Use -a option only with the -G option.

Conclusion

You learned how to add a user to group www-data on Ubuntu Linux using command-line option. For more information see useradd, and usermod man pages online here or by typing the following man command:

Источник

aldibier / How to add a user to www-data (Ubuntu)

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Add a new user to the www-data group
In this example, add a new user called vivek to the www-data group, enter:
sudo useradd -g www-data vivek
### set the password for vivek user ###
sudo passwd vivek
To verify new settings, enter:
id vivek
groups vivek
The group name www-data is the user’s initial login group i.e. vivek has been added to www-data primary group only.
www-data supplementary groups membership
To add a new user called vivek to supplementary groups called www-data and ftp, enter:
sudo groupadd vivek
sudo useradd -g vivek -G www-data,ftp vivek
sudo passwd vivek
To verify new settings, enter:
id vivek
groups vivek
Add a existing user to www-data group
Type the following command to add an existing user called vgite to the www-data group, enter:
sudo usermod -a -G www-data vgite
id vgite
groups vgite
Above command will add the user vgite to the supplementary group called www-data. Use -a option only with the -G option.

Источник

Наследовании группы www-data в директории при создании файлов и папок в Ubuntu

Часто возникает необходимость, чтобы при создании файла или директории в Ubuntu автоматически задавалась группа и пользователь www-data . Для этого необходимо выполнить следующие шаги:

Создание пользователей

Создаём пользователя. Флаг -m нужен для создания домашней директории пользователя (для ее названия используется имя пользователя)..

useradd -m имя_пользователя 

Для введения пароля нового пользователя или изменения пароля существующего введите в командной строке команду

Позже пользователь сможет изменить свой пароль при помощи команды passwd . Для этого ему понадобится ввести текущий пароль и новый.

Добавление пользователя в группу

Для добавления пользователя в группу используется команда usermod . Давайте добавим пользователя имя_пользователя в группу www-data . Опция -a означает append (добавить, прикрепить), а опция -G служит для указания названия вторичной группы.

usermod -a -G www-data имя_пользователя 

Когда вы создаете пользователя при помощи команды adduser , для него автоматически создается первичная группа, носящая его имя. Поэтому в настоящее время пользователь имя_пользователя состоит в первичной группе имя_пользователя и вторичной www-data .

Также можно добавить пользователя сразу в несколько групп, перечислив их через запятую без пробелов (-G group1,group2,group3).

А эта команда изменит первичную группу пользователя имя_пользователя на группу www-data :

usermod -g www-data имя_пользователя 

Задание прав на файлы и папки

Для того, чтобы все файлы в папке и подпапке также имели группу www-data , можно выполнить следующие команды:

sudo chgrp -R www-data польный_путь_к_папке sudo chmod -R g+s польный_путь_к_папке 

Где польный_путь_к_папке — путь к папке, в которой находятся файлы. Первая команда задает группу www-data для всех файлов в папке и подпапках, а вторая команда устанавливает бит setgid для папки, что позволяет новым файлам, созданным в этой папке и подпапках, наследовать группу, установленную для этой папки.

Права доступа

Для предотвращения несанкционированного доступа к файлам и папкам веб-приложения необходимо установить соответствующие права доступа. Например, можно установить права 755 на директорию приложения и права 644 на файлы:

sudo chmod -R 755 польный_путь_к_папке sudo find польный_путь_к_папке -type f -exec chmod 644 > \; -print 

Приветствую вас на сайте ZENCOD.ru! Здесь вы найдете статьи по web-разработке, javascript, linux и прочим темам, которые могут быть полезны.

Источник

Оцените статью
Adblock
detector