bash 如何从linux api中的已知uid获取用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21816560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:35:17  来源:igfitidea点击:

How to get username from know uid in linux api?

linuxbashapigcc

提问by techfun

I am new in linux. I need to know user name from given user id in my c application. Is there any linux apifunction available to get this?

我是新来的linux。我需要知道我的 c 应用程序中给定用户 ID 的用户名。有什么linux api功能可以得到这个吗?

Thanks

谢谢

采纳答案by fimad

If you want to get the username from a c program then getpwuidis probably the easiest way. Man entry here: http://man7.org/linux/man-pages/man3/getpwnam.3.html

如果您想从 ac 程序中获取用户名,那么getpwuid可能是最简单的方法。男人入口在这里:http: //man7.org/linux/man-pages/man3/getpwnam.3.html

From the command line, I would just grep through passwd:

在命令行中,我只会通过 passwd 进行 grep:

grep 1000 /etc/passwd | cut -f1 -d: