3

I am searching a way to replace the mysql user shell configuration in /etc/passwd from command line as there are multiple servers where I want to disable the shell for mysql user.

Actual

mysql:x:498:498:MySQL server:/var/lib/mysql:/bin/bash

Desired

mysql:x:498:498:MySQL server:/var/lib/mysql:/sbin/nologin
  • 4
    Dont use sed! see chsh or usermod. – meuh Jul 31 '16 at 8:57
5

Don't modify /etc/passwd with sed. Use the tools provided for interfacing with that file instead.

In this case, chpass/chsh, or possibly the more general usermod.

On most systems:

$ sudo chpass -s /sbin/nologin mysql

or

$ sudo chsh -s /sbin/nologin mysql

would work.

You don't want to edit /etc/passwd with sed (or with vim/emacs), and definitely not with sed -i.

User @JdeBP reminds me about the vipw utility in the comments below. This is a special command, much like visudo, that lets you edit the /etc/passwd while taking care of file-locking etc. It also does consistency/syntax checking of the saved file before installing the new password file in place of the old one. Refer to the manual for vipw on your system for more information. This is the only safe way to edit the passwords file by hand.

Also note that on some Unix systems, the default login shell is set by other means than through the password file. On OS X, for example, ordinary users don't even have an entry in /etc/passwd.

|improve this answer|||||
  • @SuyashJain The way to modify /etc/passwd is not the sed way. Don't try to use sed to modify important system files that have tools specifically designed to interface these files. Do not teach others the wrong way to manage their systems. – Kusalananda Jul 31 '16 at 9:11
  • I do agree, completely – Suyash Jain Jul 31 '16 at 9:13
  • You must log out and log back in to see this change. – Rahul Jul 31 '16 at 16:14
  • 1
    This answer could be made even better by mentioning vipw, by noting (since the question is a general one that didn't specify one operating system) that it is pw usermod rather than usermod on FreeBSD/PC-BSD, and by mentioning that on the BSDs /etc/passwd is not even the right file to be modifying anyway. – JdeBP Aug 1 '16 at 8:06
  • @JdeBP Thanks for reminding me about vipw. I'm ignoring usermod since it is fairly chimeric between systems. Could you please clarify your last point though, as it doesn't apply to OpenBSD. – Kusalananda Aug 1 '16 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.