Linux ubuntu sudo 用法

crazyjin · 2013年05月16日 · 最后由 vincenttone 回复于 2013年05月17日 · 4094 次阅读

我安装了 mongodb 到/opt/mongodb 目录下,然后 export PATH=$PATH:/opt/mongodb/bin

重启 shell 执行: crazyjin@T500:~$ sudo mongod sudo: mongod:找不到命令

切换到 root 后则可以找到命令,mongod 正常运行 crazyjin@T500:~$ sudo su root@T500:/home/crazyjin# mongod mongod --help for help and startup options ……

不用 root 权限也可以执行,但是: Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

何解?

try sudo -E mongod

你的环境变量是设置在当前用户下的,所以你用 sudo 的时候不行。

#1 楼 @iBachue i tried.don't work.

#2 楼 @vincenttone crazyjin@T500:~$ echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/crazyjin/sda3/myscript/:/opt/jdk1.6/bin:/opt/jdk1.6/jre/bin:/opt/mongodb2.4.3/bin crazyjin@T500:~$ sudo echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/crazyjin/sda3/myscript/:/opt/jdk1.6/bin:/opt/jdk1.6/jre/bin:/opt/mongodb2.4.3/bin crazyjin@T500:~$ 这可以说明问题么?

chown <UR USERNAME> /data/db

#5 楼 @fenprace that should be a good idea to solve my problem. but what's going wrong?

Because it said "Permission denied" 😊 Mongodb created /db/data as the default directory to store the database after installing, and it belongs to root defaultly.

#7 楼 @fenprace i tried to start mongod under the priority of root: sudo mongod but it always return this: crazyjin@T500:/$ sudo mongod sudo: mongod:找不到命令

that's what makes me confused.

#8 楼 @crazyjin Your PATH varibale is different from root's, you could run it to prove it:

$ echo $PATH
$ sudo su
# echo $PATH

To solve your problem, you could simply create a symolic link:

sudo ln -s /opt/mongodb/bin/* /usr/local/bin

#9 楼 @fenprace crazyjin@T500:/$ echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/crazyjin/sda3/myscript/:/opt/jdk1.6/bin:/opt/jdk1.6/jre/bin:/opt/mongodb2.4.3/bin crazyjin@T500:/$ sudo su root@T500:/# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/crazyjin/sda3/myscript/:/opt/jdk1.6/bin:/opt/jdk1.6/jre/bin:/opt/mongodb2.4.3/bin root@T500:/#

when i ln /opt/mngodb/bin/mongod to /usr/local/bin/mongod as a symbolic link and i excutes sudo mongod it works well.

i read manpage of sudo.but i can not understant it.it looks like there are some security policies there. The symbolic links solved my problem. thank you.

#9 楼 @fenprace If you use Ubuntu and some other linux distributions, the root password is unknown actually. You can have a try to run su, and input your password. You'll get an error.

To run a shell as the root, you can run sudo su, it means running su as the root. And the su command will start a shell. Simply running sudo bash is also available.

This problem won't happen on Archlinux, Gentoo and most of the server linux distributions. Because on these linux distributions, the root's password is set by user before the installation and the password is known. So if you are using one of the above, just running sualso works well.

#4 楼 @crazyjin

sudo su
export PATH=$PATH:/opt/mongodb/bin
#mongod相关操作

不过我认为数据库操作不必使用 root,应该有独立的用户。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号