su不会将所有内容更改为其他用户(cgroups)

时间:2016-08-24 10:41:00

标签: ssh systemd su

如果我运行此命令:

su -l otheruser -c 'strace /usr/lib/systemd/systemd --user 2> /tmp/su.err'

失败了:

  

无法创建根cgroup层次结构:权限被拒绝

     

无法分配经理对象:权限被拒绝

我在strace输出中看到,作为用户启动systemd在这里失败了:

mkdir("/sys/fs/cgroup/systemd/user/root/754/systemd-3893", 0755) = -1 
     EACCES (Permission denied)

/ sys / fs / cgroup / systemd / user / root /来自哪里?

如果我通过ssh向localhost运行相同的命令,它可以工作:

ssh otheruser@localhost 'strace /usr/lib/systemd/systemd --user 2> /tmp/ssh.err'

这里使用了正确的目录:

mkdir("/sys/fs/cgroup/systemd/user/modwork_gew_dfj/825/systemd-4272", 0755) = 0

为什么它通过ssh工作,而不是通过su?

版本:su(GNU coreutils)8.17

更新

在这里,您可以看到我的su版本

不会更改cgroup
host:~ # su -l otheruser
otheruser@host:~$ cat /proc/$PPID/cgroup
10:hugetlb:/
9:perf_event:/
8:blkio:/
7:net_cls:/
6:freezer:/
5:devices:/
4:memory:/
3:cpuacct,cpu:/
2:cpuset:/
1:name=systemd:/user/root/5913 <################ root

通过ssh

host:~ # ssh otheruser@host
otheruser@host:~$ cat /proc/$PPID/cgroup
10:hugetlb:/
9:perf_event:/
8:blkio:/
7:net_cls:/
6:freezer:/
5:devices:/
4:memory:/
3:cpuacct,cpu:/
2:cpuset:/
1:name=systemd:/user/otheruser/5919 <################ otheruser

UPDATE2

我的su版本不会更改cgroup(请参阅用户“ax。”答案中的链接)。有没有办法改变cgroup(之前或之后)调用su

UPDATE3

此版本没有此问题:su util-linux 2.25

2 个答案:

答案 0 :(得分:3)

su inherits its cgroup from the originating session,而不是传递给su的用户。因此,当您以root身份调用su -l otheruser -c systemd ...时,systemd会尝试将根cgroup(/sys/fs/cgroup/systemd/user/root/...)用作otheruser并失败。

使用ssh otheruser@localhost ...,用户和cgroup都是otheruser,一切都按预期工作。

答案 1 :(得分:0)

guettli指出su不再工作了。 在centos7.2作为root我试过这似乎适用于uid的cgroup: 假设你有uid = 1000这是一个高cpu共享用户而uid = 1001是一个低cpu共享用户,(我猜测默认情况下每个新用户获得的份额为1024,这将是root用户的情况(uid) = 0))

在centos7.2作为root我试过这似乎适用于cgroup:

systemd-run --uid=1000 --slice=user-1000.slice do_uid_1000_work_commands
systemd-run --uid=1001 --slice=user-1001.slice do_uid_1001_work_commands

上面将使用/ run / systemd / system /下的相应用户切片配置创建两个adhoc服务:

/run/systemd/system/*10345*

/run/systemd/system/run-10345.service

/run/systemd/system/run-10345.service.d:

50-Description.conf 50-ExecStart.conf 50-Slice.conf 50-User.conf

以下是我的其他配置: - &GT; /etc/systemd/system/user-1000.slice.d/50-CPUShares.conf

[Slice]
CPUShares=4096

- &GT; /etc/systemd/system/user-1001.slice.d/50-CPUShares.conf

[Slice]
CPUShares=1024

- &GT; /usr/lib/systemd/system/user-1001.slice

[Unit]
Description=User and Session Slice for uid = 1001 (low cpu share user)
Documentation=man:systemd.special(7)
Before=slices.target

[Service]
Slice=user-1001
CPUShares=1024

- &GT; /usr/lib/systemd/system/user-1000.slice

[Unit]
Description=User and Session Slice for uid = 1000 (high cpu share user)
Documentation=man:systemd.special(7)
Before=slices.target

[Service]
Slice=user-1000
CPUShares=4096