- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script to check user id
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2011 02:08 AM
тАО02-09-2011 02:08 AM
I just to create a script who can run only root user and once any other user(not root) will try to execute, will be getting non-root error.
I believe for that we need to compare user id and in the starting of the script and if it goes corect then script will run.
Appreciating your reponse.
Solved! Go to Solution.
- Tags:
- id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2011 02:14 AM
тАО02-09-2011 02:14 AM
Re: Script to check user id
Are you talking about setting setuid on the script so that the user executing the script gains the permission as the owner has for that file.
What are the current permission on the file.
Thanks
Manix
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2011 02:37 AM
тАО02-09-2011 02:37 AM
Solution-----
#!/bin/sh
if [[ $(whoami) != "root" ]]; then
echo "${0##*/}: Access Denied: you must be root to run this script."
exit 1
fi
# If we get this far, we must be running as root.
# Add whatever you want here.
echo "OK, you're root"
-----
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2011 03:15 AM
тАО02-09-2011 03:15 AM
Re: Script to check user id
First review these "id" command options:
#id -u
Display the effective user ID
-or-
#id -ur
Display the real ID instead of the effective ID
Then you can asign one of them to a variable and ask if the content is "root":
WHOIS=`id -ur`
if [ "$WHOIS" != "root" ]
then
echo "root privileges are requested to run this script"
exit 99
fi
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2011 06:20 AM
тАО02-09-2011 06:20 AM
Re: Script to check user id
I would query the UID of the process with 'id -u'. This will return zero (0) if the 'root' user is the real *or* the effective ID.
If you need to distinguish between the real and the effective UID of a process, add the '-r' option to 'id' as 'id -ur'.
If the real UID and the effective UID aren't the same, the user may have performed an 'su -' operation, or the process may have been 'setuid'.
I prefer to use 'id' as opposed to 'whoami' or 'who am i' since it's the numeric UID of zero that signifies "root" or "superuser" privilege. One could, but should not, have multiple UID=0 accounts in '/etc/passwd' and the name "root" is ambiguous in that case.
Regards!
...JRF...