su 和su -的区别

参考文章:(总结)Linux下su与su -命令的本质区别

  我一直是习惯使用su username来切换用户的,而且并不知道su和su -有什么区别,一直以为是没啥区别的,但是在一个测试中,在测试的同事的指导下,他说你su - ××× 之后×××,那一刻突然福至心灵觉得可能二者是有区别的,然后执行了su - 用户 之后,果然得到了su 用户的时候得不到的结果。。。。。然后还发现,以前执行db2的命令要cd /opt/IBM/db2/V9.7.5/,然后bin/db2 ×××真的有够麻烦的,但是su -之后,直接db2就可以执行命令了,所以有必要来分析一下这两个命令到底有什么区别。

su命令和su -命令最大的本质区别就是:
- 前者只是切换了root身份,但Shell环境仍然是普通用户的Shell
- 而后者连用户和Shell环境一起切换成root身份了。只有切换了Shell环境才不会出现PATH环境变量错误
- su切换成root用户以后,pwd一下,发现工作目录仍然是普通用户的工作目录;而用su -命令切换以后,工作目录变成root的工作目录了。用echo $PATH命令看一下su和su -以后的环境变量有何不同。以此类推,要从当前用户切换到其它用户也一样,应该使用su -命令。

  • 在LINUX和AIX系统上还是有一些区别,AIX上一定要su -

  • 不带-的su不会读取目标用户的环境配置文件,带-的su才会读

去哪里读取配置文件?

使用su切换到root:

[ltt@db22 Desktop]$ pwd
/home/ltt/Desktop
[ltt@db22 Desktop]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/ltt/bin
[ltt@db22 Desktop]$ su
Password: 
[root@db22 Desktop]# pwd
/home/ltt/Desktop
[root@db22 Desktop]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/ltt/bin

使用su -切换到root:

[ltt@db22 Desktop]$ pwd
/home/ltt/Desktop
[ltt@db22 Desktop]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/ltt/bin
[ltt@db22 Desktop]$ su -
Password: 
[root@db22 ~]# pwd
/root
[root@db22 ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

PATH的配置文件

一般PATH在 HOME/.bashrc HOME/.bash_profile中设置,我们来看一下

[ltt@db22 ~]$ pwd
/home/ltt
[ltt@db22 ~]$ ls -la
total 156
drwx------. 26 ltt  ltt  4096 Jul 31 11:55 .
drwxr-xr-x.  6 root root 4096 May 10 10:49 ..
drwxrwxr-x.  2 ltt  ltt  4096 Apr 10 11:37 .abrt
-rw-------.  1 ltt  ltt    31 Jul 31 11:46 .bash_history
-rw-r--r--.  1 ltt  ltt    18 Feb 22  2013 .bash_logout
-rw-r--r--.  1 ltt  ltt   176 Feb 22  2013 .bash_profile
-rw-r--r--.  1 ltt  ltt   124 Feb 22  2013 .bashrc
... ... ...

.bashrc文件

[ltt@db22 ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions
[root@db22 ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

.bash_profile文件

[ltt@db22 ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
[root@db22 ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

所以读取的配置的文件应该是 .bash_profile文件,这个文件中会导入 .bashrc的定义。.bashrc文件主要定义一些命令别名和函数,PATH变量设置在.bash_profile文件中。

  • 13
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值