Archiv der Kategorie: Linux

Artikelnavigation

There are dozens of nice music players around, that’s for sure. You can choose between featur rich killer applications like Rhythmbox or Amarok, use old-school but up to date standards like XMMS or even a console classic like mp3blaster. Most of the standard players have in common, that the interface and the player itself are the same. That’s normal and therefore everything is fine. But maybe you are in a situation where you prefer playing music on a remote machine, only have a console or just don’t want your music player to stay open while playing. Huh?

Okay, again: Imagine, you set a machine dedicated for playing music. You put it into a corner, it has no keyboard, just a network interface. How do you proceed? Ah, you install mp3blaster via ssh and let mp3blaster run in the console, right? That’s fine and mp3blaster really kicks ass. But… With mp3blaster you are forced to have a shell account on the machine, just for playing music. Mp3blaster has a lot of features, but it has only one interface and you have to stick to it – and at least you also have to get into the “screen” business for letting the player run while not being logged in.

So what’s the alternative? Well: Imagine a music player daemon, that keeps your music collection and listens for clients telling him, what to play next. You don’t even need to imagine the music player daemon, just have a look at the “Music Player Daemon” – abbreviated “mpd”. It’s idea is to completely split user interface and player daemon, enabling you to choose between different user interfaces (from console to gui) and to manage a remote daemon without the need of logging into the machine directly.

Now let’s test it, right away. First, we install “mpd” by calling “aptitude install mpd”. This will enroll the daemon and start it right away. “mpd” organises its files in “/var/lib/mpd” and expects music by default in “/var/lib/mpd/music”. Strange location, isn’t it? While you are free to configure the mpd daemon in /etc/mpd.conf, the easiest way of getting you music into the daemon is to symlink your directory/directories. I keep my music collection in /home/ccm/Music. Therefore I run “sudo ln -s /home/ccm/Music /var/lib/mpd/music/ccm”. Now you have to take a short break and think about access rights: While mpd runs as user “mpd”, which is a good idea, you need to make sure, that your music files are accessible to mpd. I ran into trouble as my mp3 files provided read access only the owner (me). Therefore I needed to decided whether to run mpd as “ccm”, chown my music to “mpd” or to extend access rights. I decided for last version and just chmod’ed the music files and directories (see below). Now you need to tell mpd that there is new music around. Just run “sudo mpd –create-db” and it should crawl your library quite fast. So until here we took the following steps:

Now let’s listen to some music! But stop: We need a client! Until now we only installed the daemon. An “apt-cache search mpd client” gives you a brief overview over possible clients. I’d suggest you install “mpc” (the classic command line tool), “ncmpc” (an enhanced menu driven client), ario (a gtk client) for now, to see differences. Therefore run “sudo aptitude install mpc ncmpc ario”. Give ario a try if you prefer a gui. Like all clients, ario tries to connect to a mpd instance on the localhost by default. You should already see your music like this:

The mpd daemon expects a playlist from you. This is party on the lower side of the interface. Feel free to drag and drop the name of a band/musician, an album or even just a single track into this area and hit play. Hey, mpd play music! But that’s actually like the situation before…

A first console approach: ncmpc

