设为首页 收藏本站
查看: 1013|回复: 0

[经验分享] linux学习命令总结⑦

[复制链接]

尚未签到

发表于 2018-5-24 08:37:18 | 显示全部楼层 |阅读模式
  #useradd命令:建立用户帐号和创建用户的起始目录,使用权限是超级用户
[root@VM_168_102_centos ~]# useradd test
[root@VM_168_102_centos
~]# id test
uid
=502(test) gid=502(test) groups=502(test)
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test:x:
502:502::/home/test:/bin/bash

  useradd –u UID:创建用户时直接指定UID

[root@VM_168_102_centos ~]# useradd -u 888 test_1
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_1:x:
888:888::/home/test_1:/bin/bash

  useradd –g GID:创建用户时直接指定GID,GID要事先存在

[root@VM_168_102_centos ~]# useradd -g 500 test_2
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_2:x:
889:500::/home/test_2:/bin/bash
[root@VM_168_102_centos
~]# id test_2
uid
=889(test_2) gid=500(wanghan) groups=500(wanghan)
[root@VM_168_102_centos
~]# useradd -g 777 test_3
useradd: group
'777' does not exist

  useradd –G GID:创建用户时直接指定额外组,但组要事先存在

[root@VM_168_102_centos ~]# useradd -G 500 test_3
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_3:x:
890:890::/home/test_3:/bin/bash
[root@VM_168_102_centos
~]# id test_3
uid
=890(test_3) gid=890(test_3) groups=890(test_3),500(wanghan)

  useradd –d:创建用户时指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主目录

