在 /etc/passwd 文件中用 /bin/false 替换 /bin/bash

5 script bash solaris

我正在编写一个程序来禁用系统中的用户,我想替换/bin/bash/bin/false.

例子

xxx:x:1:22:xx:/export/home/xx:**/bin/bash**
Run Code Online (Sandbox Code Playgroud)

替换为

xxx:x:1:22:xx:/export/home/xx:**/bin/false**
Run Code Online (Sandbox Code Playgroud)

我想使用 bash 脚本。

我知道一种方法是使用sed. 但我不擅长正则表达式。

任何人都可以帮忙吗?

0xC*_*22L 8

好吧,您根本不使用sed正则表达式或正则表达式。您所做的是使用程序chsh来更改用户的外壳。

chsh -s /bin/false username
Run Code Online (Sandbox Code Playgroud)

或者:

usermod -s /bin/false username
Run Code Online (Sandbox Code Playgroud)

如果您想用实际的外壳替换它,您还必须确保它列在/etc/shells.


小智 5

请注意,这chsh并不总是可用!例如在alpine Linux 或使用busybox 的嵌入式系统中..

简单的内联sed命令:

sed -i '/www-data/s/false/sh/g' /etc/passwd

(此示例将用户www-data的 shell 从/bin/false更改为/bin/sh