新建用户

  1. useradd wang
  2. passwd wang
  3. usermod -s /bin/bash wang
  4. usermod -d /home/wang wang

授权

  1. chmod u+w /etc/sudoers
  2. vim /etc/sudoers
  3. root ALL=(ALL:ALL) ALL
  4. wang ALL=(ALL:ALL) ALL
  5. %sudo ALL=(ALL:ALL) ALL
  6. tenguwang ALL=(ALL:ALL) NOPASSWD:ALL
  7. ---
  8. chmod u-w /etc/sudoers

创建登陆用户

  1. sudo useradd -r -m -s /bin/bash tenguwang
  2. chmod 644 /home/safe/.ssh/id_rsa.pub
  3. chmod 600 /home/safe/.ssh/id_rsa

shell

  1. #!/usr/bin/bash
  2. #
  3. # TenguWang
  4. read -p "请输入要创建的账户名称:" username
  5. useradd -r -m -s /bin/bash ${username}
  6. echo "用户${username}创建成功"
  7. ls -al /home/${username}
  8. read -p "请输入${username}密码:" passwd
  9. echo "${username}:${passwd}" | sudo chpasswd
  10. chmod +w /etc/sudoers
  11. echo "${username} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  12. chmod -w /etc/sudoers
  13. echo "${username}免密已设置完成"
  14. echo "开始写入密钥文件"
  15. mkdir /home/${username}/.ssh
  16. echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC688q9OFg/AChEQiJJWQ80y6HCn2LDhNDlmNn9eR7r6ZsjICxF9K8q2S9QfUAH2O78Z2plFpoBqzk3N+JYLv4R9RkxlXJBZ2EHrWfpyjLIQsPb0Bh8CRjxw0BH2ZWeiBxuteIcAEedR61A4pPzsT5JBO+DUHrCXeYSIujuIAlIROF7W0HO3gSrhzZh8QsbWbEwTTn/fgnOtmkDPx3+gwrZmn4iXUdLBJs9rd/HVQTlhMfIXc6jn3xW0Ww3hTWdf8/+hL1tJum8B2VyywiiXmENbVHVH4U5uGuqOLXUUbigUbWaVIzyV9sbJg4SQtfUt+gVDhJ6eQVuA4r40ahhKU0PIy4fSEPn+oOWrHBeB1rFR1zrrwoVUoMvIA0sbjWfIKvJUrSwLS9KBSHplxGbY/xtpXNECoIYmHz9LQOrURjRJjrddFVdND2eKUpXdw1JUvRypjCKX50YcGl4kAmVBSVXG7LYZmA+EsC1JD24uavXlS454vI8uZPW6+GE3T1DaO0= TenGuWang" > /home/${username}/.ssh/authorized_keys
  17. chown -R tenguwang.tenguwang /home/${username}/.ssh
  18. echo "您用户${username}创建成功,密码:${passwd}"

useradd: cannot open /etc/passwd

解决办法

  1. lsattr /etc/passwd
  2. ----i----------- /etc/passwd
  3. # 去除i属性
  4. chattr -i /etc/passwd

Cento7创建大于1000的用户密钥无法登录

  1. usermod -u 555 tenguwang
  2. groupmod -g 555 tenguwang
  1. [root@tenguwang ~]# usermod -u 555 tenguwang
  2. usermod: user tenguwang is currently used by process 21843
  3. kill -9 `lsof -u tenguwang` # 直接杀掉