[root@VM_168_102_centos ~]# useradd -d /home/qidian -m test_6      
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_6:x:
893:893::/home/qidian:/bin/bash
[root@VM_168_102_centos
~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_2  test_3  wanghan


  说明: –m:创建用户时,强制给用户创建主目录
  useradd –M:创建用户,但不创建家目录

[root@VM_168_102_centos ~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_2  test_3  wanghan
[root@VM_168_102_centos
~]# useradd -M test_7
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_7:x:
895:895::/home/test_7:/bin/bash
[root@VM_168_102_centos
~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_2  test_3  wanghan



  useradd -c:创建用户指定一段注释性描述,在/etv/passwd中查看

[root@VM_168_102_centos ~]# useradd -c wanghan test_9                                                
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_9:x:
897:897:wanghan:/home/test_9:/bin/bash

  useradd –s:创建用户时指定默认shell,应该指定/etc/shells文件出现的shell

[root@VM_168_102_centos ~]# useradd -s tcsh test_10
useradd: invalid shell
'tcsh'
[root@VM_168_102_centos
~]# useradd -s /bin/tcsh test_10
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
test_10:x:
898:898::/home/test_10:/bin/tcsh

  #groupadd命令:创建一个新组

[root@VM_168_102_centos ~]# groupadd hp
[root@VM_168_102_centos
~]# tail -n 1 /etc/group
hp:x:
2001:

  groupadd –g GID:创建新组并指定GID

[root@VM_168_102_centos ~]# groupadd -g 2000 wudi
[root@VM_168_102_centos
~]# tail -n 1 /etc/group
wudi:x:
2000:
[root@VM_168_102_centos
~]#

  #userdel命令:删除用户,默认保留家目录

[root@VM_168_102_centos ~]# userdel test_9
[root@VM_168_102_centos
~]# tail /etc/passwd
test_1:x:
888:888::/home/test_1:/bin/bash
test_2:x:
889:500::/home/test_2:/bin/bash
test_3:x:
890:890::/home/test_3:/bin/bash
test_4:x:
891:891::/home/abc:/bin/bash
openstack:x:
892:892::/home/openstack:/bin/bash
test_6:x:
893:893::/home/qidian:/bin/bash
wanghan11:x:
894:894::/tmp/wanghan:/bin/bash
test_7:x:
895:895::/home/test_7:/bin/bash
test_8:x:
896:896::/home/test_8:/bin/bash
test_10:x:
898:898::/home/test_10:/bin/tcsh
[root@VM_168_102_centos
~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_10  test_2  test_3  test_8  test_9  wanghan


  userdel -r:删除用户同时删除其家目录

[root@VM_168_102_centos ~]# userdel -r test_8
[root@VM_168_102_centos
~]# tail /etc/passwd
test:x:
502:502::/home/test:/bin/bash
test_1:x:
888:888::/home/test_1:/bin/bash
test_2:x:
889:500::/home/test_2:/bin/bash
test_3:x:
890:890::/home/test_3:/bin/bash
test_4:x:
891:891::/home/abc:/bin/bash
openstack:x:
892:892::/home/openstack:/bin/bash
test_6:x:
893:893::/home/qidian:/bin/bash
wanghan11:x:
894:894::/tmp/wanghan:/bin/bash
test_7:x:
895:895::/home/test_7:/bin/bash
test_10:x:
898:898::/home/test_10:/bin/tcsh
[root@VM_168_102_centos
~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_10  test_2  test_3  test_9  wanghan


  #groupdel命令:删除群组,若组中仍包括某些用户,则必须先删除这些用户后,方能删除群组。

[root@VM_168_102_centos ~]# groupdel user1
groupdel: cannot remove the primary group of user
'user1'
[root@VM_168_102_centos
~]# groupdel wudi
[root@VM_168_102_centos
~]# cat /etc/group | grep "wudi"
[root@VM_168_102_centos
~]#

  #passwd命令:设定用户密码
  管理员修改自身密码:

[root@VM_168_102_centos ~]# passwd
Changing password
for user root.
New password:
Retype
new password:
passwd: all authentication tokens updated successfully.


  管理员修改其他用户密码:

[root@VM_168_102_centos ~]# passwd wanghan
Changing password
for user wanghan.
New password:
Retype
new password:
passwd: all authentication tokens updated successfully.


  用户修改自身密码:

[wanghan@VM_168_102_centos rott]$ passwd
Changing password
for user wanghan.
Changing password
for wanghan.
(current) UNIX password:   
//输入当前密码
New password:           
Retype
new password:
passwd: all authentication tokens updated successfully.


  passwd –l:锁定用户密码,用户不能修改密码

[root@VM_168_102_centos ~]# passwd -l wanghan
Locking password
for user wanghan.
passwd: Success
[root@VM_168_102_centos
~]# su wanghan
[wanghan@VM_168_102_centos rott]$ passwd
Changing password
for user wanghan.
Changing password
for wanghan.
(current) UNIX password:
passwd: Authentication token manipulation error


  passwd –u:解除用户密码锁定

[root@VM_168_102_centos ~]# passwd -u wanghan
Unlocking password
for user wanghan.
passwd: Success
[root@VM_168_102_centos
~]# su wanghan
[wanghan@VM_168_102_centos rott]$ passwd
Changing password
for user wanghan.
Changing password
for wanghan.
(current) UNIX password:
New password:


  passwd –S:显示用户密码简要信息

[root@VM_168_102_centos ~]# passwd -S wanghan
wanghan PS
2014-08-12 0 99999 7 -1 (Password set, SHA512 crypt.)

  #usermod命令:用来修改用户帐号的各项设定
  usermod –u UID:修改用户UID

[root@VM_168_102_centos ~]# id openstack
uid
=892(openstack) gid=892(openstack) groups=892(openstack)
[root@VM_168_102_centos
~]# usermod -u 588 openstack
[root@VM_168_102_centos
~]# id openstack
uid
=588(openstack) gid=892(openstack) groups=892(openstack)

  usermod –g GID:修改用户GID

[root@VM_168_102_centos ~]# id wanghan
uid
=500(wanghan) gid=2000(wudi) groups=2000(wudi)
[root@VM_168_102_centos
~]# usermod -g 2001 wanghan
[root@VM_168_102_centos
~]# id wanghan
uid
=500(wanghan) gid=2001(hp) groups=2001(hp)

  usermod –G GID:修改用户附加组,默认覆盖原有附加组,同时使用-a选项则是再额外添加

[root@VM_168_102_centos ~]# id user1
uid
=899(user1) gid=899(user1) groups=899(user1),2000(wudi)
[root@VM_168_102_centos
~]# usermod -G 2001 user1
[root@VM_168_102_centos
~]# id user1
uid
=899(user1) gid=899(user1) groups=899(user1),2001(hp)
[root@VM_168_102_centos
~]# usermod -a -G 2000 user1
[root@VM_168_102_centos
~]# id user1
uid
=899(user1) gid=899(user1) groups=899(user1),2000(wudi),2001(hp)

  usermod –c:修改用户帐号备注信息

[root@VM_168_102_centos ~]# useradd -c hello user2
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
user2:x:
900:900:hello:/home/user2:/bin/bash
[root@VM_168_102_centos
~]# usermod -c test user2
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
user2:x:
900:900:test:/home/user2:/bin/bash

  usermod -d:修改用户家目录,默认不会迁移家目录,同时使用-m则可迁移

[root@VM_168_102_centos ~]# usermod -d /tmp/wanghan user1  
[root@VM_168_102_centos
~]# tail /etc/passwd
test_2:x:
889:500::/home/test_2:/bin/bash
test_3:x:
890:890::/home/test_3:/bin/bash
test_4:x:
891:891::/home/abc:/bin/bash
openstack:x:
588:892::/home/openstack:/bin/bash
test_6:x:
893:893::/home/qidian:/bin/bash
wanghan11:x:
894:894::/tmp/wanghan:/bin/bash
test_7:x:
895:895::/home/test_7:/bin/bash
test_10:x:
898:898::/home/test_10:/bin/tcsh
user1:x:
899:899::/tmp/wanghan:/bin/bash
user2:x:
900:900:test:/home/user2:/bin/bash
[root@VM_168_102_centos
~]# usermod -d /tmp/user2/ -m user2
[root@VM_168_102_centos
~]# tail /etc/passwd
test_2:x:
889:500::/home/test_2:/bin/bash
test_3:x:
890:890::/home/test_3:/bin/bash
test_4:x:
891:891::/home/abc:/bin/bash
openstack:x:
588:892::/home/openstack:/bin/bash
test_6:x:
893:893::/home/qidian:/bin/bash
wanghan11:x:
894:894::/tmp/wanghan:/bin/bash
test_7:x:
895:895::/home/test_7:/bin/bash
test_10:x:
898:898::/home/test_10:/bin/tcsh
user1:x:
899:899::/tmp/wanghan:/bin/bash
user2:x:
900:900:test:/tmp/user2/:/bin/bash

  usermod –s:修改用户shell

[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:
900:900:test:/tmp/user2/:/bin/bash
[root@VM_168_102_centos
~]# usermod -s /bin/tcsh user2
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
user2:x:
900:900:test:/tmp/user2/:/bin/tcsh

  usermod -l:修改用户帐号名称

[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:
900:900:test:/tmp/user2/:/bin/tcsh
[root@VM_168_102_centos
~]# usermod -l user3 user2
[root@VM_168_102_centos
~]# tail -n 1 /etc/passwd
user3:x:
900:900:test:/tmp/user2/:/bin/tcsh

  usermod –L:锁定用户帐号密码,使其无效

[root@VM_168_102_centos ~]# usermod -L wanghan
[root@VM_168_102_centos
~]# su wanghan
[wanghan@VM_168_102_centos
/root]$ passwd
Changing password
for user wanghan.
Changing password
for wanghan.
(current) UNIX password:
passwd: Authentication token manipulation error


  usermod –U:解除锁定用户帐号密码

[root@VM_168_102_centos ~]# usermod -U wanghan
[root@VM_168_102_centos
~]# su wanghan
[wanghan@VM_168_102_centos
/root]$ passwd
Changing password
for user wanghan.
Changing password
for wanghan.
(current) UNIX password:
New password:


  #groupmod命令:修改群属性定义
  groupmod –g GID:修改用户GID

[root@VM_168_102_centos ~]# cat /etc/group | grep "hp"
hp:x:
2004:user1
[root@VM_168_102_centos
~]# groupmod -g 2008 hp
[root@VM_168_102_centos
~]# cat /etc/group | grep "hp"
hp:x:
2008:user1

  groupmod -n:修改群组名称

[root@VM_168_102_centos ~]# cat /etc/group | grep "2008"
hx:x:
2008:user1
[root@VM_168_102_centos
~]# groupmod -n hp hx
[root@VM_168_102_centos
~]# cat /etc/group | grep "2008"
hp:x:
2008:user1

  #chage命令:修改帐号和密码的有效期限
  chage –l:查看帐号密码的有效期限

Last password change                    : Aug 13, 2014  //最近一次修改密码
Password expires                    : never   
Password inactive                    : never  
Account expires                        : never
Minimum number of days between password change        :
0    //密码可更改的最小天数
Maximum number of days between password change        : 99999  // 密码保持有效的最大天数
Number of days of warning before password expires    : 7  //用户密码到期前,提前收到警告信息的天数

  chage –m:设定密码可更改的最小天数

[root@VM_168_102_centos ~]# chage -m 5 wanghan
[root@VM_168_102_centos
~]# chage -l wanghan
Last password change                    : Aug
13, 2014
Password expires                    : Aug
23, 2014
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        :
5

  chage –M:设定密码有效的最大天数

[root@VM_168_102_centos ~]# chage -M 10 wanghan
[root@VM_168_102_centos
~]# chage -l wanghan
Last password change                    : Aug
13, 2014
Password expires                    : Aug
23, 2014
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        :
5
Maximum number of days between password change        :
10
Number of days of warning before password expires    :
7

  chage -E:设定帐号的到期日期,0是立即过期,-1永不过期

[root@VM_168_102_centos ~]# chage -E 0 wanghan
[root@VM_168_102_centos
~]# chage -l wanghan
Last password change                    : Aug
13, 2014
Password expires                    : Aug
23, 2014
Password inactive                    : never
Account expires                        : Jan
01, 1970  //帐号过期日期


[root@VM_168_102_centos ~]# chage -E -1 wanghan
[root@VM_168_102_centos
~]# chage -l wanghan
Last password change                    : Aug
13, 2014
Password expires                    : Aug
23, 2014
Password inactive                    : never
Account expires                        : never  //帐号永不过期


  chage –W:设定用户密码到期前,提前收到警告信息的天数

[root@VM_168_102_centos ~]# chage -W 5 wanghan
[root@VM_168_102_centos
~]# chage -l wanghan
Last password change                    : Aug
13, 2014
Password expires                    : Aug
23, 2014
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        :
5
Maximum number of days between password change        :
10
Number of days of warning before password expires    :
5

  #id命令:显示用户ID及所属群组ID

[root@VM_168_102_centos ~]# id
uid
=0(root) gid=500(wanghan) groups=500(wanghan)
[root@VM_168_102_centos
~]# id wanghan
uid
=500(wanghan) gid=2008(hp) groups=2008(hp)

  id -u:显示用户UID,跟-n一起使用则显示用户名

[root@VM_168_102_centos ~]# id
uid
=0(root) gid=0(root) groups=0(root)
[root@VM_168_102_centos
~]# id -u
0
[root@VM_168_102_centos
~]# id -u -n
root


  id -g:显示基本组ID,跟-n一起使用则显示其基本组名

[root@VM_168_102_centos ~]# id wanghan
uid
=500(wanghan) gid=2008(hp) groups=2008(hp)
[root@VM_168_102_centos
~]# id -g wanghan
2008
[root@VM_168_102_centos
~]# id -g -n wanghan
hp


  id –G:显示所有组ID,跟-n一起使用则显示所有组名

[root@VM_168_102_centos ~]# id user1
uid
=899(user1) gid=899(user1) groups=899(user1),2008(hp)
[root@VM_168_102_centos
~]# id -G user1
899 2008
[root@VM_168_102_centos
~]# id -G -n  user1
user1 hp

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-480481-1-1.html 上篇帖子: linux学习命令总结⑥ 下篇帖子: linux network code
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表