Helpful UNIX tasks
I do not pretend to be a UNIX(generally)
guru, nor a guru on any particular flavor of UNIX, however, I do happen
to know a fair amount about these things, and therefore have several very
simple tasks outlined, and am working on this and therefore eventually,
this list could get rather long.
Adding a user, general UNIX
Simple user
-
login as root
-
adduser <username>
-- this is sometimes useradd...you have to watch out.
-
passwd <username>
-- change the passwd for the user you have just added.
More complex user
-
login as root
-
adduser <username> -c <comment>
-d <home directory of user> -g <primary group of this user> -G <a
comma delimited list of groups to which this user belongs> -m -k <directory
to make this new directory look like> -s <users shell>
-- this is sometimes useradd...you have to watch out.
-- this command make take any of the -X arguments optionally.
Each are explained here:
-c -- This is a comment, and typically contains information such
as who this user is, and any other information that the system should hold
about the user.
-d -- This is used to set the home directory of the user.
-g -- Set the primary group of the user, on machines where all
users should be primarily affiliated with a certain group, such as undergrad,
or acedemic, this is the option to use.
-G -- This will set all of the groups that the user is a member
of, and must be a comma separated list of group ids.
-m -- if the users home directory does not exist, then create
it.
-k -- use a skeleton home directory to create this user from.
You can use this to make mirror copies of a skeleton directory for a user.
-s -- set a shell for the user. This must be a valid executable
file.
most of the above options have a default value if they are not specified,
and hence the simple user example works. The default values are typically:
create the users home directory in a manner such as /home/<username>
no comment
primary group == username as group id
other groups == none
use system defaults for home directory skeleton
shell == /bin/sh
-
passwd <username>
-- change the passwd for the user you have just added.
Modifying a user, general
UNIX
One should note that none of these changes will
take affect until the user logs out.
using the command usermod
-
login as root
-
usermod can be used with the same arguments and in much the
same manner as adduser/useradd.
modifying files
Changing a persons group
membership
-
edit /etc/group
-
search for the group to which you wish to add the user
-
add the user onto the end of the list for that group.
The username must be added such that it has a comma after the last username
previous to it, and has NO SPACES.
-
save the file
Changing the primary
group membership of a user
-
edit /etc/passwd
-
search for the username you wish to modify
-
(you must know the numeric id of the group to which to wish
to have this user be primarily of)
-
the line will look like this:
username:x:234:654::/home/username:/bin/sh
^^^^^^^^
^^^
This user
gid <-- modify this.
change the numeric entry to the new group id, do not modify
the line in any other way! So if you wanted to change this guy to
belong to the group 432, then the new line would look like:
username:x:234:432::/home/username:/bin/sh
-
save the file
Changing the comment
for the user
-
edit /etc/passwd
-
search for the username you wish to modify
-
the line will look like this:
username:x:234:432::/home/username:/bin/sh
^^^^^^^^
^
this user
| --- This is what you need to modify. Just insert whatever text
you like
So, if you wanted to change the comment for this guy to
be his name: Generic User, then the new line would look like:
username:x:234:432:Generic User:/home/username:/bin/sh
-
save the file
Setting Up a Linux Machine To
Automatically Start Xwindows, the Login Process
This is more commonly known as XDM, but is basically
setting up an Xserver so that it provides the login screen and people do
not have to start up Xwindows on their own on a Linux System.
These instructions assume that you already have Xwindows
installed and working on your system. If you do not have it working
(i.e. xinit either does nothing, is not found, or you can do nothing once
you enter the command other than simply kill Xwindows...) then DO NOT use
these instructions as you will have a great deal of problems if you do.
-
edit /etc/inittab
-
search for a line that has xdm in it. This will typically
be at the end of the file, and look like this:
x:5:respawn:/usr/X11R6/bin/xdm -nodaemon
if it is not in the file, and your computer currently
does not produce an Xwindows login when you start up, then add this line
to the end of the file.
-
now edit this line, by adding a 23 before the 5 in that same
field, this should look like:
x:235:respawn:/usr/X11R6/bin/xdm -nodaemon
-
save the file.
-
reboot the machine ('shutdown -r' or 'reboot' ), or
run 'init 5'
(section is denoted by a colon delimited entry)
Something to note is that the first section is unimportant,
the second section is the run-levels that you wish to run this at, so,
it may not always be 235, but this is the safest method. Sometimes
4 will be included, but often this run-level is either unimplemented, or
is a special run-level and should be left alone. The third section
('respawn') is a control for the init program. It tells init to respawn
xdm if it is killed, this is so that a person logging out does not kill
off xdm. The fourth section is where init can find the program.
This is absolutely essential. You must perform a 'where xdm' or a
'locate xdm' to ensure that this is the correct location for the program.
Otherwise init will produce many errors as it tries to respawn a non-existant
program continually...
General UNIX Commands
-
cat
-
this command will "list" a file (output
its contents to the screen). It does it in such a way that every
character in the file is output to the screen using its various representation
thanks to ASCII code.
-
rm
-
this command removes a file or directory
from the file system, dependent upon the arguments to the rm command.
The arguments to this function are -r (to recursively destroy a file or
directory, though this argument really only matters in destroying a directory.
In fact, this is the only way to destroy a directory with the rm command,
otherwise, one needs to use the rmdir command.), or -f (force, this will
destroy things even if the -i option is used to confirm the destruction,
and it will suppress error messages, such as permission errors, note, it
will not override permission errors, it simply will not tell you about
them in this case). -i can be used to force you to confirm removal
of files.
-
mv
-
this command moves a file or directory
from one location in the file system to another location in the file system.
A -i can be used to ask you to confirm any move which will overwrite files.
-
cd
-
this command changes your current focus,
or where you are, to another location. The command is an acronym
for Change Directory, as the purpose of the command is to move around in
the file structure of UNIX, known as the directory structure. The
place that you can change directory from is always from the current directory
you are in, which can be shown using the command pwd.
-
pwd
-
this command will show you the current
directory you are in.
-
ls
-
this command will show you the contents
of the current directory you are in, if used using only the command ls.
However, this command takes a myriad of arguments, some of the more useful
of which can tell you sizes, and types of files ('ls -l'), and other very
interesting things. The arguments gone over below may be combined,
such that the arguments are preceeded by a single dash, and then a list
of arguments (for the '-F', '-l', and '-a' arguments, the appropriate argument
would be '-Fla'. Though the order does not matter).
-
The output of the normal ls command
is simply a list of files, if the -a option is used, then all files are
shown that are in the directory, otherwise all hidden files (files preceeded
with a '.') are not shown. Each file is listed in a column of the
output, or if the -1 argument is used, then each file will be listed on
its own line.
-
Using the -F command produces a slightly
different output, in that using this argument, a single character is appended
to the filename, in the case that the file is executable (a '*' is appended),
a directory (a '/' is appended), or a link (not a real file, but merely
a pointer to another file, a '@' is appended).
-
find
-
this command will search every file
in the directory structure, from the place that it is given to start with
onward. The command takes a list of directories, for which it will
search every file in each directory and then will traverse the directories
underneath it until it has nowhere to go. The find command will not
traverse softlinks. Though by searching every file, I feel I must
explain what this means. It will perform the actions specified, by
the remaining arguments to the command, on every file that it searches.
To explain what this means, I will go over a common usage of the command.
Using the -name argument, it will check each file searched against the
name specified as a shell expression, this basically means a regular expression,
except there are some differences (though you must look these up, as each
shell has different issues with this. If the name matches the -name
argument, then it performs the next command on the file. Another
common usage is the -exec argument after this. The -exec executes
a command (in a new shell) with a set of arguments (it will send everything
between the -exec and the \; to the shell). A note here is that,
in the -exec the {} symbols indicate, place the filename that we are searching
here.
A cool find command (in tcsh/csh):
find . -exec test -f {} \; -exec grep -l phrase {} \;
Find all files in this directory, or any subdirectory (recursively) which contain the
word "phrase". Most importantly this command will not spam you with the
usual 'grep: x is a directory' error message for each subdirectory.
A small note is that the 'test -f' is peculiar to tcsh/csh, and if you use a
different type of shell you may have to use something else, such as for ksh
it is 'test -a'.
- ps
-
This command will display processes being run
on this computer. This is very useful for finding out whether a process is
actually done running or not, and similarly for finding processes to use the
kill command on. Though ps does not actually look for
process names, it can be done.
alias findnumber 'ps aux | grep \!* | cut -c 10-15'
is a handy alias that will list all process numbers of processes that match
the specified name when you use findnumber. For instance:
fundnumber netscape
would list all the process numbers of any process that had netscape in its
name. Explaining the findnumber alias, we look at the ps command and it uses
the argument ax, which on linux, and similar systems will show all processes
that belong to the user who runs the command, and whether they are in zombie
state or not (as the system does not normally list processes that are zombified)
The grep command uses the argument \!*, which is csh/tcsh syntax for whatever
is typed after the command (all arguments of the alias in this case). Further
the cut command takes the -c argument, which will cut a specified range of
characters out of whatever it is given (on each line). A much more useful
alias is built later in the kill command. But this can also be quite a useful
building block.
- kill
-
This command will send a signal to a
specific process (running program). The point behind this command is
that sometimes a process will become errant, in that it does not do what you
want it to. This can obviously be a problem. So this command is most often
used without argument to terminate a process, though this is also sometimes
not enough, when the process becomes so embroiled in its own processing that it
no longer responds. In these cases, it is necessary to use the -9 argument
with kill, so that the process is killed no matter what the process wants to
do. The thing about this command is that it is necessary to use the process's
number in order to do something about it, and therefore ps
is necessary.
An example of these two commands is below.
alias killproc 'kill -9 `ps aux | grep \!* | cut -c 10-15`'
As is often useful, the above will kill all programs with the name matching
the argument of the killproc alias. Taking the command a piece at a time,
we use the -9 argument of kill, telling it to take the process down no matter
what. The ps command next will list all processes you own, but it is fed into
grep, making it match only processes that have the argument to the killproc
alias in their name, then the cut command is used to cut out only the process
numbers of list. As with the ps command, these assume that you are on a linux
or similar system, as each system will require slightly different arguments.
One other interesting note is that the kill command does not take standard
input, only arguments, so that is why we included the backticks (`) in the
alias, and could not simply use a pipe (|) as we do in other places.