Get Username From UID in Unix

Finding out the username by user id (uid) in Unix is not as common a task as determining the uid by a username, but if you need to do it – I’ll show you how.

How to find the username using user id (uid)

Simply use the getent command. Most common use for it is to query the passwd source for a username, like this:

ubuntu$ getent passwd greys
greys:x:1000:113:Gleb Reys,,,:/home/greys:/bin/bash

however, if you query for a user id instead (1000 in this case), getent will work just as good:

ubuntu$ getent passwd 1000
greys:x:1000:113:Gleb Reys,,,:/home/greys:/bin/bash

That’s all there is to it! Enjoy!




What UUIDs Are and How To Use Them in Ubuntu

If you tried installing or upgrading Ubuntu recently, you probably noticed that all the storage devices are now using UUID – Universally Unique IDentifiers. I’m not claiming to know everything there is to know about UUIDs, but have become quite comfortable managing them lately, so hopefully this post will help you achieve the same.

What is a UUID exactly?

UUID is a Universally Unique IDentifier. It’s a identification code given to each storage device you have on your system, aimed to help you uniquely identify each device no matter what.

UUIDs can be used to identify DVD drives, removable media (USB flashsticks) and each partition on any of your hard drives.

This is how a typical UUID looks:

c73a37c8-ef7f-40e4-b9de-8b2f81038441

Why use UUID?

Reason 1: Truly unique identification

UUID is the only way to guarantee you recognize the same drive or partition no matter what. For example, if you introduce to your system another hard drive, this might upset quite a few things, starting with the way your system boots up (or stops booting up upon the new drive introduction). Using UUID helps remedy most of such things.

Reason 2: Device names are not always persistent

Automatically assigned device names in your system are not consistent, they are according to the order of loading the kernel modules up during (most usually) the startup time, and thus the names will look different if you boot with one of your USB flashsticks attached and then reboot after you plug it out.

Generally, I’ve also found UUIDs really useful for mounting my removable media – I have a USB reader, one of the 24-in-1 kinds – it supports various types of cards and I use UUID to always mount the same card at the same location.

Reason 3: Most critical functionality of your Ubuntu system already depends on UUIDs

GRUB – your boot loader – relies on UUIDs as well. If you look into /boot/grub/menu.lst file, you’ll find something similar to this:

title Ubuntu hardy (development branch), kernel 2.6.24-16-generic
root (hd2,0)
kernel /boot/vmlinuz-2.6.24-16-generic root=UUID=c73a37c8-ef7f-40e4-b9de-8b2f81038441 ro quiet splash
initrd /boot/initrd.img-2.6.24-16-generic
quiet

List UUIDs for all your devices

If you are using one of the recent releases of Ubuntu (UUIDs have been there since Edgy), you can use the blkid command to get a list of all the drives and partitions along with their UUIDs:

ubuntu# blkid
/dev/sda1: UUID="2220CF8220CF5B83" TYPE="ntfs"
/dev/sda2: UUID="48E81F29E81F14B2" LABEL="DRIVE-D" TYPE="ntfs"
/dev/sdb1: UUID="c73a37c8-ef7f-40e4-b9de-8b2f81038441" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdb5: TYPE="swap" UUID="abe7529e-dcd5-4afc-b714-05569dbcd30b"
/dev/sdb6: UUID="f34c8c7c-a020-4a14-8c97-257180240250" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdb7: UUID="8fa274ca-5b22-411f-b5da-7469c1f276da" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc1: UUID="1e36f323-c4e5-4f55-ba0a-838643550bf9" TYPE="ext3" SEC_TYPE="ext2"
/dev/sdc2: UUID="83aa92e4-4df4-4aab-80f3-9bbb447e0459" TYPE="ext3" SEC_TYPE="ext2"

As you can see, I’ve still got a few NTFS partitions as I’m slowly migrating my data from Windows after my switch to Ubuntu desktop a couple months ago.

