There are 3 ways to change user of a process in Unix.
2 system level ways to change user of a process
- if the process has capability CAP_SETUID, traditionally root has this capability (and all other capabilities), then it can use
setuid, setreuid, setresuid, setfsuid, system calls, to change to any other user. Any other user can shuffle uids: A process has 3 uids, it can move them around, at will: it can swap them, or remove them until it is down to one. It can not add uids, unless it has capability CAP_SETUID. In general a process can only loose privileges or move them around, using these system calls. These calls allow the program to continue.
exec a suid executable: If an executable file has its suid bit set, and if it is of a valid type (not a scripts, not java, not …), then when it is run, its effective user id is changed to that of the files owner. (same can be done for group with sgid bit). This is the only way to gain privileges. The current program ends when exec is called, it is replaced with the new program, but it is the same process, it also inherits open files (e.g. stdin, stdout, stderr).
fork dose not change user.
A forked process is an exact duplicate of its parent, with a few exceptions (see man fork). In particular the uid, gid, and capabilities are not changed.
Utility methods
These programs use the 2 system methods described above.
- use
sudo or su:
su will ask for the password of the other user.
sudo will ask for your password, but will only work if you are registered in the sudoers file.
sudo, su, login, cron etc use the 2 system methods. (And will create a new process. The other system methods do not create a new process.)
What does sudo, su do?
#↳ ll /usr/bin/sudo
-rwsr-xr-x 1 root root 155K Sep 9 2017 /usr/bin/sudo*
As use can see the sudo executable is owned by root, and has the suid bit set (the s, where you would expect to see the first x).
When sudo is run, it runs as root (don't try this, unless you know what you are doing). It then does security checks. Then it uses set??uid to become the required user, it then execs (and maybe a fork) the required program.
Running a process, without logging in
Use some timed start service.
Send a network message, e.g. a web-server may run a task in response to a web request.
Use automated login: use ssh to launch a process, via a script on another machine.
crontab -u user -e– RubberStamp Dec 19 '18 at 13:53If I want to run a process ~as~ and become its owner without logging in .... The question is now a completely different topic.
– RubberStamp Dec 19 '18 at 13:59do_command.c...static void child_process __P((entry *, user *)), do_univ __P((user *));... but this is a simple cursory look. – RubberStamp Dec 19 '18 at 14:23setuid()&co. When a user logs in, the programs they're using to log in is doing the same -- with the inconsequential difference that it may write some useless & untrustworhy garbage to/var/run/utmp.cronis switching the user without doing that -- thence the nebulous idea that it somehow runs as a user without that user being "logged in". – Dec 19 '18 at 15:56systemd-logindorConsoleKitcares about that, the kernel, doesn't. There're so many commonly used concept not defined at kernel-level, I think you should know that. Since you can always change userspace apps you use(Yeah, that's why sometimes I say open source sucks), all concept defined in userspace might change with the distro. – 炸鱼薯条德里克 Dec 20 '18 at 04:51login? Dosuandsudocount as login? Doescroncount as login?sshdinvokes programlogin, sosshdis also login? – Tim Dec 20 '18 at 05:07suorsudodoesn't create logind session, so common answer for this situation is no. But for human, you get an CLI or GUI or control board or vending machine button or a key of a locker, you logined into the system. – 炸鱼薯条德里克 Dec 20 '18 at 05:16