Environment variable changing on Linux/FreeBSD

last updated in Categories , ,

Environment variables are a set of values that can affect the way running processes will behave. It is stored in the shell’s environment space. This space can be used by any program invoked by the shell, and thus contains a lot of program configuration. Linux (and FreeBSD/Solaris/UNIX) process use environment variable for different purposes. Running programs can access the values of environment variables for configuration purposes. Examples of environment variables include:

  • PATH : Search for executable files
  • SHELL : Your current shell

You can display values of these environment variables with echo command:

$ echo $PATH
$ echo $HOME

Setting (changing) an environment variable on Linux/FreeBSD
Setting an environment variable differs from shell to shell. Setting an environment variable for csh/tcsh shell on Linux/FreeBSD:
Syntax:
setenv variable value

For example, to set or modify the PAGER environment variable under csh or tcsh you need to type command:

$ setenv PAGER less

Setting an environment variable for sh/bash or ksh shell
Syntax:
export variable=value

For example, to set or modify the EDITOR environment variable under sh or bash shell you need to type command:

$ export EDITOR=vim
$ export EDITOR=/usr/bin/vim

Shell configuration files
These environment variables defined in shell startup files so that when ever you login next time they are reloaded automatically for you. Under Linux bash is the default shell. Following list summaries bash startup files:

  • /etc/profile: The systemwide initialization file, executed when you login into system. Only super-user (root) can make any changes to this file.
  • /etc/bash.bashrc (/etc/bashrc): The systemwide per-interactive-shell startup file. Only super-user (root) can make any changes to this file. Generally, bash.bashrc is called from /etc/profile file.

Use above files to setup system wide variables such as JAVA path. Since normal users do not have access to these file they can use following two files:

  • /home/user/.bash_profile (~/.bash_profile): The personal initialization file, executed when you log into system.
  • /home/user/.bashrc (~/.bashrc): The individual per-interactive-shell startup file

Use text editor such as vi to modify or define new environment variable for you. For example, setup EDITOR variable in your .bash_profile file:

$ vi  ~/.bash_profile

OR

$ cd
$ vi .bash_profile

Add/modify EDITOR as follows:

$ export EDITOR=/usr/bin/vim

Save file logout and login again or just type following command to load environment changes immediately:

$ ./.bash_profile

See also:

Linux / UNIX find out what other users are doing?

last updated in Categories , , , , , ,

Q. Can you explain the command to find what users are doing on my UNIX / Linux system?

A. Both Linux and UNIX (FreeBSD/Solaris) has w command to show who is logged on and what they are doing.

The w command prints a summary of the current activity on the system, including what each user is doing.

=> The first line displays the current time of day

=> How long the system has been running

=> The number of users logged into the system

=> The load averages. The load average numbers give the number of jobs in the run queue averaged over 1, 5 and 15 minutes.

You can also use ps command which shows you process that are running on the system.

Type w command displays information about the users currently on the machine, and their processes.

Show who is logged on and what they are doing with w command

The fields output are the user’s login name, the name of the terminal the user is on, the host from which the user is logged in, the time the user logged on, the time since the user last typed anything, and the name and arguments of the current process.
$ w
Output:

radm    pS 66.90.90.102     Sun01PM  1day -bash
raj     pW 192.168.1.100.  7:42AM     5 ssh root@202.54.1.20
miku    pX a80-186-82-84.el  7:28AM    10 screen irssi
vivek   pY 196.15.193.111    4:11AM     0 nano -w hireme
rani    q0 dslbr0.bsnl.in    7:32AM    12 lynx http://slashdot.org/
jadmin  q2 dslbr5.bsnl.in    7:33AM     0 ssh jadmin@host.cyberciti.info
gad     q3 dslbr76.bsnl.in   7:40AM     0 -ksh
bencs   q5 dslbr22.bsnl.in   7:44AM     5 -zsh
vivek   q6 gw11-vsnl.in      7:47AM    11 -bash

You can use the ps command shows you processes that are running on the system:

$ ps -au | more
$ ps -au | less

So you can use both w and ps commands to find out who’s doing what.

How can I find out who is logged on my UNIX / Linux system?

last updated in Categories , , , , , ,

Q. How do I display who is on the UNIX / Linux system?

A. On a Linux (on Solaris/FreeBSD or any other UNIX) system, many users will be sharing the same server.

Users will use telnet (outdated and insecure) or ssh (secure and highly recommended) to login remotely.

So if you want to find out your friend or a coworker is logged in or not, use the following commands.

If you want to find out who’s logged in on the Linux server including what time they logged in and from which network computer then you can use who command:

who command ~ show who is logged on

who commands works with almost all Linux and UNIX like oses. It show who is logged on to your system. It displays information about currently logged in users. By default, this includes the login name, tty name, date and time of login and remote hostname if not local.
$ who
Output:

raj     ttypV    Jan 17 07:23   .     (192.168.1.10)
ben     ttypW    Jan 17 07:42   .     (192.168.1.11)
miku    ttypX    Jan 17 07:28   .     (user-del-net-202.vsnl.net.in)
root    ttypY    Jan 17 04:11   .     (196.15.183.151)
roomy   ttyq0    Jan 17 07:32   .     (org-rev-1.bsnl.net.in)
anita   ttyq2    Jan 17 07:33   .     (192.168.5.112)
gads    ttyq3    Jan 17 07:40   .     (gtw-1.nixcraft.in)
bencs   ttyq5    Jan 17 07:44   .     (dsl5.bsnl.co.in)
pol20um ttyq6    Jan 17 07:47   .     (gtw-2.nixcraft.co.in)

Sometime you just want to find out if user raj logged in or not then you can use grep command:

$ who | grep raj

Try out following command if you have more than 20+ users logged in (so that you can see one page of logged in users at a time):

$ who | less
$ who | more

Solaris: Forcefully unmount a disk partition to get rid of device busy error

last updated in Categories

Under Solaris, umount come does not allow a file system to be unmounted if a file on the file system is busy i.e. open by other programs/process or by user. You will get device busy error. To get rid of this message you can pass -f option to umount command. Suppose you would like to forcefully unmount a /cdrom/cdrom0 then you can type following command (login as a root user):
# umount -f /cdrom/cdrom0

Please note that using this option can cause data loss for open files; program(s) which access files after the file system has been unmounted will get an error.

How do I find what dependencies a rpm file has?

last updated in Categories

RPM is a Package Manager for popular distribution such as Red Hat, Suse and many others. It is a powerful command line package management system for installing uninstalling, verifying, querying, and updating Linux computer software packages.

You can finding out what dependencies a rpm file has i.e. it will tell you what you need to install package with following command:
rpm -qpR {.rpm-file}
rpm -qR {package-name}

Find out what dependencies a uninstalled rpm file called mediawiki-1.4rc1-4.i586.rpm:
# rpm -qpR mediawiki-1.4rc1-4.i586.rpm

It will print list of dependencies on screen (output):

mod_php
php-session
php-gettext
php-zlib
php-mysql
ImageMagick-Magick++
tetex
cjk-latex
rpmlib(PayloadFilesHavePrefix)

However RPM has in build mechanism to tell you about dependencies. Just try to install package and it will give you list of dependencies.
# rpm -ivh mediawiki-1.4rc1-4.i586.rpm

Output:

error: Failed dependencies:
   mod_php is needed by mediawiki-1.4rc1-4
   php-session is needed by mediawiki-1.4rc1-4
   php-gettext is needed by mediawiki-1.4rc1-4
   php-zlib is needed by mediawiki-1.4rc1-4
   php-mysql is needed by mediawiki-1.4rc1-4
   ImageMagick-Magick++ is needed by mediawiki-1.4rc1-4
   tetex is needed by mediawiki-1.4rc1-4
   cjk-latex is needed by mediawiki-1.4rc1-4

Note:

  • .rpm file: File with .RPM extension. Typically this file is not installed. It may be on CD or you just downloaded from Internet.
  • package-name: It is installed RPM file.

You can solve dependencies problem by installing each individual package(s). If you are using Red hat Linux then you can try this tip. If you are using Fedora core Linux then try yum. If you are using Suse linux then use Yast to install rpms.