Skip to content

files.get with sudo fail with "Permission denied" #1440

@KarolBedkowski

Description

@KarolBedkowski

Hi,

Describe the bug

Using files.get(..., _sudo=True) fail with PermissionError; example:

--> Starting operation: download etc files | get  
[rockpi.home.arpa]  >>> sudo -H -n sh -c '! (test -e /etc/nftables.conf || test -L /etc/nftables.conf ) || ( stat -c '"'"'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N'"'"' /etc/nftables.conf 2> /dev/null || stat -f '"'"'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY'"'"' /etc/nftables.conf )'
[rockpi.home.arpa]  user=root group=adm mode=-rwxr-x--- atime=1712484978 mtime=1756193730 ctime=1756193730 size=8185 '/etc/nftables.conf'
    [rockpi.home.arpa]  Loaded fact files.File (path=/etc/nftables.conf)
[rockpi.home.arpa]  >>> sudo -H -n sh -c 'cp /etc/nftables.conf /tmp/user/0/pyinfra-a302f6cd8526066f2904619397ee54c2660d795a && chmod +r /tmp/user/0/pyinfra-a302f6cd8526066f2904619397ee54c2660d795a'
[rockpi.home.arpa]  >>> sudo -H -n sh -c 'rm -f /tmp/user/0/pyinfra-a302f6cd8526066f2904619397ee54c2660d795a'
    [rockpi.home.arpa]  Command socket/SSH error: PermissionError(13, 'Permission denied')
    [rockpi.home.arpa]  Error: executed 0 commands

File is copied to tmp dir but instead of use tmp of user that is used for connection, is used root's tmp dir (/tmp/user/0/) and other users don't have access to it (all dirs in /tmp/user are 700).

On error local file is truncated.

Not sure how to fix it without breaking other things...

To Reproduce

files.get(
        name="get ",
        src="/etc/nftables.conf",
        dest="nftables.conf",
        force=True,
        _sudo=True,
    )

Target os: Debian 13.

Expected behavior

With sudo there should be possible to get any file.

Meta

  • Include output of pyinfra --support.
    If you are having issues with pyinfra or wish to make feature requests, please
    check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
    When adding an issue, be sure to include the following:
 
    System: Linux
      Platform: Linux-6.16.1-x64-k-xanmod1-x86_64-with-glibc2.41
      Release: 6.16.1-x64-k-xanmod1
      Machine: x86_64
    pyinfra: v3.4.1
      click: v8.1.8
      click: v8.1.8
      click: v8.1.8
      distro: v1.9.0
      gevent: v24.11.1
      jinja2: v3.1.6
      packaging: v24.2
      paramiko: v3.5.1
      python-dateutil: v2.9.0.post0
      pywinrm: v0.5.0
      typeguard: v4.4.2
      typing-extensions: v4.12.2
    Executable: /home/k/.local/bin/pyinfra
    Python: 3.13.7 (CPython, GCC 14.3.0)
  • How was pyinfra installed (source/pip)?

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugLabel for all kind of bugs.operationsIssues with operations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions