Hoffmann
Hoffmann

Reputation: 1082

Setting mysql as shell

I have a user on my machine that is only supposed to run mysql. Is there any way that I can set the shell of that user to mysql and login using password and username?

I know how to change the shell to the mysql binary

usermod -s /usr/bin/mysql

That is working indeed, only I can't provide a username/password in the program. Usually user/pw are given as

mysql -u $USER -p

I can not provide parameters for a shell as in

usermod -s "/usr/bin/mysql -u $USER -p" # Does not work!

Also using a simple shell-script as shell does not work:

#!/bin/sh # mysqlShell
/usr/bin/mysql -u $USER -p
----
usermod -s mysqlShell # does not work

So how can I provide parameters to a program I use as a shell for a user?


Thanks to Tom Regner I could figure out a solution using .my.cnf containing

[client]
host=localhost
user=$user
password=$pass
disable-auto-rehash

where mysql is set to the shell. I still would like give the password manually but this is the best I found.

Upvotes: 4

Views: 728

Answers (3)

user594138
user594138

Reputation:

Setup a $HOME/.my.cnf file for the user

[client]
host=localhost
user=mysqluser
password=mysqlpass

then set a bash as login shell and put the following in $HOME/.bashrc

exec mysql --host=localhost dbname

that should do what you want, while the user in question just has to give one password (the system account password on login).

exec replaces the shell process with the mysql process.

If this does not work as expected, you may need to adjust $HOME/.bash_profile to source .bashrc:

[[ -f ~/.bashrc ]] && . ~/.bashrc

It might be enough to provide an appropriate .my.cnf and setting /usr/bin/mysql as shell, but this way you can pass arbitrary commandline options/flags to the mysql client.

Upvotes: 2

You need a login password (unless you set up ssh appropriately). Use the following command: sudo passwd username to change that login password.

You also need a mysql password. Use SET PASSWORD Mysql request.

If you want the user to be connected to some fixed database with some fixed password, code a small C wrapper (then, make the executable only executable by your Unix user) doing mysql_real_connect, or calling some exec function for mysql --user=username --password=password databasename but I don't recommend doiing the later (because ps aux will show the password, and that is a security risk).

Perhaps, since MySQL is free software, you could customize the source code of mysql for your particular needs.

Perhaps using a restricted shell and carefully configuring it is better.

Upvotes: 1

Photon
Photon

Reputation: 3222

You can do that by editing the user's account details in the /etc/passwd and change the default shell.

Upvotes: 1

Related Questions