LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-04-2005, 11:31 PM   #1
n3rds3x
LQ Newbie
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
Is there an easier way to edit /etc/hosts?


My hosts file is big, I'm talking 1.1 MB. I want to try to reduce the size of the file by editing it to remove duplicate entries and all comment fields (i.e #). However, to try this by hand would take a long time. I tried to edit the file by hand and in about an hour, I was maybe 1/4 of the way done. I don't have the time to edit the file by hand, so I tried searching for Linux program or script that would do the work for me. I have been unable to find anything so I turned to the boards. When I ran Windows, I would edit the file using a program called Hostess. However, Hostess doesn't function properly when running it on Linux with Wine.

If anyone knows of a program similar to Hostess for Linux or a script, please let me know.
 
Old 05-04-2005, 11:51 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
My first thought would be to
sed -i '/^#.*/d' /etc/hosts
which will get rid of all comments ...

Then a
sort -u /etc/hosts > /etc/hosts.tmp
which should sort the file and (if the lines are
EXACTLY the same) get rid of entires that are
dupes ...

If you have slight differences in the file you'd be
wanting to use another approach.
sort -k 1,1 -u /etc/hosts > /etc/hosts.tmp

If your sed doesn't support in-place editing try
sed '/^#.*/d' /etc/hosts | sort -k 1,1 -u /etc/hosts > /etc/hosts.tmp

I've created a dupe of my hosts, entered a few duplicate
lines and with those two commands got the file from 1355
to 352 bytes in under a second :)


Cheers,
Tink

Last edited by Tinkster; 05-04-2005 at 11:54 PM.
 
Old 05-04-2005, 11:56 PM   #3
craigevil
Senior Member
 
Registered: Apr 2005
Location: OZ
Distribution: Debian Sid/RPIOS
Posts: 4,883
Blog Entries: 28

Rep: Reputation: 533Reputation: 533Reputation: 533Reputation: 533Reputation: 533Reputation: 533
Why do you have such a large HOSTS file?

You do not have to worry about 99% of the spy/adware that effects Windows.


I have casalemedia.com, paypopup.com, fastclick.net, doubleclick.net in my HOST file; just to block popups.

Like you when I was running windows my HOSTS file was huge.
 
Old 05-05-2005, 12:08 AM   #4
n3rds3x
LQ Newbie
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 2

Original Poster
Rep: Reputation: 0
I tried...

sudo sort -u /etc/hosts > /etc/hosts.tmp

and received a permission denied error. So, I then ran:

sudo sort -u /etc/hosts

and it appeared to scan my file, although nothing appears to have been removed.

I ran:

sort -k 1,1 -u /etc/hosts

which appeared to scan my file, however very few entries were removed.

I think the problem is that because I appended additional hosts files I found on-line to my file, there are so many duplicate entries and formatting differences that the commands were unable to correctly edit and sort my file. I don't know how accurate this information is as I've been using Linux for only one year so I'm not that experienced with it yet.

Quote:
Why do you have such a large HOSTS file?
I want to block as many advertisements, web bugs, data brokers, and other malicious Internet entities as possible, even though they may or may not affect Linux. Simply put, I'm paranoid.

Sometimes I think it would be easier for me just to disconnect my PC from the Internet. I spend more time trying to harden my PC against intruders and other Internet nasties than I do using it for "normal use".
 
Old 05-05-2005, 05:42 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Your last problems sounds like you didn't su to root first. Files in /etc can't be edited by normal users.

Perhaps you would rather be running a proxy server to screen out these sites. There are blacklists containing over 100,000 sites. I'd hate to maintain that many entries in the hosts file. You could run this on a computer that accesses the internet, and protect the other computers on the network.
http://www.screaming-penguin.com/main.php?storyid=4767

This post contains a link that may be more comprehensive.
http://www.linuxquestions.org/questi...ight=blacklist

Handling such a large number of website aliases, perhaps you want to use bind instead.

Last edited by jschiwal; 05-05-2005 at 06:16 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
External Hosts Resolve but Local Hosts Do Not kjm9 Linux - Networking 7 11-19-2005 03:51 PM
edit /etc/hosts ninapratt Linux - Newbie 15 09-19-2005 01:49 PM
Cannot edit hosts file k3nn0n Fedora 3 05-26-2004 11:00 AM
Adding shell commands to hosts.deny and hosts.allow ridertech Linux - Security 3 12-29-2003 03:52 PM
error on start up---edit /etc/hosts emacin777 Linux - Networking 1 09-03-2003 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration