macos 在 Mac 上的终端窗口中切换用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5671046/
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-10-21 07:57:39  来源:igfitidea点击:

Switch user in a terminal window on a Mac

macosunixterminal

提问by user633101

Could anybody help with simple question. I need login/switch user in command line in 1 line; For example: login -p username ? password.

任何人都可以帮助解决简单的问题。我需要在命令行中的 1 行登录/切换用户;例如: login -p username ? password

What should I write at '?' position? Because I need to login in script which don't know id system asked password...

我应该在'?'写什么 位置?因为我需要在不知道 id 系统要求密码的脚本中登录...

回答by ppolyzos

You could try to use

你可以尝试使用

su - username

in order to switch to the user you want.

以便切换到您想要的用户。

You can find more documentation about sucommand here

您可以在此处找到有关su命令的更多文档

回答by Jonathan Leffler

You should probably investigate sudo.

你应该调查一下sudo

回答by Anne

You can provide the password to sudo like this:

您可以像这样向 sudo 提供密码:

echo "p@sw0rd" | sudo -S cal -y 2011

The example above runs 'cal -y 2011' with root permissions.

上面的示例以 root 权限运行“cal -y 2011”。

回答by prodigitalson

Im pretty sure you cant use loginnon-interactively (same for su) unless youre logging in as the already logged in user or unless the logincommand is invoked by root. However, if you meet either one of those requirements you would use the -fswitch.

我很确定您不能以login非交互方式使用(与 相同su),除非您以已经登录的用户身份登录,或者除非该login命令由 root 调用。但是,如果您满足其中任何一个要求,则可以使用该-f开关。

manPage: http://www.manpagez.com/man/1/login/

man页面:http: //www.manpagez.com/man/1/login/

you could of course use sshbut it seems wrong to ssh locally... You could also use expectto script the interactive login process.

您当然可以使用,ssh但在本地 ssh 似乎是错误的...您也可以使用expect脚本来编写交互式登录过程。