Which user is running process linux

How do I find out, which user is running the current php script?

How do we determine which user the php script is running under when I run the script on server? Is it running under the same user as apache or phpmyadmin by chance? My question maybe wrongly framed but I want to know which user so that I set appropriate permission for different folders in /var

@rambocoder Because it is so decent it is so easy to google which means someone is very lazy. 🙂

5 Answers 5

Hmm second person downvote after it was edited. As it stands this does work see stackoverflow.com/questions/7771586/…

While this works it isn’t a particularly effective solution, and the solution below it using posix_getpwuid(posix_geteuid()) is both more efficient and able to work on servers with shell exec locked down.

I prefer the id command myself, you’ll get an output like this: uid=1004(testsite) gid=1005(testsite) groups=1005(testsite),1004(sftp)

If you have posix functions available (enabled by default in most Linux-based environments) then you can use posix_geteuid and posix_getpwuid to get the name of the user (at least in non-Windows environments) like so:

$pwu_data = posix_getpwuid(posix_geteuid()); $username = $pwu_data['name']; 

Another (more expensive) way to do it would be to use a shell-executing function like exec to run whoami:

or even the backticks (although you may need to trim the linebreak off):

I personally have only ever needed to get the username of the user running the script for PHP scripts that run in the shell (on the command-line). Typically, scripts that run in the process of building the response to a request that the web server is handling will be run as the web server user, such as www-data, apache, etc. In Apache, the user that runs the apache/httpd processes is set with the User directive.

Important note: get_current_user does NOT give you the username of the user running the script, but instead gives you the OWNER of the script. Some of the answers here (appropriately down-voted) are suggesting to use get_current_user , but that will not give you the username of the user running the current script.

Источник

Which user is running a process linux

Yes, both user processes and system processes are processes — hence the naming 😉 A kernel is a part of an OS that implements the concept of processes to allow eg. scheduling of said processes.

Execute process as another user in Linux

I don’t have privileges to comment, so I’ll write this as an answer. One way to do it would be to prompt for a password and validate it yourself, then fork and set the new user for that process. You can check Given a linux username and a password how can I test if it is a valid account? to see how to validate a password, basically it’s generating a hash for the password you have, reading the hash for the user inside /etc/shadow, and comparing both of them. For switching the user, you need to call setuid and setgid(which sets the groupid). Check this answer: Linux C programming execute as user

Читайте также:  Linux mint режим восстановления

You can get the uid and gid for a user from /etc/password. A sample entry in that file would be:

irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin 

The third field is the userid, the fourth field is the groupid, in this case 39 and 39. After calling setuid, your process will be running under the new user’s identity. Then you can start whatever process you want. For this to work, your program must have the setuid bit on.

You don’t need to set the setuid bit in the executable if you run it as root.

Since you’re using fork() already, you may want to call any variant of exec() instead of system(), as system() will create an additional process. See Difference between «system» and «exec» in Linux?

You can use getpwdnam() to read /etc/passwd as it will parse the fields for you. See How to get linux user id by user name?

How to Set Max User Processes on Linux, A single user has the capability to run a large number of processes. Linux is a multi-user operating system. Now, imagine multiple users running tons of processes. Even if each of the processes does not consume too many hardware resources on its own, the sum of all user processes may eventually hog the entire system.

Differences between system processes, and user processes, kernel control paths and kernel thread

In the case of Linux, a task (kernel internal idea of a thread; threads can share resources, like memory and open files; some run only inside the kernel) can run in userland, or (it’s thread of execution) can transfer into the kernel (and back) to execute a system call. A user thread can be highjacked temporarily to execute an interrupt (but that isn’t really that thread running).

That a process is a «system process» or a regular user process is completely irrelevant in Unix, they are handled just the same. In Linux case, some tasks run in-kernel to handle miscellaneous jobs. They are kernel jobs, not «system processes» however.

One big caveat: Text books on complex software products (compilers and operating systems are particularly egregious examples) tend to explain simplistic algorithms (often ones that haven’t been used in earnest for half a century), because real world machines and user requirements are much too complex to be handled in some way that can be described in a structured, simple way. Much of a compiler is ad-hoc tweaks (particularly in the area of code optimization, the transformations are mostly the subset of possibilities that show up in practical use). In the case of Linux, most of the code is device drivers (mentioned in passing as device-dependent in operating system texts), and of this code a hefty slice is handling misbehaving devices, which do not comply to their own specifications, or which behave differently between versions of «the same device». Often what is explained in minute detail is just the segment of the job that can be reduced to some nice theory, leaving the messy, irregular part (almost) completely out. For instance, Cris Fraser and David Hanson in their book describing the LCC compiler state that typical compiler texts contain mostly explanations on lexical analysis and parsing, and very little on code generation. Those tasks are some 5% of the code of their (engineered to be simple!) compiler, and had negligible error rate. The complex part of the compiler is just not covered in standard texts.

Читайте также:  Linux network interface aliases

Q: Are both user processes and kernel processes processes, as opposed to kernel?

