比思論壇

標題: 常用Linux命令合集(二) [打印本頁]

作者: wangsun13    時間: 2012-11-14 12:55
標題: 常用Linux命令合集(二)
1.2 用户和用户组管理
1.2.1 创建用户:useradd [-u UID] [-g initial_group] [-G other_group] [-d 用户目录]
         添加Linux用户一般使用root用户来添加。Useradd命令不指定-d参数时,表示该用户的home目录为:/home/用户名。
  例如添加所属组为mysql组的用户名为amigo的用户,所用命令参考如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# useradd -g mysql amigo
1.2.2 设置用户密码:passwd 用户名
         使用passwd命令设置用户的密码,例如设置amigo用户的密码,可使用:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# passwd amigo
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]Changing password for user amigo.
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]New UNIX password:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]

         输入一次密码,按Enter,再输入确认密码按Enter后完成设置。
         若要设置root用户的密码,直接用“passwd”命令,后面不用加用户名就可以。
1.2.3 删除用户:userdel [-r] [用户帐号]
         userdel可删除用户帐号与相关的文件。若不加参数,则仅删除用户帐号,而不删除相关文件。-f 删除用户登入目录以及目录中所有文件。
         例如:删除amigo1121用户,但不删除其所在的目录(例如:/home/amigo1121目录),参考命令为:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# userdel amigo1121
         如果想删除用户的同时,将其用户目录删除,例如删除amigo用户及其用户目录,参考命令为:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc home]# userdel -r amigo
1.2.4 切换用户:su - 用户名
         在某个用户下面操作时,可以进行用户切换,例如在sn用户下切换到amigo用户,所用命令为:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][sn@minicc ~]$ su - amigo
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]Password:

         输入amigo用户的密码后按Enter成功进行切换。若在root用户下进行用户切换,只需要运行“su – 用户名”,不用输入密码就能切换,因为root用户具有最高权限。
1.2.5 设置用户环境变量:.bash_profile
         在Windows下点击“我的电脑”->“属性”->“高级”->“环境变量”,可以进行用户环境变量的设置,在Linux下用户的环境变量在该用户根目录(例如:/home/amigo目录)的.bash_profile文件中,可使用vi等文件编辑命令进行设置。
  该文件的初始内容参考如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]# .bash_profile
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]# Get the aliases and functions
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]if [ -f ~/.bashrc ]; then
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]        . ~/.bashrc
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]fi
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]# User specific environment and startup programs
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]PATH=$PATH:$HOME/bin
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]export PATH
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]

   例如,若想在该用户下配置MySQL数据库的访问,.bash_profile文件修改参考如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]# .bash_profile
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]# Get the aliases and functions
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]if [ -f ~/.bashrc ]; then
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]        . ~/.bashrc
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]fi
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]# User specific environment and startup programs
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]SDFDB=host@localhost:user@mysql:passwd@mysql123:db@sdp:charset@utf8:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]export SDFDB
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]MYSQLDIR=/home/mysql
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]export MYSQLDIR
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]PATH=$PATH:$MYSQLDIR/bin
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]LD_LIBRARY_PATH=$MYSQLDIR/lib/mysql:$LD_LIBRARY_PATH
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]export LD_LIBRARY_PATH
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]PATH=$PATH:$HOME/bin
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]export PATH
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]export DBTESTPORT=22
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]unset USERNAME

   需要注意的是,要使该文件生效,需要在编辑完.bash_profile文件后运行如下命令:
[amigo@minicc ~]$ source .bash_profile
        若要使所设置的用户环境变量在该用户启动的应用程序生效,最好关闭该窗口,新开一个窗口,执行该应用程序的重启操作。
1.2.6 创建工作组:groupadd 工作组名
        若要创建test1121的工作组,参考命令如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# groupadd test1121
1.2.7 删除工作组:groupdel 工作组名
         例如,删除test1121的工作组,参考命令如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# groupdel test1121
1.2.8 更改用户所属工作组:usermod -g 工作组名 用户名
         例如更改amigo用户(创建时设置的工作组为mysql)的工作组为test1121,命令如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# usermod -g test1121 amigo
1.2.9 查看用户的身份:id用户名
         查看用户的身份可使用id命令,例如查看amigo用户的身份,参考运行结果如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# id amigo
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]uid=543(amigo) gid=542(test1121) groups=542(test1121)

         如果要查看root用户的身份,可直接使用“id”命令,参考结果如下:
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img][root@minicc ~]# id
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),
[img]5e345eee-d438-4d46-9506-c99db340213f_0_files/None[1].gif[/img]6(disk),10(wheel)



作者: shiyang512    時間: 2012-11-14 13:18
正在学操作系统,   不过linux 完全没基础
作者: ZHAOBU1    時間: 2012-11-14 13:39
看不懂!但顶你
作者: txzslgjb    時間: 2012-11-28 18:03
完全不懂,,,,,,
作者: chengxinpeng    時間: 2012-12-5 09:40
受用了,谢谢




歡迎光臨 比思論壇 (http://108.170.5.98:8080/) Powered by Discuz! X2.5