Get UUID for a particular device

If you know a device name and just want to confirm the UUID for it to later use it in /etc/fstab, here’s how you can do it using vol_id command:

ubuntu# vol_id -u /dev/sdb1
c73a37c8-ef7f-40e4-b9de-8b2f81038441

That’s all I can think of so far. I know a few more things about UUID which I’ll share in a separate post, but it’s a start.

Have you got any more great ideas and tips for UUID? Let me know and I’ll be sure to share them with others in the future posts.

Related books

If you want to learn more, here’s a great book:


ubuntu-kung-fu-practical-guide
Ubuntu Kung Fu

See also:




Using variables in Unix shell scripts

Any Unix shell script longer than a line will most likely involve using variables. Variables are used to store temporary values to simply using them in various Unix commands of your script. The beauty of using variables is that they can be evaluated and set ones, but then reused as many times as you like without your shell interpreter having to re-evaluate them again.

Defining a variable in Unix shell

To specify a value for a variable, you need to decide on the variable name – can be any word or combination of English alphabet symbols and digits, and specify the value.

In Bourne shell (sh), Bourne Again Shell (bash) and Korn Shell (ksh), here’s how you would define a new variable and assign it a value:

CONFIG_FILE="/etc/myfile"

In C-Shell (csh), it’s done like this:

setenv CONFIG_FILE "/etc/myfile"

Basic variables usage

To access the value of a variable, you need to use the same variable name, but with a dollar sign in front of it, like this:

$CONFIG_FILE

Important: to set a new value to the variable, you use just the variable name like CONFIG_FILE, but to access this value later you need to use the $CONFIG_FILE form.

The most basic way to use variables is to assign them constant values at the beginning of your script. This is a good way to define locations of standard files or directories your script will work with, etc:

#!/bin/sh
#
CONFIG_FILE=/etc/myfile
MY_DIR=/etc
echo $CONFIG_FILE

Using output of Unix commands to set variables

One of the best things about shell scripting is that it’s very easy to use any Unix command to generate the output and use it to set the variable.

In this example, I’m running a date command and saving its output as values for my variables:

ubuntu$ cat /tmp/1.sh
#!/bin/sh
#
STARTED=`date`
sleep 5
FINISHED=`date`
#
echo "Script start time: $STARTED"
echo "Script finish time: $FINISHED"

If I run this simple script, I see the following:

ubuntu$ /tmp/1.sh
Script start time: Wed May 7 04:56:51 CDT 2008
Script finish time: Wed May 7 04:56:56 CDT 2008
The same approach can be used for practically any scenario.

Here’s an example of using uname command to extract some useful information about our system:

#!/bin/sh
#
STARTED=`date`
NODE=`uname -n`
OS=`uname -o`
CPU=`uname -p`
FINISHED=`date`
#
echo "Nodename: $NODE"
echo "OS type: $OS"
echo "Processor: $CPU"
echo "Script start time: $STARTED"
echo "Script finish time: $FINISHED"

And this is how it works:

ubuntu$ /tmp/1.sh
Nodename: ubuntu
OS type: GNU/Linux
Processor: unknown
Script start time: Wed May  7 05:05:31 CDT 2008
Script finish time: Wed May  7 05:05:31 CDT 2008

That’s it for today! Let me know what next topic about Unix shell scripting you’d like to see covered, and I’ll do my best to explain it in the coming posts.

Related books

If you want to learn more, here’s a great book:


linux-command-line-shell-scripting-bible
Linux Command Line Shell Scripting Bible




How To Show a Processes Tree in Unix

Showing your processes in a hierarchical list is very useful for confirming the relationship between every process running on your system. Today I’d like to show you how you can get tree-like processes lists using various commands.

Showing processes tree with ptree

In Solaris, there’s quite a few commands which make the life of any system administrator much easier, they’re the process commands (p-commands). One of them which I particularly like is the ptree command which shows you a list of processes.

