简体   繁体   English

Root 无权更改目录所有者

[英]Root doesn‘t have the permission to change a directory owner

I can't change a directory's owner even if I'm root.即使我是 root,我也无法更改目录的所有者。

I want to build a NAS by using raspberry 3B+.我想用raspberry 3B+搭建一个NAS。

  1. So I have created a user named dorm.所以我创建了一个名为 dorm 的用户。
  2. Then I changed sshd_file in order to restrict SFTP users(dorm) to only visit his home directories然后我更改了 sshd_file 以限制 SFTP 用户(宿舍)只能访问他的主目录

  1. Now the user(dorm) doesn't have the write permission.现在用户(宿舍)没有写权限。 So After I google it,I should mkdir a 777 directory at /home/dorm/ .So I made it by root.所以在我谷歌它之后,我应该在 /home/dorm/ 中 mkdir 一个 777 目录。所以我用 root 做了它。
  2. Then I wanna change its owner by chown -R dorm:dorm /home/dorm/Documents.然后我想通过 chown -R dorm:dorm /home/dorm/Documents 更改它的所有者。 I failed.我失败了。

Maybe its best to remove the user and try again, this is what I did to make it work.也许最好删除用户并重试,这就是我为使其工作所做的工作。

  1. log into newly setup raspberry pi as pi user, only default logins exist.pi用户身份登录新设置的 raspberry pi,仅存在默认登录名。
pi@raspberrypi:~ $ whoami
pi
pi@raspberrypi:~ $ lslogins -u
 UID USER PROC PWD-LOCK PWD-DENY LAST-LOGIN GECOS
   0 root   84                              root
1000 pi      5                        15:31 ,,,
  1. add new user dorm添加新用户dorm
$ sudo adduser dorm
Adding user `dorm' ...
Adding new group `dorm' (1001) ...
Adding new user `dorm' (1001) with group `dorm' ...
Creating home directory `/home/dorm' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for dorm
Enter the new value, or press ENTER for the default
    Full Name []:
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] y

pi@raspberrypi:~ $ lslogins -u
 UID USER PROC PWD-LOCK PWD-DENY LAST-LOGIN GECOS
   0 root   87                              root
1000 pi      5                        15:31 ,,,
1001 dorm    0                              ,,,
  1. make a backup and edit your sshd_config file,备份并编辑您的sshd_config文件,
pi@raspberrypi:~ $ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config-bak
pi@raspberrypi:~ $ sudo vim /etc/ssh/sshd_config
pi@raspberrypi:~ $ diff /etc/ssh/sshd_config /etc/ssh/sshd_config-bak
122,128d121
<
< Match User dorm
<     X11Forwarding no
<     AllowTcpForwarding no
<     PermitTTY no
<     ForceCommand internal-sftp
<     ChrootDirectory /home/dorm
  1. restart ssh service with sudo service sshd restart使用sudo service sshd restart启动 ssh 服务
  2. change the ownership of the dorm home directory to root.将宿舍主目录的所有权更改为 root。
$ sudo chown root:root /home/dorm
$ sudo chmod 755 /home/dorm
  1. from the dorm user's directory use sudo as the pi user to create the Documents folder and give ownership to the dorm user从 dorm 用户的目录使用 sudo 作为 pi 用户创建 Documents 文件夹并将所有权授予dorm用户
pi@raspberrypi:/home/dorm $ ls
pi@raspberrypi:/home/dorm $ mkdir Documents
mkdir: cannot create directory ‘Documents’: Permission denied
pi@raspberrypi:/home/dorm $ sudo mkdir Documents
pi@raspberrypi:/home/dorm $ ls -l
total 4
drwxr-xr-x 2 root root 4096 Feb  8 18:15 Documents
pi@raspberrypi:/home/dorm $ sudo chown -R dorm:dorm Documents
pi@raspberrypi:/home/dorm $ ls -l
total 4
drwxr-xr-x 2 dorm dorm 4096 Feb  8 18:15 Documents
  1. test the connection etc. using your IP address from your host computer (ssh should reply No route to host )使用主机上的 IP 地址测试连接等(ssh 应回复No route to host
$ sftp dorm@<IP>
dorm@<IP>'s password:
Connected to dorm@<IP>.
sftp>
  • you can see the created Documents folder and the user number that owns it (dorm's number)您可以看到创建的 Documents 文件夹和拥有它的用户号(宿舍号)
sftp> ls -ltr
drwxr-xr-x    2 1001     1001         4096 Feb  8 18:15 Documents
  • you can't move out of the base directory你不能移出基本目录
sftp> pwd
Remote working directory: /
sftp> cd ..
sftp> pwd
Remote working directory: /
  • you can't put files in the base directory as dorm doesn't have permissions.您不能将文件放在基本目录中,因为 dorm 没有权限。 (file path will need to be altered for your system) (需要为您的系统更改文件路径)
sftp> put /Users/<USER>/tmp
Uploading /Users/<USER>/tmp to /tmp
remote open("/tmp"): Permission denied
  • if you move into Documents then you can then upload files如果你进入Documents那么你可以上传文件
sftp> cd Documents
sftp> put /Users/<USER>/tmp
Uploading /Users/<USER>/tmp to /Documents/tmp
/Users/<USER>/tmp                                                                       100%    0     0.0KB/s   00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM