Category Archives: MacOSX

MacOSX command line tricks

Here's a list of MacOSX commands I had to search for all over the internet because I needed to use them lately and I'm sure I'm going to forget since I'm not a big OSX user. So here they are for when I'll need them again ... 🙂

In linux when you want to know which ports are opened and what applications listen on those ports you use netstat -lnp
In MacOSX you get the listening ports with this:

  1.  

Want to see what system call am application is making use strace, for MacOSX thats:

  1.  

Here's how to install an application that comes packaged in a .dmg:

  1.  

uninstall a package

  1.  

pidof required by some mysql scripts

  1.  

remount with noatime

  1.  

More to come ...

vim arrows in MacOSX

I know vim gurus would criticize me for using arrows in vim's insert mode but it's really hard to give them up.

I have this problem when I connect from my linux box to a MacOSX or FreeBSD box over ssh. I find it one of the most annoying things when using vim. When you are in insert mode and hit one of the arrows to move around, instead of the expected action vim will just print A, B, C or D on a new line. This makes vim practically useless.

So either you are very careful and always exit the insert mode before you move or fix the keys.

I think it's hard to always remember to get out of insert mode and it's one extra operation you have to do that I find useless not to mention you will probably have to enter insert again a few seconds after that.

So here's the fix for the arrow keys.  Edit vimrc either the global vimrc ( I'm using vim from macports so my vimrc is /opt/local/share/vim/vimrc ) or ~/.vimrc like this:

$ vim ~/.vimrc
set t_ku= (now type Ctrl-V and press cursor up)
set t_kd= (now type Ctrl-V and press cursor down)
set t_kr= (now type Ctrl-V and press cursor right)
set t_kl= (now type Ctrl-V and press cursor left)

This solution was stolen from vim tips wiki. I posted it here to avoid looking for it again if I need it. It's the second time I am hit by this problem and every time I had to search through a few pages with solutions that didn't work for me

MacOSX: Automount external drives at boot

After spending about an hour trying to figure out why I can't mount disks from /etc/fstab in MacOSX 10.4 I finally figured it out.

MacOSX 10.4 ( and maybe lower ) doesn't use fstab anymore. Well not just fstab. The stupidest thing about this is that fstab is referenced in every man page related to mount but nowhere it says it does NOT work anymore. I guess apple didn't want to invest in updating the man pages, no one is reading them anyway, everyone is using the gui tool, etc...

MacOSX holds all this information about mounts, users, groups, passwords, services, protocols, hosts and other stuff ( that uses to be in plain text files on Unix ) in it's NetInfo database. The good news is that you can import the information from the Unix standard format plain text files into the NetInfo database and you have an easy way to do this over the command line.

To mount one or more external disks at boot you will simply import the fstab entries just by piping them to the niload command:

echo "echo "/dev/disk1s10 none hfs rw,auto"  > /etc/fstab  	
cat /etc/fstab | niload -m fstab /   	# load fstab in netinfo

At the next boot /dev/disk1s10 will be mounted to /Volumes/disk1s10's label.
The weird thing about this is that you can't just remove /etc/fstab after you import in NetInfo. If you do this will not work anymore. If you just put the info in fstab but not inport it with niload it will not work. You need to both have a valid /etc/fstab and import it in order for this to work.

change hostname on MacOSX

here's a quick tip about how you can change the hostname on your MacOSX and keep it changed after a reboot:

  1.  

Of course you have to type that as root.

It seems there is also possible to set in it /etc/hostconfig but that file on it's own will not work. You have to load it in netinfo somehow but I don't know how to do that.

I think I like the /etc/hostconfig approach more then the scutil (even if it has to be loaded in netinfo ) because it's somehow closer to how most unix/linux systems do it.
Anyone knows how this can be done ?

Permissions on HFS+ volumes

This post will show you how to enable or disable permissions on a HFS+ volume and also how to check if a volume had the permissions enabled or not.

By default when you create a HFS+ filesystem it will have the permissions/owners disabled. I'm using a macmini for backing up files from a linux system and if I want the backup to be fully consistent with the source I need filesystem permissions.

Another problem is that using rsync between a permission based and a non permission based filesystem is useless because all files will always be out of sync.

Check permissions

  1.  

it will "Permissions on '/Volumes/YourVolume' are enabled ( or disabled ) of just "No entry found for '/Volumes/YourVolume'" which means the permissions are disabled.

Enable Permissions

  1.  

Disable Permissions

Since permissions are disabled by default I don't see many wanting to disable permissions but here's how you can do it anyway:

  1.  

I guess this is really useful only when you want to distribute a dmg image which currently has permissions enabled.
Distributing images with permissions enabled doesn't make much sense because the user that will open the image may have a different id then the user id that owns the files in the image so he may not be able to access the files inside the image.

How to create a dmg image

In a previous post I showed you how to mount a .dmg image from the command line.

Have you ever wondered how you can create such an image? Continue reading if you want to know.

Let's assume you want to create an image with a size of 10Mb that is formatted with a case sensitive HFS+ filesystem, with a Volume name Test stored in a file with the name Test.dmg.
Here is the command line you have to type in the shell:

  1.  