For getting the difference, close the player by clicking on the X on the upper right side. Ario will totally disappear (feel free to check with “ps aux | grep ario”, but the music will continue to play. Nice. Now let’s get it even more abstract and open a console and start “ncmpc”. In this player you can switch to the different “tabs” by hitting a key from 1 to 5. “2″ is the current playlist, “3″ a file browser. You can hit ENTER on a file in the playlist, and it will play, while DELETE will remove it from the playlist. In the browser, you hit SPACE on a directory and it will be added to the file list or ENTER on a single track. This way you can build a huge playlist very fast.

Let your command line completion work: mpc

With Ario and ncmpc you can already choose between a gui and console interface for controlling your music player. As I really like working on the console, I was happy to see, that the TAB completion from Bash and ZSH support the “mpc” command, which is the standard console interface. With “mpc” you will normale use one of the following commands:

# add a directory to the playlist
$ mpc add directory
# add a single track to the playlist
$ mpc add file
# play a track by number from the playlist
$ mpc play NUMBER
# show current playlist
$ mpc playlist
# update the current database as new files have been added
$ mpc update
# list all availabe music
$ mpc listall

If you are a ZSH user like I am, you will be pleased by the heavy TAB completion support. All commands and file names can be completed which actually makes you faster than bothering with any gui. The Bash support is okay, but not that sophisticated. If you don’t know, what I am talking about: never mind. You will, one day

I think this is enough for a first insight into “mpd” and its clients. If you like the idea, you should read about connecting to remote mpd instances, so you are able to use your local client for a daemon running on a different machine, enabling authentication in mpd or maybe think about running mpd just on your local machine as it is a nice way of keeping your music collecting while being able to choose a client depending on the situation you are in. The mpd wiki is a great place to start gathering further information. You’ll find that a lot of utilities are available – from pidgin plugin for showing the music you are listening to a gnome panel applet. Always have in mind, how strong you are while being on the console as you can even use pipes when using mpc… Have fun.

You know this? Somebody just sent you a mail with attachments that don’t have usable file extensions so you don’t really know how to handle them. Audio file? PDF? What is it? The same problem might occur after a file recovery, on web pages with upload features or just when you are really and time pressure and have time for messing around with file type guessing.

While you can try to give the file an extension and open it with a software you think might be suitable, the more sophisticated way is to let your computer find out what is all about. As a GNU/Linux user you probably already think “There is surely a command line tool for this”. Of course there is: The package “file“, that often gets automatically installed by dependencies or just an “aptitude install file” will help you out.

“file” depends on “libmagic” which provides patterns for the so called “magic number” detection. You don’t have to know, what that is, but if you want, see this Wikipedia article for reference. So all you have to know, is how to handle the file command. And actually there is not much to learn. Let’s assume we have the following directory with unknown files:

Now we want to know what’s inside those black boxes. Therefore we just call “file *” on the console:

Hey, that’s all. Pretty impressive, isn’t it? “file” does even not only differs binary from text files, it even tries to guess what programming language a text file is written in. And the magic is not that much magic: In case of the zsh file it just sees a shebang pointing to the zsh in the first line of the file, a PDF file typically starts with “%PDF” and so on. It’s all about patterns.

“file” provides you with some command line options that make it’s usage even more helpful. The most interesting is “-i” as it prints out mime types instead of verbose file types. If you are a web developer and want to know the exact mime type for a file download, this can save you a lot of time:

Great, isn’t it? The Apache webserver also uses libmagic for this purpose. With “file” you just use a wrapper for the same task.

That’s all about “file” for today. Happy file detection – and feel free to report back.

Always wanted to learn using a shell more deeply? Maybe “fish“, the “friendly interactive shell” is the right kickoff for you.

If you are already a heavy command line user with customized .bashrc or even .zshrc (like me), thank you probably don’t need another shell. But if this shell thingy is somehow a miracle to you but you saw people using it like wizards with colorful commands and a typing speed that made you jealous then it could help you to start with a shell that concentrates on being very friendly to new users as common shells like Bash and ZSH expect you to read the manual and write a config file (there are aids and defaults that vary from distribution to distribution).

The standard shell for login users in Ubuntu/Debian is “Bash”. Ubuntu already ships the file /etc/bash_completion that is read by default and helps users using the TAB key more exensively. Try it on you bash shell: just type something like “ls –” and press TAB twice. You’ll see a list of options that “ls” provides. Nice but it could be nicer. Let’s compare this to fish. Install fish by using Synaptic or “aptitude install fish”, open a terminal and start the shell by typing “fish”. You should a changed green prompt. Now type “ls -” and press TAB.

Stop: Already while typing you should see a strange color change. When entering “l” the character turns red and underlined. Looks like an error? Well, it is: fish tells you, that “l” is probably not a command. An aid during typing before running a command. Neat. Now, when pressing TAB you should a very clean list of options for “ls” with a short description of each option:

Helpfull, isn’t it? Of course this is not limited to ls. Try it with other commands you are using. If you ask yourself why you have to type “command –” and press TAB: “–” introduces a command line option (“-” does this also – try it!). As you press TAB after this, the shells knows “the user wants to do something and needs help on completing it”. It looks after a pattern and sees that you want to use the given command and are looking for options. That’s all. As I said: This works in Bash often by default also, but not that nice.

Now fish can do more with completion of course. Want to install a program? Try “aptitude install mut” and press TAB. It will show you a list of packages matching that pattern:

Need to kill a process? Type “kill ” and press TAB and you will get a nice list of running processes:

The list of possible TAB completions on fish is endless. Just notice that emphasis has been put on commands like mount, make, su, ssh, apt-get/aptitude. In most commands usernames, process ids will automatically be completed. The trick is just to try TAB when you are too lazy to type or unsure how to proceed. A good shell surprises you from time to time with it’s completion.

Also very helpful is the extended pattern matching for file names. Let’s say you want a list of all pdf files in a directory and all it’s subdirectories. On bash you probably use something like “find . -name “*.mp3″. On fish you use the pattern “**” which means any files and directories in the current directory and all of its subdirectories. So type “ls **.pdf” and you get the list you want as fish crawls through the directories for you. Want alle .mp3 and mp4 files but not files like .mpeg? Use “ls **.mp?” as “?” stands for one character. Of course commands like “rm **.bak” are possible, too. Use them with care! In the following example we are looking for pdf files in all subdirectorie, delete them and afterwards make sure they are really gone:

So let me stop here. I hope, I was able to show you that using fish instead of an unconfigured shell is a nice way of getting in the command line business. Fish provides you with a lot of more features that you might need and saves you from writing a config file from scratch.

If you want to give fish a try: Install it and run the “help” command. I will launch a nice help page in you browser. Read some parts of the document as they’ll show you nice gimmicks. Or just don’t and start right away. But trust me: Reading hints for a shell from time to time will save you … time.

(Just in case you don’t know: You can change your standard shell by using the “chsh” command. But when being a novice it is always a good idea to stick to the distribution specific default shell and run your shell directly by calling it. When you are more used to it feel free to make it your standard shell…)

Let me introduce you today into a tool that a lot of people might evaluate as useless: Jeremy Stanley’s weather-util. Whith this tiny python script, which finally found its way into Debian Etch and Ubuntu repositories, you can retrieve weather information from weather stations worldwide directly from the command line.

After installing it by running “aptitude install weather-util” or synaptec, call “weather”:

Pretty impressive, isn’t it? Weather just makes an http call to a weather server for a preset station (where the heck is Raleigh-Durham International Airport?) and returns the current weather information. Of course you can also retrieve the forecast for the next days by running “weather -f”:

Sadfully the forecast only displays Fahrenheit, but that way we have enough space for patching the package

Retrieving local weather information

Now we are, of course, we are interested in the weather in our area. The easiest way is getting the ID for a weather station. Just go to http://weather.noaa.gov/ and choose your country/city/station by using the drop down menus for US and international stations. When you found a station close to your point of interest you can see a four letter id in round brackets. See the example above – the airport has KRDU. I am using EDDI most of the times which is Berlin Tempelhof – an airport in the city center of Berlin.

So you are ready to ask politely for weather again by giving the id with “weather –id=ID”, in my case “–id=EDDI”. (note: you can also make it short with “-iEDDI”:

for reference. Just text files on a web server regularly updated. Click around and go to there parent dir – you’ll find even more interesting information. So using weather-util without weather-util should be not a big deal.

Screen integration

Now for the console lovers: You are using screen with a pimped status bar, don’t you? And in your wildest dreams you imagined the status bar showing the weather report, so you even don’t have to look outside the window because as a console guy you don’t even like your real “window”? No problem anymore by using screens backticks and weather-util.

As I noticed that weather-util runs into trouble from time to time when not being able to send it’s http request, I decided for a indirect weather pull by writing the information I need to a flat file by a cronjob. We just call weather-util and use awk to grab the snippet we need. I am interested in the temperature in Celsius. weather-util shows this line:

Temperature: 62 F (17 C)

So I use the following very quick and very dirty awk to get the “17″ out:

Feel free to brush this up and report back. I am sure you can improve to use only one awk call instead of two.

You save this line to a shell script that is scheduled to run every five minutes and direct it via “>” to write it’s output to a flat txt file. Within you .screenrc you read this file and display the contents in you status bar.
~/.screenrc:

Let me tell you something about “less”: You are probably underrating it for no reason. Of course you know “less” is always there and it does it’s job – showing files while being able to scroll backward – and some even use it instead of “tail”. But, hey, let’s examine some of the command line options to get more out of less:

-M: this option extends the prompt on the bottom. By default less in most cases just shows the name of the file it is showing, with “-M” turned on, it also shows how many lines the files has, which lines it is currently showing and how far (in percent) you have gone. No killer feature, but nice to have.

-i: this option causes searches to ignore cases. A search for “pattern” therefore also finds “PaTTerN”. You like this, don’t you? You like this even more, as this search still enables you to switch case sensitive search on by searching for a pattern containing at least one uppercase letter. A search for “Pattern” for instance would still be case sensitive. If you even want to prevent this, you could use “-I” which totally ignores cases.

-r: Sometimes getting warnings about binary characters? With “-r” you tell less to display raw characters. This can help you when displaying files containing color codes. It is said that log files from Rails contain these types of code.

-c: Just a gimmick to redraw the screen more clearly by beginning from the top line instead of scrolling. This might result in a slightly increased data transfer rate when using ssh but can improve usability.

-a: This causes less to skip found search patterns when pressing “n” not from item to item but from page to page. You might know the pain when searching for a pattern that comes up more than once on a page and you start hammering “n” getting confused on what you have already seen and what not. This options just skips at least the current page before displaying the next found pattern while still marking all patterns of course.

-f: This can help you in conjunction with “-r” to force the display of raw characters without being questioned again.

Confused about the sequence of the options? Don’t be:

$ less -Mircaf

is something you just learn or create an alias for.

This one, to sum up the options, will display an extended prompt, ignores cases in searches, while being able to switch them on, skip found search patterns at least per page, display raw characters like color codes without asking and redraws the screen as good as possible.

Another feature that should be mentioned is the “follow file” mode that some of you might know. It is similar to tail as it shows you the content of a file that gets appended while viewing. You turn this mode on by pressing “F” (uppercase F). The advantage over tail is that you can interrupt the mode by pressing ctrl-c and scroll back though still being able to return to follow by pressing “F”.

Not so familiar is the fact that you also can jump into the follow mode from the command line:

$ less +F

starts the follow mode immediatly. Of course typing “+F” on the command line is not sophisticated as typing “tail” but you can create an alias for it like “ltail” or whatever you like.

As a summary:

$ alias eless="less -Mircaf"
$ alias ltail="less +F"

gives you two new commands. “eless” as an extended less provides you with the described features. “ltail” simulates “tail” but enables you to jump back to the normal less by pressing ctrl-c.

Instead of creating an alias for “less -Mircaf” you could also use the environment variable “LESS”:

Please note: As less is still being developped, command line options might slightly change. For instance in newer version “-R” instead of “-rf” might lead to the same result. Just try it or check the version of less you are running (“less –version”) against the official less changelog.

Maybe you are in the nice situation of running machine with multiple cpu cores having to crunch a lot of numbert or providing network daemons. Multiple cpu cores can boost your performance dramatically but there are, of course, possible issues. On is the fact, that interrupts by default are mainly called by the first cpu. As applications that aren’t able to thread correctly can stick to this cpu you might notice by a “cat /proc/interrupts” that something goes wrong. The following is a (compressed) /proc/interrupts from a live server running for about ten weeks:

You can see, that actually all interrupts are called by CPU0. We want to brush this up! How? Just run a “aptitude install irqbalance” as irqbalance promises:

Daemon to balance interrupts across multiple CPUs, which can lead to better performance and IO balance on SMP systems. This package is especially useful on systems with multi-core processors, as interrupts will typically only be serviced by the first core.

So let’s check after about one week by another “cat /proc/interrupts”:

Nice, isn’t it? CPU1 started to grab interrupts also. If we would reboot the server, most of the irqs would look balanced over time. (most, not all)

Please notice:

Before installing irqbalance, check your /proc/interrupts. It might be possible, that you don’t need it though you have multiple cores as there is a value “CONFIG_IRQBALANCE” in 2.6 kernels that can be turned on.

[update]

The comments (thank you!) pointed out the following:

There are reports on crashed systems using irqbalance. (Though I have never seen anyone by myself)

Note that irqbalance is not in main – if you are using it on an important server.

CONFIG_IRQBALANCE seems to be enabled in Bbuntu Hardy by default.

There are discussions about removing CONFIG_IRQBALANCE as it is said that irqbalance is more reliable.

So it is up to you to decice which one to use!

[update2]

Actually a glance on /boot/config-2.6.24-17-generic shows, that CONFIG_IRQBALANCE seems not to be enabled in Hardy though the balancing seems to work. Actually I am not one of the kernel guys so my investigation will take it’s time. Any hints welcome (thank you lissyx).

Ubuntu Berlin strikes again! On the 31th of May, this Saturday (and last day of the “Linuxtag”), Ubuntu Berlin is proud to present the “Ubuntu BBQ” – an event you should not miss when staying in Berlin for attending “Linuxtag 2008“, live here or happen to be around by chance.

Hosted again by the sunken starship “c-base” we are happy to provide you with drinks at low prices, and BBQ and entrance for … free! Sponsorships from Canonical Ltd. (you might have heard of them) and ubuntu Deutschland e.V. (thank you!) and an invitation from the Linuxtag Community staff made this possible and we are anxious to see how many Linuxtag attendees make their way to the base.

But it’s not about eating and drinking: The event gives you the possibility to meet a lot of GNU/Linux and Ubuntu interested folks, even some of the well known free software gurus in a relaxed atmosphere. Of course you can use a free wifi network with you notebook/gadget/whatever, but don’t forget: It’s a party. Music will be around and you can sit directly at the rivercoast of the “Spree”. It’s said the weather will be great.

You are invited the join us starting from 4 pm – the BBQ will start from 7pm – so you have enough time to come over from Linuxtag. We will arrange some groups you can join on Linuxtag making it easier for you to find the c-base. See more on our (yet only German) announcement.

What a pain. Imagine you are in Windows network environment and have a small amount of Ubuntu desktops. You task is to let them join the Active Directory so users can login with their known credentials. There is a package in universe called “authtool” even providing and promising to do what you need. Sadfully it is quite broken in it’s current status and if you ask me one should even consider removing it until it does at least not break you boot (don’t ask for details) and has a good set of working dependencies. There are other methods as ldap-binding but in my eyes there are either not stable or just too complicated to configure (and therefore hardly qualified for convincing people).

It is planned to package the open source version of Likewise called “Likewise Open” for Ubuntu Hardy. Likewise Open enables you to join an Active Directory with actually some simple clicks or one console command. There is already an updated source tarball which can be installed quite easily:

If you have all necessary dependencies resolved the make process should provide you with .deb files which you should install. As Jerry states there is currently one blocker which can be worked by not using the gui but calling a line like this

1

$ sudo domainjoin-cli join AD_REALM ADMIN_ACCOUNT

Afterwards you should be able to login like this “realm\username”. I tried the process on Gutsy and it worked quite well. I had to reboot once as my gdm hang – maybe it’s better to call the command directly from a “real” console. So what is missing? Check the comparison of Likewise Open and Likewise Enterprise, the commercial version of Likewise. The thing you might miss at first is:

Do more during logon: Create a home directory, copy template files, set permissions, run scripts, deliver messages, and more.

This means that Likewise Open enables you to login as AD user, creates his home under /local/AD_REALM/USER but you have be smart and hack around a bit to get things working like managing sudo, running scripts and so on. But nonetheless Likewise Open seems to be a promising approach for solving the problem of Ubuntu-Windows-network integration and I am sure to see some nice addons from the community in the future.

Please note: Installing software that changes login procedures is a deep intervention into Linux core procedures. So please: Do this with a test environment before considering it for production purposes.

Just a small and old snippet that might be helpful or an example: Some years ago I’s in need of getting to know early about new released Linux kernel versions. Therefore I wrote a (not sophisticated but working) crontabbed script checking the kernel page for a new stable Linux kernel and alerting me via mail if a new version is found with link to the changelog:

The only real bug in this script is that it does not detect network issues and therefore alerts you when it is not able to get a http response. But this could be fixed with one or two lines of code. And yes most lines could be more elegant Probably today there are better channels like rss or even an old mailing list with announcements that I never looked for, but this snippet does it’s job very well.

update:

Fixed the broken wrapping of the script. Sorry about this. (Thank you Jeremy.)

Jonne stated that of course using a feed like http://kernel.org/kdist/rss.xml is the better choice today. He is surely right about this though sometimes receiving a mail is a need.