在继续阅读此答案之前,您应该先阅读Eliah Kagan的答案。他解释了如何处理这种情况以及为什么通常不需要手动更改/ etc / passwd。
无论如何,如果您真的知道自己在做什么,并且需要/etc/passwd手动进行编辑,就可以做到,但是仍然不应该简单地使用自己喜欢的编辑器来更改文件。而是有工具
vipw
从手册页:
The vipw and vigr commands edits the files /etc/passwd and /etc/group,
respectively. With the -s flag, they will edit the shadow versions of those
files, /etc/shadow and /etc/gshadow, respectively. The programs will set
the appropriate locks to prevent file corruption.
例如,据我所知,如果要更改用户UID,以手动编辑文件是唯一的方法。同样,如果您想更改用户密码,然后又不知道又返回到以前的密码,则大约无法进行usermod。但是,如果您将散列密码保存在影子文件中,然后更改用户密码,则可以随后使用再次添加散列密码以编辑影子文件vipw -s。