RSS

Category Archives: Unix & Linux

The difference between hard and soft links

When I clean up the tech documents these days, I found many useful materials that I seldom read. And I think it’s a better way to post here and go over again.

Unix files consist of two parts: the data part and the filename part.

The data part is associated with something called an ‘inode’. The inode
carries the map of where the data is, the file permissions, etc. for the
data.

                               .---------------> ! data ! ! data ! etc
/ +------+ !------+
! permbits, etc ! data addresses !
+------------inode---------------+

The filename part carries a name and an associated inode number.

                         .--------------> ! permbits, etc ! addresses !
/ +---------inode-------------+
! filename ! inode # !
+--------------------+

More than one filename can reference the same inode number; these files are
said to be ‘hard linked’ together.

        ! filename ! inode # !
+--------------------+
\
>--------------> ! permbits, etc ! addresses !
/ +---------inode-------------+
! othername ! inode # !
+---------------------+

On the other hand, there’s a special file type whose data part carries
a path to another file. Since it is a special file, the OS recognizes the
data as a path, and redirects opens, reads, and writes so that, instead of
accessing the data within the special file, they access the data in the
file named by the data in the special file. This special file is
called a ‘soft link’ or a ‘symbolic link’ (aka a ‘symlink’).

        ! filename ! inode # !
+--------------------+
\
.-------> ! permbits, etc ! addresses !
+---------inode-------------+
/
/
/
.----------------------------------------------'
(
'--> !"/path/to/some/other/file"!
+---------data-------------+
/ }
.~ ~ ~ ~ ~ ~ ~ }-- (redirected at open() time)
( }
'~~> ! filename ! inode # !
+--------------------+
\
'------------> ! permbits, etc ! addresses !
+---------inode-------------+
/
/
.----------------------------------------------------'
(
'-> ! data ! ! data ! etc.
+------+ +------+

Now, the filename part of the file is stored in a special file of its own
along with the filename parts of other files; this special file is called a
directory. The directory, as a file, is just an array of filename parts of
other files.

When a directory is built, it is initially populated with the filename parts
of two special files: the ‘.’ and ‘..’ files. The filename part for the ‘.’
file is populated with the inode# of the directory file in which the entry
has been made; ‘.’ is a hardlink to the file that implements the current
directory.

The filename part for the ‘..’ file is populated with the inode# of the
directory file that contains the filename part of the current directory
file. ‘..’ is a hardlink to the file that implements the immediate parent
of the current directory.

The ‘ln’ command knows how to build hardlinks and softlinks; the
‘mkdir’ command knows how to build directories (the OS takes care of the
above hardlinks).

There are restrictions on what can be hardlinked (both links must reside on
the same filesystem, the source file must exist, etc.) that are not
applicable to softlinks (source and target can be on seperate file systems,
source does not have to exist, etc.). OTOH, softlinks have other
restrictions not shared by hardlinks (additional I/O necessary to complete
file access, additional storage taken up by softlink file’s data, etc.)

In other words, there’s tradeoffs with each.

Now, let’s demonstrate some of this…

ln in action

Let’s start off with an empty directory, and create a file in it

~/directory $ ls -lia 
total 3
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:16 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:16 ..

~/directory $ echo "This is a file" >basic.file

~/directory $ ls -lia
total 4
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:17 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:16 ..
73478 -rw-r--r-- 1 lpitcher users 15 Mar 11 20:17 basic.file

~/directory $ cat basic.file
This is a file

Now, let’s make a hardlink to the file

   
~/directory $ ln basic.file hardlink.file

~/directory $ ls -lia
total 5
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:20 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-r--r-- 2 lpitcher users 15 Mar 11 20:17 basic.file
73478 -rw-r--r-- 2 lpitcher users 15 Mar 11 20:17 hardlink.file

~/directory $ cat hardlink.file
This is a file

We see that:

  1. hardlink.file shares the same inode (73478) as basic.file
  2. hardlink.file shares the same data as basic.file

If we change the permissions on basic.file:

~/directory $ chmod a+w basic.file

~/directory $ ls -lia
total 5
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:20 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 basic.file
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 hardlink.file

then the same permissions change on hardlink.file.

The two files (basic.file and hardlink.file) share the same inode and data,
but have different file names.

Let’s now make a softlink to the original file:

~/directory $ ln -s basic.file softlink.file

~/directory $ ls -lia
total 5
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:24 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 basic.file
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 hardlink.file
73479 lrwxrwxrwx 1 lpitcher users 10 Mar 11 20:24 softlink.file -> basic.file

~/directory $ cat softlink.file
This is a file

Here, we see that although softlink.file accesses the same data as
basic.file and hardlink.file, it does not share the same inode (73479 vs
73478), nor does it exhibit the same file permissions. It does show a new
permission bit: the ‘l’ (softlink) bit.

If we delete basic.file:

~/directory $ rm basic.file

~/directory $ ls -lia
total 4
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:27 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-rw-rw- 1 lpitcher users 15 Mar 11 20:17 hardlink.file
73479 lrwxrwxrwx 1 lpitcher users 10 Mar 11 20:24 softlink.file -> basic.file

then we lose the ability to access the linked data through the softlink:

~/directory $ cat softlink.file
cat: softlink.file: No such file or directory

However, we still have access to the original data through the hardlink:

~/directory $ cat hardlink.file
This is a file

You will notice that when we deleted the original file, the hardlink didn’t
vanish. Similarly, if we had deleted the softlink, the original file wouldn’t
have vanished.

A further note with respect to hardlink files

When deleting files, the data part isn’t disposed of until all the filename
parts have been deleted. There’s a count in the inode that indicates how
many filenames point to this file, and that count is decremented by 1 each
time one of those filenames is deleted. When the count makes it to zero,
the inode and its associated data are deleted.

By the way, the count also reflects how many times the file has been opened
without being closed (in other words, how many references to the file are
still active). This has some ramifications which aren’t obvious at first:
you can delete a file so that no "filename" part points to the inode,
without releasing the space for the data part of the file, because the file
is still open.

Have you ever found yourself in this position: you notice that
/var/log/messages (or some other syslog-owned file) has grown too big, and
you

     rm /var/log/messages
touch /var/log/messages

to reclaim the space, but the used space doesn’t reappear? This is because,
although you’ve deleted the filename part, there’s a process that’s got the
data part open still (syslogd), and the OS won’t release the space for the
data until the process closes it. In order to complete your space
reclamation, you have to

     kill -SIGHUP `cat /var/run/syslogd.pid`

to get syslogd to close and reopen the file.

You can use this to your advantage in programs: have you ever wondered how
you could hide a temporary file? Well, you could do the following:

     {
FILE *fp;

fp = fopen("some.hidden.file","w");
unlink("some.hidden.file"); /* deletes the filename part */

/* some.hidden.file no longer has a filename and is truely hidden */
fprintf(fp,"This data won't be found\n"); /* access the data part */
/*etc*/
fclose(fp); /* finally release the data part */
}

 

 
Leave a comment

Posted by on May 25, 2007 in Unix & Linux

 

import private key from Putty to SecureCRT

As we know, SecureCRT can use public/private key pairs generated by OpenSSH, and puttygen could export to an OpenSSH key…
Hypothesis, we got public/private key pairs generates by puttygen: pub.ppk / pri.ppk
 
run puttygen and export private key (pri.ppk) )via the "Export OpenSSH key" in puttygen’s "Conversions" menu.
name the new private key "Identity", also name pub.ppk "Identity.pub"
 