I’m not sure if there is a single correct answer, but I’ll give it a try.
Citing from «Operating Systems Design & Implementation» (A. Tanenbaum), 3rd edition, Chapter 2.1 says:

2.1. Introduction to Processes

All modern computers can do several things at the same time. While running a user program, a computer can also be reading from a disk and outputting text to a screen or printer. In a multiprogramming system, the CPU also switches from program to program, running each for tens or hundreds of milliseconds. While, strictly speaking, at any instant of time, the CPU is running only one program, in the course of 1 second, it may work on several programs, thus giving the users the illusion of parallelism. Sometimes people speak of pseudoparallelism in this context, to contrast it with the true hardware parallelism of multiprocessor systems (which have two or more CPUs sharing the same physical memory). Keeping track of multiple, parallel activities is hard for people to do. Therefore, operating system designers over the years have evolved a conceptual model (sequential processes) that makes parallelism easier to deal with. That model, its uses, and some of its consequences form the subject of this chapter.

2.1.1. The Process Model

In this model, all the runnable software on the computer, sometimes including the operating system, is organized into a number of sequential processes , or just processes for short. A process is just an executing program, including the current values of the program counter, registers, and variables.

While I have not yet gotten around to finish reading the book, according to this explanation a «process» is a unit of work that’s executed on the processor and holds all necessary resources (image, state, registers, counters. ).

Answer to edited question

a kernel always runs in kernel mode and uses kernel space only.

That depends on the type of the kernel. A monolithic kernel runs its stuff in a single address space (kernel space), while microkernels may run their kernel processes in user space.

Can «system process» mentioned in the two books run in user space, kernel space, or both?

See above, a system process may run in both modes, depending on the type of kernel.

Are both user processes and system processes processes, as opposed to kernel?

Yes, both user processes and system processes are processes — hence the naming 😉 I don’t understand the part after the comma, though.

Is «system process» different from kernel control path and kernel thread?

Yes. A process (user or system=kernel) is something different.

The kernel control path denotes the sequence of instructions, a kernel thread (aka LWP — lightweight process) is a thread that’s created and scheduled directly be the kernel (as opposed to user threads, which are created by a threading lib).

Conclusion

A process is just a theoretical structure.
A kernel is a part of an OS that implements the concept of processes to allow eg. scheduling of said processes.
A thread is the smallest part of a process that can be scheduled independently.

Try to make a short but hopefully clear it all answer here. Only applied to modern linux kernel.

Читайте также:  Версии линукс для начинающих

There are struct task s, used internally by kernel as minimal schedulable unit, which is always created by kernel code using ring0 and starts running in ring0, but may or may not switch to ring3 later, or switch back to ring0(using the platform specific syscall instructions) later.

A task has many resources or say properties, the most important ones, are, memory space, task id(tid viewd from top-level pid namespace), task group id(pid viewd from top level of namespace). Although in ring3, tasks obviously can’t access even it’s own struct task directly but at least thoes properties I just mentioned are exposed to ring3 by kernel through /proc fs. But modern kenerl might expose it in a weird way, like /proc/[pid]/status use the word «Pid» to refer to task id viewd from the pid namespace associated with its procfs instance.

A task may or may not share these properties with other tasks(except tid viewd from top-level pid namespace), like they might sharing exactly the same one memory space, or having the same task group id.

Now users in ring3, or say userspace, can invent the concept of «thread» and «process». But since they’re purely userspace invention, different people/textbook might use different terminology. So here we only talk about commonly used terminology.

OS-Thread(thread): a synonym of task.

Kernel-Thread: a task that never switch to ring3 and always share the memory space with the kernel.

OS-Process(process): Kernel doesn’t track process’s existence, human just define it as one or more tasks that having the same task group id (pid viewd from top-level pid namespace). Once all these threads died, the process disappeared in human mind.

One important fun fact is, as you should be able to conclude from the definitions above, a OS-Process might contain one or more OS-Threads, these OS-Threads, or say tasks, in one OS-Process are actually more restricted by kernel about the properties they can un share.

Once a task is created, it belongs to some certain process, and this relationship can never change until it dies.

Tasks belonging to one process must share the exact same one memory space.

Tasks belonging to one process can’t stay in different user namespace or pid namespace, these must be shared.

Find the Process That is Using a File in Linux, The lsof command returns the process name, the PID, and the user who is running the process. If the process has threads, we’ll see their identification number, TID, with the task command.

How can I change the owner of a process?

It’s not possible to externally change the user or group of a running process in Linux. What you can do is change the configuration of whatever you’re running to use the new user from the next time it runs. That depends on how you have installed and configured PHP (or rather, whatever is running PHP for you — apache, php-fpm, etc.).

You can’t do it man, if you can kindly share with us. check this link: https://stackoverflow.com/questions/37401774/change-owner-of-a-currently-running-process

Processes in Linux/Unix, Through a 5 digit ID number Unix/Linux keeps an account of the processes, this number is call process ID or PID. Each process in the system has a unique PID. Used up pid’s can be used in again for a newer process since all the possible combinations are used.

Источник

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