Count the Number of Words in a Text File (OSX, Linux, BSD)

Say you have a text file,  and you want to know either how many lines are in the file, how many words are in the file, or how many characters are in the file.

How can you do that?  Counting them manually would take way too long.  This is where wc comes in!

The wc manpage (this is the Ubuntu manpage) shows the options, but using it is simple from a terminal:

Count the number of lines:

wc -l filename.txt

Count the number of words:

wc -w filename.txt

Count the number of characters:

wc -m filename.txt

That will give a basic look at the number of lines, words, or characters of the supplied text file – there are many other small command line utilities which can be used in conjunction with wc to do all sorts of clever things, and I hope to produce further posts on these in the near future.

Framebuffer Resolution Codes (Linux Console)

If you’re used to running Framebuffer for higher resolution consoles within Linux console, then you’ll be used to the vga=xxx switch on your bootloader.  As this takes a three letter code for various resolutions, some may use vga=791 for example – but what if you want a different resolution?  You need a different code, which are below:

     +-------------------------------------------------+
          | 640x480    800x600    1024x768   1280x1024
      ----+--------------------------------------------
      256 | 0x301=769  0x303=771  0x305=773   0x307=775
      32K | 0x310=784  0x313=787  0x316=790   0x319=793
      64K | 0x311=785  0x314=788  0x317=791   0x31A=794
      16M | 0x312=786  0x315=789  0x318=792   0x31B=795
     +-------------------------------------------------+

So, for example, if you want 1280×1024 at 16M colours, you’d use vga=795… or for 800×600 at 64k colours, you’d use vga=788 and so on.

Taking a Screenshot in OSX

Wondering where that Print Screen button is you’re used too?  Well, it’s not available on a Mac – instead, there is a key combination to take a screenshot (remember to press all buttons at the same time in this order!):

Full Screenshot

Apple/cmd + Shift + 3

The file will then be put onto your desktop with the name “Picture (number)”

Single Window/Area Shot

Apple/cmd + Shift + 4 (See notes below)

This will allow you to select, using the mouse, the area you want to take a screenshot of (just click the mouse and drag the area you want) or, if you press the spacebar straight after (So Apple/cmd + Shift + 4, release these keys then press spacebar – you’ll see the mouse cursor change to a camera) , you can click the window you want an image of. Again, the image file will be placed on the desktop with the name “Picture (number)”.

Both of these methods simply create an image file on the desktop – it doesn’t place the image onto the clipboard like you may be used to in Microsoft Windows.

Vim Text Editor: Guides on how to use it

New users to Linux or BSD, will have no doubt come across Vim (or at least Vi). Vim is “Vi IMproved” – it’s simply an extended Vi.

You’ll have probably had times when you need to do something very simple, such as Cut and Paste, and not been able to, just because you don’t know how.  You’ve also then resorted to using the window manager cut and paste, and relying on that (which does work, but isn’t very “Vim”!)

I use Vim a lot, yet I still only use some very very basic commands, which isn’t ideal, nor is it very productive, although I do get by.

I, probably like you, want to learn how to use Vim properly, so went in search for some good help.

@peteog pointed me towards a couple of resources, that after looking at them, seem extremely good and easy to follow.  I’ve certainly started using some of the things on them.

The first resource is “Vim Recipes” found here:  http://vim.runpaint.org.  Vim Recipes is an online cookbook, and of course, a free resource.  I highly recommend you go through the book online, or download the PDF.

The second resource, which may even find it’s way into a frame and on my wall, is: http://www.viemu.com/vi-vim-cheat-sheet.gif – this is a great piece of reference material you can have nearby, so whilst you’re using Vim, you can have a quick glance over if you need a particular feature.

Hopefully with those two resources, we’ll be well on our way to being better Vim users!  Please let me know in the comments of any other good resources you may have or have used in the past!

Basic Steps to Securing OpenSSH from Random Attacks