run SecureCRT — Tools — Create Public Key…
no need to change the name "Identity", it is used to be covered
and make the keys be the default globel keys.
 
copy the keys Identity and Identity.pub (just exported from puttygen, the OpenSSH keys) to the folder which contiained SecureCRT’s private and public key ,( just generated by SecureCRT), and cover them.
 
MUST put private key and public key in the same folder.
 
 
 
Leave a comment

Posted by on November 9, 2006 in Unix & Linux

 

FreeBSD 6.1 安装后设置

刚安装完的FreeBSD,重新后,就会出现Login提示符,输入root,回车,熟悉的#号又出现在你的面前了(因为安装时没有设置密码)。
 
1,首先更改root的pd和添加用户
#passwd

#adduser

在选择group的时候输入“wheel”,以便远程SSH时可以用su执行root权限
 
========================================================================================
2,在命令行需要入sysinstall,是不是又到了我们熟悉的菜单了,选择Configure(里面的选项有:X Exit、Distributions、Packages、Root Password、Fdisk、Label、User Management、Console、Time Zone、Media、Mouse、Networking、Security、Startup、TTYs、Options、HTML Docs、Load KLD),进行FreeBSD的一些基本设置。
 
========================================================================================
 
3,设定时区(Time Zone):会询问你是否属于UTC时间(格林威治时间),对于绝大部分人来说,当然选NO了,既然不是UTC时间,那系统就会让你自己选择,选择大区域,当然是5亚洲Asia,接下来选择时间区了,9是中国,然后是选1北京时间east China了,回车,接下来系统再次询问,是否属于该时区,选(Yes)。
 
========================================================================================
 
4,鼠标的设定(Mouse) :一般选择“2 Enable”,系统会自动找到鼠标的。
 
