2014-09-11 38 views
7

yapar: i piton ./myCreate.py çalıştırdığınızda
fo = open("./testFile.txt", "wb")i myCreate.py denilen piton script basit parça linux üzerinde çalışan var onun sahibi kök

- sahibidir testFile.txt kullanıcım kalır. Sudo python çalıştırdığımda ./myCreate.py - testFile.txt dosyasının sahibi artık root'dur. infaz

ikisine de daha önce çalıştırılmadı

testfile.txt

nasıl ben dosyanın sahibi gerçek kullanıcı ve etkili kullanım kalmak yapabilir ?! Teşekkürler!

+0

'it chown' 5gon12eder. –

+0

Gerçek kullanıcı olarak açılmasını istiyorum, bunun kim olduğunu düşünmeden ... sabit kodlanmış bir şey değil – ABR

+0

Geçerli kullanıcıyı belirleyebilir ve bunu seçebilirsin. –

cevap

6

root olarak çalıştırmak demektir. Yani normal dosya dosyanız root'a ait.

Yapabilecekleriniz, oluşturulduktan sonra dosyanın sahipliğini değiştirmek. Bunu yapmak için hangi kullanıcının sudo çalıştırdığını bilmeniz gerekir. Neyse ki, sudo kullandığınızda ayarlanmış bir SUDO_UID ortam değişkeni var.

Yani, bunu yapabilirsiniz:

import os 
print(os.environ.get('SUDO_UID')) 

Sonra change the file ownership gerekir:

os.chown("path/to/file", uid, gid) 

Beraber koyarsanız: Elbette

import os 

uid = int(os.environ.get('SUDO_UID')) 
gid = int(os.environ.get('SUDO_GID')) 

os.chown("path/to/file", uid, gid) 

, sen olacak Bir işlev olarak istiyorum, çünkü daha kullanışlı, yani:

import os 

def fix_ownership(path): 
    """Change the owner of the file to SUDO_UID""" 

    uid = os.environ.get('SUDO_UID') 
    gid = os.environ.get('SUDO_GID') 
    if uid is not None: 
     os.chown(path, int(uid), int(gid)) 

def get_file(path, mode="a+"): 
    """Create a file if it does not exists, fix ownership and return it open""" 

    # first, create the file and close it immediatly 
    open(path, 'a').close() 

    # then fix the ownership 
    fix_ownership(path) 

    # open the file and return it 
    return open(path, mode) 

Kullanımı:

# If you just want to fix the ownership of a file without opening it 
fix_ownership("myfile.txt") 

# if you want to create a file with the correct rights 
myfile = get_file(path) 

DÜZENLEME: Robᵩ @, @Basilevs cevabım sayesinde Güncelleme ve @ oluşturduktan sonra

+0

Harika bir açıklama, ancak SUDO_UID, SUDO_USER'den daha uygun olmaz mıydı? –

+0

@ Robᵩ veya daha güvenli? – Basilevs

+0

"SUDO_USER" ayarlanmamışsa dosyayı 'chown' yapmamak daha taşınabilir olacaktır. – 5gon12eder

2

İlk olarak, içeren klasörün izinlerini almak ve sonra da dosya oluşturulmasına uygulamak için os.stat'u kullanmayı öğrenin.

bu (python2 kullanarak) gibi görünecektir: Elliot gibi

import os 

path = os.getcwd() 
statinfo = os.stat(path) 

fo = open("./testFile.txt", "wb") 
fo.close() 
euid = os.geteuid() 
if (euid == 0) # Check if ran as root, and set appropriate permissioning afterwards to avoid root ownership 
    os.chown('./testFile.txt', statinfo.st_uid, statinfo.st_gid) 

aynı anda birkaç dosya yaratıyor olsaydı, bu bir fonksiyonu olarak daha iyi yapılandırılmış olacağını işaret etti.

Uygun kullanıcı kimliği bulmak için os.environ kullanarak Kullanım os.chown()
2

: sudo ile komut çalıştırma

import os 

fo = open("./testFile.txt", "wb") 
fo.close() 
os.chown('./testFile.txt', 
     int(os.environ['SUDO_UID']), 
     int(os.environ['SUDO_GID'])) 
İlgili konular