If you want to put files on it you have to attach it first:

  1.  

Or you could just add -attach to the list of parameters of the hdiutil command and it will also attach it after it creates it:

  1.  

Now you can copy your files to /Volumes/Test and when you're done you can detach the image in case you want to transfer/copy/move it:

  1.  

If you want a quick way to create an image from the contents of a folder you can use something like this:

  1.  

With that you can also specify a format for the image like compressed or read-only images.
Note that in this case I have not used that -size specifier anymore because the image will be as large as the contents of the source folder.

hdutil create --help for more details about image formats encryption and other goodies 🙂

MacOSX: adduser

I really needed an adduser like program on MacOSX  and after a few searches on the internet I found a script that works like the linux adduser program.

I remember I had to modify it to use bash ( my preferred shell ) as the default shell and I make it use the right path when copying the user template. I can't remember exactly where I found it so if you have seen this someplace else feel free to let me know and I'll be happy to give credit.

Here it is :

  1.  

Connect to MacOSX over vnc

One of the first unpleasant surprises I had when I first tried MacOSX was that I couldn't connect to it using standard vnc clients available for linux like realvnc or tinyvnc. I was actually able to connect but after the authentication the client would just hang.  I guess the protocol used by apple is a bit different then the protocol used by the rest of the vnc clients.

here are your options:

  1. use another Mac with a vnc client like  the  non free Timbuktu  or the open source Chicken of the VNC.
  2. if you have ssh access to the mac where you want to connect over vnc you can set up Vine server ( also known as OSXvnc ) . Vine is a vnc server that is compatible with realvnc client running on  linux or windows machines.

I chose the second option when possible because I don't want to fire up the macmini every time I needed to connect to a remote machine .

The vine  server comes with a nice GUI and can coexist with the standard MacOSX  vnc server listening on the next available port ( 5901 ) or you can just stop the standard server if you don't need it.

Of course if the system is really remote, you don't have another mac at hand and this is the first time you access it you wouldn't be able to use the GUI so here is how to set start vine form the command line:

  1. connect to the system where you want to install vine over ssh as root or su  root after you have connected as a normal user
  2. download  Vine3.0.dmg  in your home folder or  scp it there .
  3. attach the image:  hdiutil attach Vine3.0.dmg    -  This will make it's contents available in /Volumes/Vine3.0
  4. before you start the server you have to create a password for authentication:
    /Volumes/Vine3.0/Vine\ Server.app/storepasswd    <yourpasswordhere> password_file
  5. start the server:  /Volumes/Vine3.0/Vine\ Server.app/OSXvnc-server  -rfbauth password_file
    The server will output some debuggind information and the last line should be something like:
    Started Listener Thread on port 5901
    The port can be 5900 if the standard vnc server is not already listenting on 5900
  6. leave the ssh session open and use something like realvnc vncviewer  to connect to your server on port 5901 :
    vncviewer mac.machine.name:5901

The only disadvantage of this method is that it requires a bit of work in order to be able to connect but this can be avoided by setting the Vine server to start on boot and that will be explained in another post 🙂

Mount .dmg images from command line

DMG images are the way that a lot of MacOSX binary packages are distributed.  They are somehow like virtual hard disk images or .iso images that you can just mount with a simple double click if you have access to the gui.

But how do you do that from the command line?

quick answer:

  1.  

and the image should now be mounted somewhere in /Volumes

The hdiutil is a tool used to manipulate such disk images. It can do a lot more then just mount the images. It can even burn  them to an optical media, convert them to an iso  and some other formats , even compress them using gzip or bzip2 .

MacOSX case sensitivity

I just got the biggest slap from MacOSX. Yes, I was punished for my ignorance.  I had not idea that the HFS filesystem defaults to case insensitive. I thought it would be case sensitive like in Unix but no.

When I first saw it I didn't believe it. The first clue was when I ran mysql_install_db an I got some warnings about about the filesystem being case insensitive. I didn't believe it because I knew I was able to create files with lower and upper cases so I just ignored it.

The confusion comes from the fact that the HFS+ filesystem is  MyFile.txt would be the same as myfile.txt .  The filesystem preserves cases so it will remember the original file name and a ls will show that but you would be able to access the file with different names. At the first glance this seems like a neat feature but it can actually create big problems when you're interacting with files from case sensitive filesystems.

The slap came from using rsync to backup files from a Linux machine to a macmini machine.   Just imagine a directory with many files split over directories from a to z and A to Z on Linux, something like files/a , files/A, files/b, files/B .

Now rsync -avz -e ssh  files/  backup@macmini:~/files/   and you'll end up with a folder named files on macmini but it will only have the upper case subfolders. The files in the lower case subfolders would be put in the upper case subfolders. This if really bad, it can really break consistency and mess up the data ( imagine different files with the same name one in A and the other in a as just one of the possibilities, there are other like this)

So now, what ? Reformat and change the filesystem to be case sensitive and journaled for easier recovery.
Be careful the next operation will remove all data on your disk:

  1.  

YourDiskId is the disk that you want to erase.It can be disk1, disk2,etc... Look for it using diskutil list