If you run an SSH server, and have left it on the default port, you’ll probably notice in time lots of login attempts. These login attempts will be from random IP addresses, and usually try lots of different username/password combinations, and hopefully failing.

This article will focus on OpenSSH – I’m not sure if all SSH daemons have similar options (such as AllowUsers).

Unfortunately, people realised that administrators often used usernames such as “test” with password “test” to, you guessed it, test certain functionality. They also realised that administrators would leave these accounts in place even after testing.  This is obviously a big security risk – if anyone was to guess “test” and “test” (and lets face it, it’s not that difficult), they would have access to your server.  This goes for any common username with a weak password combination.

Obviously it’d be very boring for someone to sit down and try lots of different IP addresses on the internet, check to see if ssh is open, and if it is, try lots of random username/password combinations.  To get around this, bots/scripts were developed to carry out this task – start it up, and let it go – any successful attempts, tell the user and move on to the next potential victim.

So, what can you do to combat this at a very basic level?  Well, as they’re usually bots, then a few basic steps can go a long way to preventing them from probing your server:

  • Change the SSH daemon to run on a different port – by default, SSH runs on port 22/tcp.  This is the port which SSH runs on “out of the box”.  It’s possible to change this to something random (providing it’s not being used by something else) and that’s a good first step.  Of course, if they did try a different port, and found your SSH service, it makes this step pretty useless, but they tend to just stick at the default for the initial probing.
    The required option in sshd_config is the “Port” line; to change the port, it would be “Port 2202″ for example to put it on Port 2202 (tcp).  Bear in mind though, it’s easy to lock yourself out if you’ve changed the port on the ssh daemon but forgotton to change any firewalling/routing – you have to allow the new port through your router, firewall, etc.
  • Only allow certain users SSH access – the AllowUsers setting in sshd_config is useful in that you can restrict SSH login to certain users – what this means is that any random attempt, where the username isn’t listed under the AllowUsers, will be blocked.  Also, you should disallow root logins directly via SSH.
    The required option in sshd_config is the “AllowUsers” line.  Usernames are space seperated so, if you want two usernames to be able to SSH, the line would be:  “AllowUsers username1 username2” and so on.
  • Use SSH Keys (with no password authentication) – You may be using password authentication, which is fine, but passwords can be guessed.  By using SSH keys, unless the client connects and passes it’s private key information to the server, it won’t be allowed to authenticate.  This means that any username/password attempt will not even attempt to authenticate, which is obviously a good way to prevent random password attacks!
    Using SSH keys is a bit too indepth for this post, but check back soon where I will document this feature in more detail.

I’m not saying this will make your server invincible to these attacks; it won’t (well, SSH keys will probably stop password attacks if setup correctly!), but it’ll certainly help against the casual probe – if you allow a user access, who has a simple password, and a bot does probe a non-standard port and gets lucky, your machine will be compromised, but it certainly makes it a little bit more difficult for the automated, “lets get lucky” processes out there.

Look out for another article on droptips soon, where I will show how you can rate-limit attempts using iptables on Linux -this means, that if you do keep SSH on port 22, and you get, say, more than 5 new SSH connections in a minute from the same host, you can block the IP it’s coming from automatically, as it’s probably not up to much good!

Please let me know if you have similar tips with securing SSH access.  What do you do?  Find me on twitter @dazuk or by using comments below!

Taking a Screenshot in Linux and BSD

Screenshots; everyone loves them – whether it’s to show off your new shiny desktop, or just to record something on the screen at that moment in time, we’ve all taken lots!

I guess at the moment a lot of screenshots are being taken with the GNOME tool, but what if you don’t use GNOME?  What if you use one of the many other desktop environments, or maybe just a window manager on its own such as Fluxbox?

This is where scrot comes in – scrot is a command line based screen capsture tool based on the imlib2 library.

Usage is simple – open a terminal window:

scrot filename.png

… and it’s done.  Of course, there are a wide range of other options as mentioned in the manpage:  http://manpages.ubuntu.com/manpages/jaunty/en/man1/scrot.1.html

