Sunday, August 3, 2014

Unix Prog: Unix System Overview - User ID

1. User Identification

User ID: used to identify the user to the system.Saved in /etc/passwd
Group ID: used to identify a group of users, saved in /etc/group

/etc/passwd: map the user name to user id
/etc/group: map the group name to group id

user id: 0 is the root user.

2. Printout current user id, group id
uidgid.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(void)  
 {  
  printf("uid=%d, gid=%d\n", getuid(), getgid());  
  exit(0);  
 }  

shell:
1) List the details information for this file, its permission means: owner could have read, write, execute permission, same group member could have read, write, execute permission, and other users only have read and execute permission.
And its user name(owner) is ubuntu, group name is also ubuntu.
2) Run the ./uidgid.out, and print out the current user id and group id. From output, we can know that "ubuntu"'s user id is 1000, group "ubuntu"'s id is also 1000.
 ubuntu@ip-172-31-23-227:~$ ls -lrt ./uidgid.out  
 -rwxrwxr-x 1 ubuntu ubuntu 9684 Aug 3 21:31 ./uidgid.out  
 ubuntu@ip-172-31-23-227:~$ ./uidgid.out  
 uid=1000, gid=1000  

Explanation:
getuid() and getgid() is defined at unistd.h
 ubuntu@ip-172-31-23-227:~$ less /usr/include/unistd.h  
 ......  
   
 /* Get the real user ID of the calling process. */  
 extern __uid_t getuid (void) __THROW;  
   
 /* Get the real group ID of the calling process. */  
 extern __gid_t getgid (void) __THROW;  
   
 ......  

user ubuntu's information is saved at /etc/passwd:
group ubuntu's information is saved at /etc/group:
1) Find the user record whose user name is ubuntu
2) Find the group record whose group name is ubuntu
3) Print out the content of /etc/passwd, its first column is user name, 4th column is group id. We pick up these 2 columns, and select the user record with group id 1000(ubuntu). By this way, we can select all user records who belong to group ubuntu.
 ubuntu@ip-172-31-23-227:~$ grep ubuntu /etc/passwd  
 ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash  
 ubuntu@ip-172-31-23-227:~$ grep "^ubuntu" /etc/group  
 ubuntu:x:1000:  
 ubuntu@ip-172-31-23-227:~$ cat /etc/passwd | cut -d ":" -f 1,4 | grep 1000  
 ubuntu:1000  

No comments:

Post a Comment