在Ubuntu服务器上添加用户

时间:2020-01-09 10:44:38  来源:igfitidea点击:

Ubuntu Server与任何Linux版本一样,具有完整的多用户功能,并且任何服务器上的常见任务都是添加用户。

用户添加

通过useradd命令,我们可以从命令行轻松添加新用户:

useradd <username>

该命令将添加用户,但是没有任何其他选项,用户将没有密码或者主目录。

注意:如果收到一条消息,提示找不到该命令,请尝试使用完整路径,如下所示:

/usr/sbin/useradd <username>

我们可以使用-d选项设置用户的主目录。 -m选项将强制useradd创建主目录。我们将尝试使用这些选项创建一个用户帐户,然后使用passwd命令设置该帐户的密码。我们也可以在useradd命令上使用-p设置密码,但是我更喜欢使用passwd设置密码。

sudo useradd -d /home/testuser -m testuser
sudo passwd testuser

这将创建一个名为testuser的用户,并在/ home / testuser中为其提供自己的主目录。新主目录中的文件是从/ etc / skel文件夹复制的,该文件夹包含默认主目录文件。如果要为用户设置默认值,可以通过在该目录中修改或者添加文件来实现。如果我们查看用户的新主目录:

data@ubuntuServ:/etc/skel$ ls -la /home/testuser

total 20

drwxr-xr-x 2 testuser testuser 4096 2006-12-15 11:34 .

drwxr-xr-x 5 root root 4096 2006-12-15 11:37 ..

-rw-rr 1 testuser testuser 220 2006-12-15 11:34 .bash_logout

-rw-rr 1 testuser testuser 414 2006-12-15 11:34 .bash_profile

-rw-rr 1 testuser testuser 2227 2006-12-15 11:34 .bashrc

我们会注意到该目录中有bash脚本。如果要为所有新用户设置默认路径选项,可以通过修改/ etc / skel中的文件来完成,然后通过useradd命令将其用于创建这些文件。

添加用户

adduser命令甚至比useradd命令更容易,因为它会提示我们输入每条信息。我发现有两个几乎完全相同名称的命令可以执行相同的操作,但我觉得这有点可笑,但这是Linux给。语法如下:

adduser <username>

例:

data@ubuntuServ:/etc/skel$ sudo adduser thedata

Password:

Adding user `thedata'&#8230;

Adding new group `thedata' (1004).

Adding new user `thedata' (1004) with group `thedata'.

Creating home directory `/home/thedata'.

Copying files from `/etc/skel'

Enter new UNIX password:

Retype new UNIX password:

No password supplied

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

Changing the user information for thedata

Enter the new value, or press ENTER for the default

Full Name []: The data

Room Number []: 0

Work Phone []: 555-1212

Home Phone []: 555-1212

Other []:

Is the information correct? [y/N] y