在Linux系统中,1000以下的UID是系统保留的UID。随意修改系统上某些帐号的 UID 很可能会导致某些程序无法进行,甚至导致系统无法顺利运行。我们可以通过/usr/share/doc/setup-2.8.71/uidgid来查看具体对应关系,当然不同的版本路径可能不一样,可以用/usr/share/doc/setup*/uidgid来通配

  1. [root@tenguwang ~]# cat /usr/share/doc/setup-2.8.71/uidgid
  2. NAME UID GID HOME SHELL PACKAGES
  3. root 0 0 /root /bin/bash setup
  4. bin 1 1 /bin /sbin/nologin setup
  5. daemon 2 2 /sbin /sbin/nologin setup
  6. sys - 3 - - setup
  7. adm 3 4 /var/adm /bin/bash setup
  8. tty - 5 - - setup
  9. disk - 6 - - setup
  10. lp 4 7 /var/spool/lpd /sbin/nologin setup
  11. mem - 8 - - setup
  12. kmem - 9 - - setup
  13. wheel - 10 - - setup
  14. cdrom - 11 - - setup
  15. sync 5 (0) /sbin /bin/sync setup
  16. shutdown 6 (0) /sbin /sbin/shutdown setup
  17. halt 7 (0) /sbin /sbin/halt setup
  18. mail 8 12 /var/spool/mail /sbin/nologin setup
  19. news 9 13 /var/spool/news /sbin/nologin setup
  20. uucp 10 14 /var/spool/uucp /sbin/nologin uucp
  21. operator 11 (0) /root /sbin/nologin setup
  22. games 12 (100) /usr/games /sbin/nologin setup
  23. gopher 13 30 /var/gopher /sbin/nologin -(not created by default)
  24. ftp 14 50 /var/ftp /sbin/nologin setup
  25. man - 15 - - setup
  26. oprofile 16 16 /var/lib/oprofile /sbin/nologin oprofile
  27. pkiuser 17 17 /usr/share/pki /sbin/nologin pki-ca,rhpki-ca
  28. dialout - 18 - - setup
  29. floppy - 19 - - setup
  30. games - 20 - - setup
  31. slocate - 21 - - slocate
  32. utmp - 22 - - initscripts,libutempter
  33. squid 23 23 /var/spool/squid /dev/null squid
  34. pvm 24 24 /usr/share/pvm3 /bin/bash pvm
  35. named 25 25 /var/named /bin/false bind
  36. postgres 26 26 /var/lib/pgsql /bin/bash postgresql-server
  37. mysql 27 27 /var/lib/mysql /bin/bash mysql
  38. nscd 28 28 / /bin/false nscd
  39. rpcuser 29 29 /var/lib/nfs /bin/false nfs-utils
  40. console - 31 - - dev
  41. rpc 32 32 / /bin/false portmap
  42. amandabackup 33 (6) /var/lib/amanda /bin/false amanda
  43. tape - 33 - - setup
  44. netdump 34 34 /var/crash /bin/bash netdump-client, netdump-server
  45. utempter - 35 - - libutempter
  46. vdsm 36 - / /bin/bash kvm, vdsm
  47. kvm - 36 - - kvm, vdsm, libvirt
  48. rpm 37 37 /var/lib/rpm /bin/bash rpm
  49. ntp 38 38 /etc/ntp /sbin/nologin ntp
  50. video - 39 - - setup
  51. dip - 40 - - ppp
  52. mailman 41 41 /var/mailman /bin/false mailman
  53. gdm 42 42 /var/gdm /bin/bash gdm
  54. xfs 43 43 /etc/X11/fs /bin/false XFree86-xfs
  55. pppusers - 44 - - linuxconf
  56. popusers - 45 - - linuxconf
  57. slipusers - 46 - - linuxconf
  58. mailnull 47 47 /var/spool/mqueue /dev/null sendmail
  59. apache 48 48 /var/www /bin/false apache
  60. wnn 49 49 /home/wnn /bin/bash FreeWnn
  61. smmsp 51 51 /var/spool/mqueue /dev/null sendmail
  62. puppet 52 52 /var/lib/puppet /sbin/nologin puppet
  63. tomcat 53 53 /var/lib/tomcat /sbin/nologin tomcat
  64. lock - 54 - - lockdev
  65. ldap 55 55 /var/lib/ldap /bin/false openldap-servers
  66. frontpage 56 56 /var/www /bin/false mod_frontpage
  67. nut 57 57 /var/lib/ups /bin/false nut
  68. beagleindex 58 58 /var/cache/beagle /bin/false beagle
  69. tss 59 59 - /sbin/nologin trousers
  70. piranha 60 60 /etc/sysconfig/ha /dev/null piranha
  71. prelude-manager 61 61 - /sbin/nologin prelude-manager
  72. snortd 62 62 - /sbin/nologin snortd
  73. audio - 63 - - setup
  74. condor 64 64 /var/lib/condor /sbin/nologin condord
  75. nslcd 65 (55) / /sbin/nologin nslcd
  76. wine - 66 - - wine
  77. pegasus 66 65 /var/lib/Pegasus /sbin/nologin tog-pegasus
  78. webalizer 67 67 /var/www/html/usage /sbin/nologin webalizer
  79. haldaemon 68 68 / /sbin/nologin hal
  80. vcsa 69 69 - /sbin/nologin dev,MAKEDEV
  81. avahi 70 70 /var/run/avahi-daemon /sbin/nologin avahi
  82. realtime - 71 - - -
  83. tcpdump 72 72 / /sbin/nologin tcpdump
  84. privoxy 73 73 /etc/privoxy /bin/bash privoxy
  85. sshd 74 74 /var/empty/sshd /sbin/nologin openssh-server
  86. radvd 75 75 / /bin/false radvd
  87. cyrus 76 (12) /var/imap /bin/bash cyrus-imapd
  88. saslauth - 76 - - cyrus-imapd
  89. arpwatch 77 77 /var/lib/arpwatch /sbin/nologin arpwatch
  90. fax 78 78 /var/spool/fax /sbin/nologin mgetty
  91. nocpulse 79 79 /etc/sysconfig/nocpulse /bin/bash nocpulse
  92. desktop 80 80 - /sbin/nologin desktop-file-utils
  93. dbus 81 81 / /sbin/nologin dbus
  94. jonas 82 82 /var/lib/jonas /sbin/nologin jonas
  95. clamav 83 83 /tmp /sbin/nologin clamav
  96. screen - 84 - - screen
  97. quaggavt - 85 - - quagga
  98. sabayon 86 86 - /sbin/nologin sabayon
  99. polkituser 87 87 / /sbin/nologin PolicyKit
  100. wbpriv - 88 - - samba-common
  101. postfix 89 89 /var/spool/postfix /bin/true postfix
  102. postdrop - 90 - - postfix
  103. majordomo 91 91 /usr/lib/majordomo /bin/bash majordomo
  104. quagga 92 92 / /sbin/nologin quagga
  105. exim 93 93 /var/spool/exim /sbin/nologin exim
  106. distcache 94 94 / /sbin/nologin distcache
  107. radiusd 95 95 / /bin/false freeradius
  108. hsqldb 96 96 /var/lib/hsqldb /sbin/nologin hsqldb
  109. dovecot 97 97 /usr/libexec/dovecot /sbin/nologin dovecot
  110. ident 98 98 / /sbin/nologin ident
  111. nobody 99 99 / /sbin/nologin setup
  112. users - 100 - - setup
  113. qemu 107 107 / /sbin/nologin libvirt
  114. ovirt 108 108 / /sbin/nologin libvirt
  115. rhevm 109 109 /home/rhevm /sbin/nologin vdsm-reg
  116. jetty 110 110 /usr/share/jetty /sbin/nologin jetty
  117. saned 111 111 / /sbin/nologin sane-backends
  118. vhostmd 112 112 /usr/share/vhostmd /sbin/nologin vhostmd
  119. usbmuxd 113 113 / /sbin/nologin usbmuxd
  120. bacula 133 133 /var/spool/bacula /sbin/nologin bacula
  121. cimsrvr 134 134 / /sbin/nologin tog-pegasus-libs
  122. mock - 135 / - mock
  123. ricci 140 140 /var/lib/ricci /sbin/nologin ricci
  124. luci 141 141 /var/lib/luci /sbin/nologin luci
  125. activemq 142 142 /usr/share/activemq /sbin/nologin activemq
  126. stap-server 155 155 /var/lib/stap-server /sbin/nologin systemtap
  127. stapusr - 156 / - systemtap-runtime
  128. stapsys - 157 / - systemtap-runtime
  129. stapdev - 158 / - systemtap-runtime
  130. swift 160 160 /var/lib/swift /sbin/nologin openstack-swift
  131. glance 161 161 /var/lib/glance /sbin/nologin openstack-glance
  132. nova 162 162 /var/lib/nova /sbin/nologin openstack-nova
  133. keystone 163 163 /var/lib/keystone /sbin/nologin openstack-keystone
  134. quantum 164 164 /var/lib/quantum /sbin/nologin openstack-quantum
  135. cinder 165 165 /var/lib/cinder /sbin/nologin openstack-cinder
  136. ceilometer 166 166 /var/lib/ceilometer /sbin/nologin openstack-ceilometer
  137. ceph 167 167 /var/lib/ceph /sbin/nologin ceph-common
  138. avahi-autoipd 170 170 /var/lib/avahi-autoipd /sbin/nologin avahi
  139. pulse 171 171 /var/run/pulse /sbin/nologin pulseaudio
  140. rtkit 172 172 /proc /sbin/nologin rtkit
  141. abrt 173 173 /etc/abrt /sbin/nologin abrt
  142. retrace 174 174 /usr/share/retrace-server /sbin/nologin retrace-server
  143. ovirtagent 175 175 / /sbin/nologin ovirt-guest-agent
  144. ats 176 176 / /sbin/nologin trafficserver
  145. dhcpd 177 177 / /sbin/nologin dhcp
  146. myproxy 178 178 /var/lib/myproxy /sbin/nologin myproxy-server
  147. sanlock 179 179 /var/run/sanlock /sbin/nologin sanlock
  148. aeolus 180 180 /var/aeolus /sbin/nologin aeolus-configure
  149. wallaby 181 181 /var/lib/wallaby /sbin/nologin wallaby
  150. katello 182 182 /usr/share/katello /sbin/nologin katello-common
  151. elasticsearch 183 183 /usr/share/java/elasticsearch /sbin/nologin elasticsearch
  152. mongodb 184 184 /var/lib/mongodb /sbin/nologin mongodb
  153. jboss 185 185 /var/lib/jbossas /sbin/nologin jbossas-core #was jboss-as and wildfly
  154. jbosson-agent 186 - / /sbin/nologin jboss-on-agent
  155. jbosson - 186 - - jboss-on-agent
  156. heat 187 187 /var/lib/heat /sbin/nologin heat
  157. haproxy 188 188 /var/lib/haproxy /sbin/nologin haproxy
  158. hacluster 189 - / /sbin/nologin pacemaker
  159. haclient - 189 - - pacemaker
  160. systemd-journal - 190 - - systemd
  161. systemd-journal-gateway 191 191 / /sbin/nologin systemd
  162. #systemd-journal-gateway dynamic on new systems (may have different uid/gid)
  163. systemd-network 192 192 / /sbin/nologin systemd
  164. systemd-resolve 193 193 / /sbin/nologin systemd
  165. gnats ? ? ? ? gnats, gnats-db
  166. listar ? ? ? ? listar
  167. nfsnobody 65534 65534 /var/lib/nfs /sbin/nologin nfs-utils
  168. # Note: nfsnobody is 4294967294 on 64-bit platforms (-2)