您的位置:首页 > 运维架构 > Linux

[技巧]如何防止Linux命令行下MySQL登录密码泄露?

2017-03-17 08:47 381 查看



命令行登录防止MySQL密码泄露的几个小妙招。

明知山有虎偏向虎山行的方案:

1、可以通过如下环境变量强制Linux不记录敏感历史命令

在命令行执行HISTCONTROL=ignorespace后,再输入带密码的命令的前面加一个空格登录,登录命令不会被记录到历史记录里。
[root@yunweipai~]# HISTCONTROL=ignorespace

#<==这里是临时生效,要想永久生效,请放入/etc/bashrc。
[root@yunweipai~]#  mysql -uroot -p'yunweipai123'

#<==命令的开头要多一个空格。

2、操作完敏感的命令后可以及时删除命令行记录

执行“history -d 历史命令序号” 清除指定历史记录命令
[root@yunweipai~]# history|tail -4

#<==显示历史记录。

252  mysql -uroot -p'yunweipai123'

#<==此条带密码,敏感,待删除。

253 pwd

254 history

255 history|tail -4
[root@yunweipai~]# history -d 252

#<==删除序号为252的历史记录。
[root@yunweipai~]# history|tail -5

252 pwd

#<==序号252对应的带密码登录的命令已经消失。

253 history

254 history|tail -4

255 history -d 252

256 history|tail -5

执行“history -c”清除所有所有记录
[root@yunweipai~]# history -c [root@yunweipai~]# history

1 history

执行“>~/.bash_history”清除历史记录文件

3、给带密码的启动脚本以及备份脚本等加700权限,用户和组改为root。

chmod700 /data/3306/mysql

#<==可以采用kill信号的关闭方式数据库,从而防止密码泄露。

chmod700 /server/scripts/bak.sh

#<==将密码写入my.cnf配置文件,使得执行备份命令不需要加密码。

4、把密码写入my.cnf配置文件并加700权限,用户和组改为mysql。
[root@yunweipai~]# cp /application/mysql/my.cnf /etc/ [root@yunweipai~]# grep -A 2 client /etc/my.cnf

#<==配置文件开头添加如下三行,无需重启系统。
[client]

#<==客户端模块标签。

user=root

#<==用户参数及密码。

password=yunweipai123

#<==密码参数及密码。
[root@yunweipai~]# mysql

#<==此时登录数据库就不用输入密码了。

Welcometo the MySQL monitor.  Commands end with; or \g.

YourMySQL connection id is 8

Serverversion: 5.6.34 Source distribution

...省略若干行...

Type'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

知道山上有老虎,就不去的的方法:
[root@yunweipai~]# mysql -uroot -p

#<==这里标准dba命令行登陆命令,交互式输入密码可有效防止密码泄露。

Enter password:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: