python在linux下创建目录,Linux下(Ubuntu)如何在创建用户时指定shell解释器及home目录...

案例1:只创建用户不创建其他

相关命令:“useradd 用户名”

输入命令“useradd tom1”,创建用户,并利用passwd命令设置密码

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:~#1

2

3

4

5

6

7

8

9

10

11

12

登录新创建的用户我们可以看到,刚刚创建的用户并没有home目录

liuyan@liuyan-virtual-machine:~$ su - tom1

Password:

No directory, logging in with HOME=/

$

$ pwd

/

$1

2

3

4

5

6

7

案例二:创建用户的的同时创建home目录

相关命令:“useradd -m 用户名”

利用相同的办法创建用户,并加入“-m”命令,创建新用户的home目录

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:~#1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

由于sh解释器需要用与sh相关的命令操作计算机,并不是我们常用的shell命令,无法正常利用shgll命令行来控制计算机

liuyan@liuyan-virtual-machine:~$ su - tom2

Password:

$ pwd

/home/tom2

$ echo $PS1

$

$1

2

3

4

5

6

7

案例三:创建用户同时创建shell解释器

相关命令:“useradd -s /usr/bin/python3 用户名(/bin/bash也可以)

利用“-s”命令创建的用户只有设置的解释器,没有属于新用户的home目录

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:~#1

2

3

4

5

6

7

8

9

10

当我们将新用户的解释器设置为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

NameError: name 'pwd' is not defined

>>>1

2

3

4

5

6

7

8

9

10

11

12

13

案例四:创建用户时创建home目录和解释器

相关命令:”useradd -m -s /bin/bash 用户名“

还可以通过全套命令创建完整的用户

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:~#1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

以常规命令创建的用户可以同普通用户一样进行相关操作

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:~$1

2

3

4

5

6

7

案例五:改变/etc/dsfault下的配置文件

相关命令:“useradd -m 用户名”

除了通过各种命令创建用户外,还可以更改/etc/default目录下的useradd文件的环境变量

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 rsync1

2

3

4

5

6

7

8

9

10

将SHELL后的更改成自己想要的解释器,之后直接通过该解释器的相关命令操作系统

3f98c9a78d53ad136ad49fa2fe016ca6.png

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 successfully1

2

3

4

5

6

切换用户后我们可以看到新创建的用户

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:~$1

2

3

4

5

6

7

8

9

10

11

12

13

14

以上是Linux系统(Ubuntu)在创建用户时的相关问题及解决办法,同时可以通过“useradd --help”命令查看useradd命令的其他操作

注: /etc/default/useradd 目录下的useradd配置文件可用来配置添加用户的默认配置如SHELLL、HOME等

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值