root@liuyan-virtual-machine:~#
root@liuyan-virtual-machine:~# useradd tom1
root@liuyan-virtual-machine:~# passwd tom1
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@liuyan-virtual-machine:~# cat /etc/passwd
tom1:x:1001:1001::/home/tom1:/bin/sh
root@liuyan-virtual-machine:~#
root@liuyan-virtual-machine:~# ll /home/tom1
ls: cannot access '/home/tom1': No such file or directory
root@liuyan-virtual-machine:~#
登录新创建的用户我们可以看到,刚刚创建的用户并没有home目录
liuyan@liuyan-virtual-machine:~$ su - tom1
Password:
No directory, logging in with HOME=/
$
$ pwd
/
$
root@liuyan-virtual-machine:~# useradd -m tom2
root@liuyan-virtual-machine:~# passwd tom2
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@liuyan-virtual-machine:~# cat /etc/passwd|grep tom2
tom2:x:1002:1002::/home/tom2:/bin/sh
root@liuyan-virtual-machine:~# ll /home/tom2
total 24
drwxr-xr-x 3 tom2 tom2 4096 2月 27 12:32 ./
drwxr-xr-x 4 root root 4096 2月 27 12:32 ../
-rw-r--r-- 1 tom2 tom2 220 4月 5 2018 .bash_logout
-rw-r--r-- 1 tom2 tom2 3771 4月 5 2018 .bashrc
drwx------ 3 tom2 tom2 4096 2月 27 12:32 .gnupg/
-rw-r--r-- 1 tom2 tom2 807 4月 5 2018 .profile
root@liuyan-virtual-machine:~#
由于sh解释器需要用与sh相关的命令操作计算机,并不是我们常用的shell命令,无法正常利用shgll命令行来控制计算机
liuyan@liuyan-virtual-machine:~$ su - tom2
Password:
$ pwd
/home/tom2
$ echo $PS1
$
$
root@liuyan-virtual-machine:~# useradd -s /usr/bin/python3 tom3
root@liuyan-virtual-machine:~# passwd tom3
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@liuyan-virtual-machine:~# cat /etc/passwd|grep tom3
tom3:x:1003:1003::/home/tom3:/usr/bin/python3
root@liuyan-virtual-machine:~# ll /home/tom3
ls: cannot access '/home/tom3': No such file or directory
root@liuyan-virtual-machine:~#
当我们将新用户的解释器设置为python的时候,我们即可通过常规python命令操作计算机
liuyan@liuyan-virtual-machine:~$ su - tom3
Password:
No directory, logging in with HOME=/
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> pwd
Traceback (most recent call last):
File "" , line 1, in <module>
NameError: name 'pwd' is not defined
>>>
root@liuyan-virtual-machine:~# useradd -m -s /bin/bash tom4
root@liuyan-virtual-machine:~# passwd tom4
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@liuyan-virtual-machine:~# ll /home/tom4
total 20
drwxr-xr-x 2 tom4 tom4 4096 2月 27 13:01 ./
drwxr-xr-x 5 root root 4096 2月 27 13:01 ../
-rw-r--r-- 1 tom4 tom4 220 4月 5 2018 .bash_logout
-rw-r--r-- 1 tom4 tom4 3771 4月 5 2018 .bashrc
-rw-r--r-- 1 tom4 tom4 807 4月 5 2018 .profile
root@liuyan-virtual-machine:~# cat /etc/passwd|grep tom4
tom4:x:1004:1004::/home/tom4:/bin/bash
root@liuyan-virtual-machine:~#
以常规命令创建的用户可以同普通用户一样进行相关操作
liuyan@liuyan-virtual-machine:~$ su - tom4
Password:
tom4@liuyan-virtual-machine:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
tom4@liuyan-virtual-machine:~$ pwd
/home/tom4
tom4@liuyan-virtual-machine:~$
root@liuyan-virtual-machine:/home# cd /etc/default/
root@liuyan-virtual-machine:/etc/default# ls
acpid console-setup kerneloops rsyslog
acpi-support crda keyboard saned
alsa cron locale speech-dispatcher
amd64-microcode dbus motd-news ssh
anacron grub networkd-dispatcher ufw
apport im-config networking useradd
avahi-daemon intel-microcode nss
bsdmainutils irqbalance rsync
将SHELL后的更改成自己想要的解释器,之后直接通过该解释器的相关命令操作系统
root@liuyan-virtual-machine:/etc/default# vim useradd
root@liuyan-virtual-machine:/etc/default# useradd -m aaa
root@liuyan-virtual-machine:/etc/default# passwd aaa
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
切换用户后我们可以看到新创建的用户
root@liuyan-virtual-machine:/etc/default# su - aaa
aaa@liuyan-virtual-machine:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
aaa@liuyan-virtual-machine:~$ ll
total 20
drwxr-xr-x 2 aaa aaa 4096 2月 27 17:42 ./
drwxr-xr-x 4 root root 4096 2月 27 17:42 ../
-rw-r--r-- 1 aaa aaa 220 4月 5 2018 .bash_logout
-rw-r--r-- 1 aaa aaa 3771 4月 5 2018 .bashrc
-rw-r--r-- 1 aaa aaa 807 4月 5 2018 .profile
aaa@liuyan-virtual-machine:~$ pwd
/home/aaa
aaa@liuyan-virtual-machine:~$
以上是Linux系统(Ubuntu)在创建用户时的相关问题及解决办法,同时可以通过“useradd --help”命令查看useradd命令的其他操作
注: /etc/default/useradd 目录下的useradd配置文件可用来配置添加用户的默认配置如SHELLL、HOME等