- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Find and replace text in a passwd file....
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
Forums
Discussions
Discussions
Discussions
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
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
06-27-2003 07:30 AM
06-27-2003 07:30 AM
I wanna do a script to manage my users and I want to have a function that can deactivate user by replacing in the /etc/passwd file the crypted password with the * caracter...
Example:
User1:e94xsM0XKbu4E:367:230:User1 user:/home/User1:/bin/ksh
I want to replace "e94xsM0XKbu4E" by "*" to make the line in the file look like that:
User1:*:367:230:User1 user:/home/User1:/bin/ksh
I know that I can use sed to do that, but I don't clearly understand how it work?? Can somebody help me with that??
Thanks
Jonathan
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:35 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:36 AM
06-27-2003 07:36 AM
Re: Find and replace text in a passwd file....
cat /etc/passwd | grep User1 | sed 's/e94xsM0XKbu4E/*/' >/etc/passwd.new
mv /etc/passwd.new /etc/passwd
chmod 444 /etc/passwd
Sed simply looks for text (within the '/') and replaces it with the 2nd field (the '/*/').
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:36 AM
06-27-2003 07:36 AM
Re: Find and replace text in a passwd file....
In this case,...
cat /etc/passwd |awk -vuser=$USER -F":" '$1 == user {$2="*"}{print $1":"$2":"$3":"$4":"$5":"$6":"$7}' >/etc/newpasswd
cp /tmp/newpasswd /etc/passwd
This script tested and approved for human consumption. Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:37 AM
06-27-2003 07:37 AM
Re: Find and replace text in a passwd file....
Editing the 'etc/passwd' directly isn't the safest mode in which to operate. You can easily accomplish what you want with:
# passwd -r files -l User1
This locks the account. In an untrusted system this puts a "*" in teh encrypted password field.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:37 AM
06-27-2003 07:37 AM
Re: Find and replace text in a passwd file....
cat /etc/passwd | sed 's;^\([^:]*\):[^:]*:\(.*\)$;\1:*:\2;'
will do it
- ^\([^:]*\): defines \1 as all chars from begining to first ':'
- [^:]*: defines password fields as all char up to next ':'
- \(.*\)$ defines \2 as all char up to end of line
- \1:*:\2 replace pattern by \1:*:\2
HTH
Jean-Louis.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:40 AM
06-27-2003 07:40 AM
Re: Find and replace text in a passwd file....
Reading other's posts I must agree that it's not really the safest way of doing it ... But it shows you what sed and awk can do anyway :-)
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:40 AM
06-27-2003 07:40 AM
Re: Find and replace text in a passwd file....
BTW thanks all, I'll be able to use sed now!!!
ciao!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:43 AM
06-27-2003 07:43 AM
Re: Find and replace text in a passwd file....
If you ruin your password file, you'll disable your system, and you do not want that to happen!
a5:/tmp 113 > diff passwd /etc/passwd
a5:/tmp 114 > perl -pi -e'BEGIN{($usr,@ARGV)=(@ARGV,"passwd")}s/^$usr:[^:]+:/$usr:*:/' broomer
a5:/tmp 115 > diff passwd /etc/passwd a5:/tmp 116 > perl -pi -e'BEGIN{($usr,@ARGV)=(@ARGV,"passwd")}s/^$usr:[^:]+:/$usr:*:/' gert
a5:/tmp 117 > diff passwd /etc/passwd 18c18
< gert:*:201:200:G. Hoff,,26,0220717600:/u/usr/gert:/pro/bin/tcsh
---
> gert:4k3K1WTnC3SAg:201:200:G. Hoff,,26,0220717600:/u/usr/gert:/pro/bin/tcsh
Exit 1
a5:/tmp 118 >
Enjoy, have FUN! H.Merijn [don't break your sys]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 07:48 AM
06-27-2003 07:48 AM
Re: Find and replace text in a passwd file....
You wanna lock user so don't do it by replace
the encripted passwd to "*" use option of passwd -l
That's do the job well.
Caesar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 08:03 AM
06-27-2003 08:03 AM
Re: Find and replace text in a passwd file....
you can try the following with awk
nullify_pw() {
rm /tmp/passwd-new >/dev/null 2>&1
cat /etc/passwd | gawk -v USER=$1 '
BEGIN {
FS = ":"
OFS = ":"
}
{
if ($1 == USER) {$2 = "*"; print
} else {
}
}' >>/tmp/passwd-new
cp /tmp/passwd-new /etc/passwd
}
and call nullify_pw with the username.
HTH,
Michael