scrot is available from within Ubuntu and Debian (apt-get install scrot) and probably your choice of distribution.  It’s also available within BSD (It’s  under /ports/graphics/scrot on FreeBSD for example).  You can get the source from here:  http://linuxbrit.co.uk/scrot/

End all processes belonging to a user (Linux)

You may, at some point, need a quick way to end all processes for a specific username, instead of listing them all and going through each one.  Sure, you could write a small script to do this, and that’s what the developer of slay has done.

slay is a shell script which, when given a username as an argument, ends all processes belonging to that user – simple.

Of course, if you slay the username you are currently logged on as, you’ll get logged off, but it does work. Likewise, if you slay another username, they will be logged off and whatever they were doing ended.

I’ve used slay many times to end processes which have started on my own user account in the background, and I couldn’t be bothered to do it manully.

Of course, to end another users’ processes, you will need to be root.

slay is a already packaged in Ubuntu and Debian (apt-get install slay), and it’s probably packaged in your distribution too, so take a look.  Be careful though, because it’s quite unforgiving – it just does it, and people may have important processes running!

You can read the slay manpage here:  http://manpages.ubuntu.com/manpages/jaunty/man1/slay.1.html

How to install telnet in Windows Vista

If like me you use telnet a lot, you’ll have been surprised when the command wasn’t available to you within Windows Vista. This is because Microsoft have taken telnet out of the default Windows Vista installation, and made it a feature which you have to manually install.

Installing telnet within Vista is simple, though, and only takes a few minutes:

Start by opening Control Panel and then go to Programs and Features

Then once open, down the left-hand side, you’ll see an option Turn Windows Features on or off – go into that.

Then scroll down in the list and check the box next to “Telnet Client” and click OK.  Windows will then install telnet, and you’ll be able to run it from a command prompt as normal.

Install telnet in Windows Vista
Install telnet in Windows Vista

You could, of course, download PuTTY and use that instead of the Windows version of telnet, and benefit from a wide range of additional features.

Todo/Task Management and Organiser for GNOME

Everyone has a long list of tasks to do of a day – and everyone always forgets something; we just can’t remember everything.

This is where Getting Things GNOME! (gtg for short) comes in – an application for the GNOME desktop environment, that focuses on being easy to use, but powerful and flexible at the same time.

GTG is still a new application, and in it’s early development stages, but it’s showing some good features and a solid base for the developers to build upon.

If you do use GNOME on your desktop, then Getting Things GNOME! is well worth a look. Remember to help the developers by submitting bug reports as you go.

More information is available here:  http://gtg.fritalk.com/

Mount an ISO Image Under Windows 98/2000/XP/Vista

Following on from my post about how to mount an ISO image under Linux, this article will focus on doing the same thing from within Windows.

From Mount an ISO Image Under Linux:

“You may have many ISO images lying around of various CDs or DVDs. ISO images are an exact copy of an optical disk, and are increasingly popular – with high speed internet being available to many, people are taking advantage of this and instead of spending money on discs and shipping costs, simply making them available online for download.

The advantage of ISO images are that you can either choose to burn your own disc from the image, or, if you prefer, simply mount the image and use it as normal – again, saving a disc!”

So, how can you do this in Windows?  By using an application called Virtual CloneDrive from SlySoft.
Virtual CloneDrive is a free piece of software, that creates a virtual CD/DVD drive, which is accessible from (My) Computer.  You will also have the application (which is presented as a system tray icon on the task bar by default) where you can select which image you want mounting to that virtual drive.

Virtual CloneDrive works on Windows 98, 98SE, ME, 2000, XP, XP x64, Vista, and Vista x64, so you’re pretty much covered!

For more information and to download the application, visit the SlySoft site: http://www.slysoft.com/en/virtual-clonedrive.html

[Update: I’ve tested this tool on Windows 7, and it works fine!]