-->
Commonly Used Linux/Unix
Commands:
"Breaking" out from a locked GUI session:
Cntrl-Alt-F2,Cntrl-Alt-F3,Cntrl-Alt-F4, or Cntrl-Alt-F5 to get a "login:"
prompt.
Next, login as root, find the process ID of the Xserver and kill
it.
Creating users:
useradd {user name} w/ optional switches:
-m Creates a new home directory for a new user.
-M Tells useradd NOT
to create a home directory for the new user.
-s Defines path to users
default shell (such as /bin/bash).
-h Defines path to user's home
directory.
-c Attaches comment text to user.
-e Defines account
expiration date.
-u Sets user ID.
-D Changes the default values for
useradd command.
* Don't forget to set a password for the new user! Do that with
passwd {user name}
Deleting users:
Finding files by name:
Kill X in an emergency:
If X has stalled (e.g. during re-configuration after an update etc.), or
if a graphical application has crashed and left X unstable, use
Ctrl+Alt+Backspace to kill X and return you to either the shell prompt
or the login manager, depending on how X was started.
SysRq:
This key may be labelled Print Screen or Prt Scrn on some keyboards. If
your system has crashed and become unstable, just cutting the power will at
the very least cause an fsck when you eventually restart and it may well
damage the system itself. A safer option is to attempt to prevent disaster in
the hope that the kernel is still responding, even if everything else has
died. (If your computer is on a network, try ssh or telnet first and see if
you can get root access before giving up on the system.) Otherwise, use these
in this order!
Alt+SysRq+S synchronises the discs.
Alt+SysRq+U
unmounts ALL filesystems.
Alt+SysRq+B reboots.
Memory monitoring:
Find out which processes are taking the most memory: $ ps -aux | sort
+5n | less
Find out who is listening to your box:
$ netstat -na | grep -i listen gives you a list of the daemons
listening on TCP or UDP ports
$ netstat -na | grep -i
established will let you see who is connected.
Logged on user listings:
Finding library dependencies:
If an installed application is causing problems, or if you want to copy it
to another machine, find out which libraries are required by the binary. e.g.
for /usr/bin/procmail: $ ldd /usr/bin/procmail produces:
libm.so.6 => /lib/libm.so.6 (0x40025000)
libnsl.so.1 =>
/lib/libnsl.so.1 (0x40048000)
libdl.so.2 => /lib/libdl.so.2
(0x4005e000)
libc.so.6 => /lib/libc.so.6
(0x40062000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Tab completion:
Linux files names can be long but you don't always have to type the whole
thing. In the shell, type the first few characters and press the TAB key. If
there is more than one file with those letters in common, Linux will prompt
you, showing the remaining choices. Type a few more characters and hit TAB
again. If no files match, you may get a beep.
Using Fetchmail without Sendmail:
To fetch email without having to configure a full mail-transfer-agent like
sendmail, use: mda "/usr/bin/procmail -d username" in your
~/.fetchmail.rc file.
Root password failure (Failure of your memory as opposed to failure of
the operating system!):
At the LILO prompt (boot:), enter linux init=/bin/sh This will put
you into a shell session and you can now use: $ passwd to enter a new root
password. (Hence the need to protect your system with a LILO password. Anyone
with physical access to your machine could change the root password.) The LILO
password can be changed provided you can log in as root. If you forget the
LILO password AND the root password then maybe you should invest in another
form of security for the written passwords you will need for Linux. In all
other cases, avoid writing down your root password.
Protect your system using LILO:
LILO can use password control to prevent other users changing the boot
parameters of various boot images. Put the password password=mypassword at the
top of /etc/lilo.conf and the keyword optional on a new line beneath. Now add
the keyword restricted to any of the image sections to allow those to boot
without a password only if no extra parameters are specified at the LILO
prompt. Images without restricted added will require a password for every boot
attempt. Finally, make sure that you run (as root): $ chmod 600
/etc/lilo.conf and $ lilo sometime before next rebooting the
system! Check man lilo and man lilo.conf for more info.
LILO error messages:
If LILO fails to start Linux, it should give you one of a few possible
error codes which may help pinpoint the problem (although not necessarily
start the system!).
L means that boot.b cannot be loaded, possibly a disk
error.
LI means that boot.b has been moved or a disk error
occurred.
LIL means that LILO cannot allocate data for the map
file.
LIL? means another possible disk error.
LIL- means
the map file data is invalid.
Mounting drives and devices:
Floppy drives:
1) Create a mount point... mkdir /myfloppy
2) Mount...
mount -t vfat /dev/fd0 /myfloppy
*(unmount /myfloppy to
undo.)
CDROM drives:
1) mkdir /mycd
2) mount /dev/cdrom /mycd
Mounting other devices:
A:floppy = /dev/fd0 or /dev/floppy
B:floppy = /dev/fd1
Keyboard
= /dev/tty
Master drive on primary IDE = /dev/hda
First partition
master drive on primary IDE = /dev/hda1
Third partition on master drive
on primary IDE = /dev/hda3
Slave drive on primary IDE = /dev/hdb
Master drive on secondary IDE = /dev/hdc
Slave drive on secondary
IDE = /dev/hdd
SCSI target ID 0 = /dev/sd0
SCSI target ID 1 =
/dev/sd1
Second partition on hard disk at SCSI target ID 0 = /dev/sd02
CDROM (various) = Either /dev/cdrom or IDE/SCSI designation (/dev/hdb,
/dev/sd5, etc.)
Modem = /dev/modem
Sound system = /dev/sound
Adding hard drives, partitioning, and formatting:
File system permissions and ownership:
Starting up Gnome session:
Starting up a KDE session:
Creating archive/tarball files:
1) tar -cvf newfilename.tar filetoadd1 filetoadd2 fil...
2)
tar -cvf newfilename.tar /directorytobackup /directory2tobackup
...
To compress the archive/tarball files:
1) tar -cvzf newfilename.tgz /directorytobackup
*Note: You
could also call the new file newfilename.tar.gz, Linux does not differentiate.
Verifying an archive (from a tape, for example):
1) tar -df /dev/st0
*Note: The d argument means "difference".
It compares current directory contents to those on the tape device. The -v
option shows every file name.
Seeing what is in an
archive:
1) tar -tvf /dev/st0
*Note:The -v option will show owners and
permissions.
Restoring an archive:
1) tar -xvpf /dev/st0
*Note: This will restore EVERYTHING! The
-p argument will preserve permissions on the files.
2) tar -xvpf
/dev/st0 /directorytorestore
*Note: This will restore a particular
directory
To find out where a program resides or which version is
active:
1) which programname
*Example - which smbd
To see
"hidden" files:
1) ls -al (The -a argument is the key.)
Making files
executable:
Services - Start, stop, list,
and run levels:
1) chkconfig --list (Lists all services at each run
level.)
2) chkconfig --level2345 gpm off (Example - Turns off mouse
service at 4 run levels.)
*NOTE: Service stays off after next
reboot.
Mounting a remote SMB/Windows share:
1) smbmount //server/share /mountpoint -o
username=jdoe,password=whatever
OR try
using...
mount -t smbfs -o username=jdoe,password=whatever
//servername/share /mountpoint
(*NOTE: Password arguments are optional,
you will be prompted for one if not included. You must be logged in as root to
mount the remote share. Also, the user account used MUST be a member of the
domain THAT THE TARGET MACHINE IS A MEMBER OF.)
Creating a
symbolic link / shortcut:
1) ln -s file newlinklocation