As you run the command, you get a hierarchical list of all the processes running on your Solaris system, along with process IDs (PIDs). To me, this is a very useful command, because it shows you how exactly each process relates to others in your system.

Here’s a fragment of the ptree output:

bash-3.00$ ptree
7     /lib/svc/bin/svc.startd
  250   /usr/lib/saf/sac -t 300
    268   /usr/lib/saf/ttymon
  260   -sh
    5026  -csh
9     /lib/svc/bin/svc.configd
107   /usr/lib/sysevent/syseventd
136   /usr/lib/picl/picld
140   /usr/lib/crypto/kcfd
159   /usr/sbin/nscd
227   /usr/sbin/rpcbind
234   /usr/lib/nfs/statd
235   /usr/sbin/keyserv
236   /usr/lib/netsvc/yp/ypserv -d
  237   rpc.nisd_resolv -F -C 8 -p 1073741824 -t udp
241   /usr/lib/nfs/lockd
247   /usr/lib/netsvc/yp/ypbind
263   /usr/lib/utmpd
286   /usr/sadm/lib/smc/bin/smcboot
  287   /usr/sadm/lib/smc/bin/smcboot
  288   /usr/sadm/lib/smc/bin/smcboot

Processes tree with pstree

In most Linux distributions, you can find a pstree command, very similar to ptree.

That’s how you may use it (-p is an option to show PIDs and -l uses long output format):

ubuntu$ pstree -pl
init(1)─┬─NetworkManager(5427)
        ├─NetworkManagerD(5441)
        ├─acpid(5210)
        ├─apache2(6966)─┬─apache2(2890)
        │               ├─apache2(2893)
        │               ├─apache2(7163)
        │               ├─apache2(7165)
        │               ├─apache2(7166)
        │               ├─apache2(7167)
        │               └─apache2(7168)
        ├─atd(6369)
        ├─avahi-daemon(5658)───avahi-daemon(5659)
        ├─bonobo-activati(7816)───{bonobo-activati}(7817)
...

Showing processes tree with ps –forest

ps command found in Linux has a –forest option, which shows you a tree structure of processes.

The best in my experience is to use it like this:

ubuntu$ ps -aef --forest
UID        PID  PPID  C STIME TTY          TIME CMD
...
107       5473     1  0 10037  4600   0 Apr28 ?        00:00:02 /usr/sbin/hald
root      5538  5473  0  5511  1288   0 Apr28 ?        00:00:00  \_ hald-runner
root      5551  5538  0  6038  1284   0 Apr28 ?        00:00:01      \_ hald-addon-input: Listening on /dev/input
107       5566  5538  0  4167   992   1 Apr28 ?        00:00:00      \_ hald-addon-acpi: listening on acpid socke
root      5600  5538  0  6038  1272   1 Apr28 ?        00:00:15      \_ hald-addon-storage: polling /dev/scd0 (ev
root      5476     1  0 10272  2532   0 Apr28 ?        00:00:00 /usr/sbin/console-kit-daemon
root      5627     1  0 12728  1176   1 Apr28 ?        00:00:00 /usr/sbin/sshd
root      9151  5627  0 17536  3032   0 10:53 ?        00:00:00  \_ sshd: greys [priv]
greys     9162  9151  0 17538  1892   1 10:54 ?        00:00:00      \_ sshd: greys@pts/3
greys     9168  9162  0  5231  3820   1 10:54 pts/3    00:00:00          \_ -bash
greys     9584  9168  0  3802  1124   0 11:27 pts/3    00:00:00              \_ ps -aeF --forest

This output is for demonstration purpose only, and so I’ve taken the first lines of the output out because they weren’t serving the purpose of this example very well.

For thins fragment of the output you can see how you get all the vital information about each process. I really like this way of running the ps command.

That’s it for today! Do you know any other neat way of looking at processes tree? Let me know!