## Managing Accounts with pw
## Nathan Underwood <>
Intro
For many of you who are new to FreeBSD, the only user currently on your
system is likely going to be yourself; but, what happens when you want to
take advantage of the multi-user capabilities of a UNIX-based system?
For many experienced BSD-based users, the adduser command is the only
utility they have ever used. While adduser does the job just fine, it is
a bit cumbersome and requires answering a series of questions for each
user. What if there was a way to quickly add users with one simple
command? There is a way, and it is already available for use in FreeBSD;
one just has to know how to use it. Pw is the tool, and it has a few
advantages over adduser. It is faster (written in C rather than Perl), it
is more easily configurable, and it has many more command line arguments,
to name a few.
From the pw man page:
Pw was written to mimic many of the options used in the SYSV
shadow support suite, but is modified for passwd and group
fields specific to the 4.4BSD operating system, and combines
all of the major elements into a single command.
So, now you have a brief history of pw, but you are probably asking, "How
do I use it...and how easy is it?". Well, this is the great part. It is
easy, and anyone can set it up and be adding users in rapid succession
in no time at all. All that is required is a /etc/pw.conf file, some
skeleton files in /usr/share/skel, and a couple of commands at the prompt.
Getting Started
There are two ways to get started, and I recommend whichever you feel
comfortable with, or a combination of both.
Man pages
If you have never read the man pages for the program that you are trying
to use, you are either very brave or very ignorant. Man pages are the
best way to introduce yourself to a particular program. This doesn't
necessarily mean that you have to read the entire man page, but they are
often an invaluable source of information.
$ man pw
This will give you loads of info about pw and its various command line
arguments, and:
$ man pw.conf
An excellent man page for help on writing your pw.conf file.
After doing this, you may not have to read the rest of this article! But,
if you want some clear examples, continue on.
Configuration
Configuring for pw
I certainly recommend reading the man pages, for they will give you a
better understanding of what the next section explains. To start, we need
a pw.conf file in our /etc directory. (On a side note, you will have to
be the superuser to complete most of the following tasks.) In the
following example, I am using the bash shell, which you may not have
installed on your system. Substitute the path to whatever shell you want
to use and include the shell name (ie. tcsh, csh, etc.)
The pw.conf file:
#
# /etc/pw.conf for generic use
#
# Minimum and Maximum uid
#
# Define the minimum and maximum user id's for your users. These
# numbers can be of your choosing, I wouldn't recommend starting with
# less than a uid of 100 since some programs (that you might not have
# installed yet) require uid's below 100. If you start creating users
# with extremely low (below 100) uid's, you are asking for trouble.
minuid 1000
maxuid 2000
# Passwords
# This will set it so that you have to use the passwd command
# after the user is created to set the password. Other options are
# explained in the man pages. This is the most simple use.
defaultpasswd no
# The default group
# This set the default group for all users, you may override it with
# -g "groupname" at the command line; or, you may comment it
# it out to create a separate group for each user. I have set the
# default group here as users.
defaultgroup users
# Re-using gaps in uid sequences
# If you delete a user and have this set in your file, the next user
# added will receive the deleted person's user id.
reuseuids
# Set root for home directories
# This allows you to set the root directory where all your user
# directories will be created. I have chosen /home as the
# root in this example, thus giving any user the path /home/username.
home /home
# The default shell
# This is important to set if you want to use anything other than
# /bin/sh for your shell. There are 3 items here: the path to
# the directory where the default shell resides, the available shells
# to use (I've only listed bash), and the actual default shell.
# In this example I've chosen the bash shell.
shellpath /usr/local/bin
shells bash
defaultshell bash
# EOF
Don't forget to put it in your /etc directory! While there are more
options you can add to your pw.conf file such as extra groups and
sending out a welcome letter, the above are most crucial to adding a user
to the system.
Checking /usr/share/skel
That's it! Now you're probably asking, "Well, what does it create and how
do I use it?". To add a user to your system after you have written your
pw.conf and placed it in your /etc directory, you will need to make sure
that some skeleton files are already in the default place that pw looks
for them in. In FreeBSD, this location is /usr/share/skel. On a virgin
FreeBSD setup, these files will be:
$ cd /usr/share/skel/
$ pwd
/usr/share/skel
$ ls -l
total 16
-rw-r--r-- 1 bin bin 509 Dec 1 01:34 dot.cshrc
-rw-r--r-- 1 bin bin 561 Dec 1 01:34 dot.login
-rw-r--r-- 1 bin bin 139 Dec 1 01:34 dot.login_conf
-rw------- 1 bin bin 351 Dec 1 01:34 dot.mail_aliases
-rw-r--r-- 1 bin bin 313 Dec 1 01:34 dot.mailrc
-rw-r--r-- 1 bin bin 749 Dec 1 01:34 dot.profile
-rw------- 1 bin bin 257 Dec 1 01:34 dot.rhosts
-rw-r--r-- 1 bin bin 832 Dec 1 01:34 dot.shrc
$
These are the files that will be inserted in any users home directory that
you create. You may add any files to this directory that you want
to be in your users home directories by default (eg. dot.muttrc or
dot.bashrc). *When you run pw, the "dot" at the beginning of the files
in the skel directory will be removed, leaving the familiar .muttrc or
.bashrc*. You are now ready to begin to rapidly add users.
Adding Users
The pw useradd command
If your user's username was going to be "foo" for example, at the command
line you would type:
$ pw useradd foo -c "Mr. Foo" -m
You've just added a user! Now for an explanation:
useradd username |
adds user "username" ("foo" in our example) |
-c "Real Name" |
gecos info (Our user's real name is Mr. Foo) |
-m |
creates the users home dir with /usr/share/skel contents |
Setting the password
The only other thing you will have to do is create a password for the
user, which is simply done:
$ passwd foo
Making sure with pw usershow
Lastly, to make sure that you have created the account with the
information you thought, use the pw usershow command and check it out.
For example, to check on the user we just created (the following will
look slightly different on your system, depending on your personal changes
to the pw.conf file), we will do:
$ pw usershow foo
foo:*:1001:1001::0:0: Mr. Foo :/home/foo:/usr/local/bin/bash
$
It was a success, and with this tool under your belt you can spend more
time having fun on your system and less time adding users.
Deleting Users
The pw userdel command
Yes, there will come a time when you want to delete users, and pw makes it
extremely simple to do so. All we have to do to delete poor Mr. Foo is:
$ pw userdel foo -r
The -r removes all of foo's files and deletes his home directory.
You have now learned to effectively manage accounts with pw; however,
experiment and read the man pages, for there are a myriad of options and
other account management features that you can utilize with pw. I hope
you will take the time to experiment and read the man pages. Happy adding
and deleting!
- Nate
Return to Issue #2
|