========================================================================================
 
5,进入“Networking”–Interfaces (网卡设定) (开始网卡设定之前,系统会将现有的可能通讯接口列出。通常,你的网卡会是第一个位置,网卡的型号会因为网卡不同而有改变)。
  a.请选择该网卡(lnc0 Lance/PCnet (Isolan/Novell NE2100/NE32-VL) ethernet);
  b.是否采用IPv6 (选择“Yes”) ;
  c.是否采用DHCP(动态DNS) (依照个人需求,此例我选“Yes”,它会自动配置)
  d.然后分别输入Host(切记后面一定要多输入一个小数点,否则启动有问题,我一般都输入smart.com)、domain(可不填)、IPv4 Gateway、Name server、IPv4 Address、Netmask等;
  e.显示“Would you like to bring the lnc0 interface up right now?”时选“Yes”。
  选择“sshd”以便可以远程ssh进控制台管理FreeBSD,然后“X Exit”退出到“FreeBSD Configuration Menu”配置菜单。再“X Exit”到“sysinstall Main Menu”。“X Exit Install”。
 
========================================================================================

6,Xorg的配置
安装Xorg:
(1)通过FreeBSD光盘安装(推荐)
sysinstall — Configure — Distributions — X.Org(把Basic、Server和Fonts中的内容全选) — ports(最好能装上这个,因为很多东东用它很方便,如果不能从光盘上安装 ports则可改FTP作为安装介质)
从“Distributions”退回“Configuration”过程中会安装刚才选中的软件包
(2)编译安装
# cd /usr/ports/x11/xorg
# make install clean
(3)从Package安装
# pkg_add -r xorg
Install VMware Tools安装(若不做此步,直接设定桌面生成xorg.conf.new时会死)。
Ctrl+Alt回到VM界面
VM — Install VMware Tools –Install
回到FreeBSD
# mount /cdrom
# cp /cdrom/vmware-freebsd-tools.tar.gz /tmp
# umount /cdrom
# cd /tmp
# tar zxf vmware-freebsd-tools.tar.gz
# cd vmware-tools-distrib
# ./vmware-install.pl
选择一下你所要的分辨率,其他的一路回车就可以了(最后一步需要perl支持,但装了X.Org后就已经有了)。
配置Xorg
(1)在/root中生成xorg.conf.new
# shutdown -r +1
# Xorg -configure
先执行shutdown -r +1, 再执行Xorg -configure,等待一分钟后,系统会自动重启,免得黑屏后无法操作。
(2)测试配置文件,以确认Xorg能够和系统上的显卡正常工作
# Xorg -config xorg.conf.new
如果能显示黑灰的格子和"X"鼠标指针,说明配置成功,Ctrl+Alt+Backspace键退出测试界面。
如果配置不成功也没关系,可以vi编辑xorg.conf文件,具体可以section可以google之
 
========================================================================================
 
7,Gnome安装
sysinstall — Configure — Packages — CD/DVD — gnome — gnome2-2.12.3
按Tab键选OK再回车返回“Package Selection” 随便再把“linux”也装上
在“Package Selection”窗口按Tab键选“Install”开始安装软件包
从“Distributions”退回“Configuration”过程中会安装刚才选中的软件包,此时安装时间较长,最后回到“FreeBSD configuration Menu”,最后退出sysinstall。
% echo "/usr/X11R6/bin/gnome-session" > ~/.xinitrc
接下来输入 startx, GNOME 桌面环境就启动了。
 
Leave a comment

Posted by on September 7, 2006 in Unix & Linux

 

File Structure

File Types
Ordinary: Text or code data
Directory:
a table of contents, that stores a list of files within that directory.
special files
Represent hardware or loqical devices.
eg.CD-ROM-Device is represented bt /dev/cd0
=========================================
File Structure

/             rooot
./sbin        System utilities. sbin=system binary.
./dev         files represent devices
./etc         system configuration.

/usr          user? contain system programs.
/usr/bin      user’s binary. cmd such as ls, cat.
/home         contain user login directories and files
/var          files dynamically change
/tmp          hold files that are temporarily needed or created by app and program.

/opt          optional. contain Linux command. such as gzip, tar.
/proc         supported with AIX 5L. pseudo file system processes and kernel data structures to corresponding files.
===========================================
cmd:
pwd: print working directory
ls: list
mkdir: make directory.
mkdir -m //special with a perticular set of permissions.
mkdir -p // create multiple directory.
rmdir: remove directory, & directory must be empty.
istat: display i-noe stat.
touch: update access & modification times, or create zero-length files.

 
Leave a comment

Posted by on September 2, 2006 in Unix & Linux