telnet server在嵌入式系統上的建立

http://blog.chinaunix.net/u/21948/showart_506112.html

 

Telnet協議是登陸遠程網絡主機最簡單的方法之一,只是安全性非常低。對target board來說,必須執行telnet監控程序,這樣纔可以遠程登陸到target board。同時,如果想從開發板通過telnet遠程登陸其他host,就需要具備telent client。

 
    在嵌入式Linux系統上的telnet的工具有:
 
    ·telnet client
 
    busybox telnet client。busybox本身就是爲嵌入式系統量身打造,其telnet client精簡,而且比較好用。
 
    ·telnet server
 
    主要有telnetd和utelnetd。就文件大小而言,utelnetd套件產生的二進制文件比telnetd要小,但是utelnetd不支持internet super-server.下面先看busybox的telnet功能。client很簡單,選擇上就可以用了;而telnetd則要相對麻煩一些。
 
    Telnetd的移植倒不麻煩,busybox已經集成了一個。但是因爲開始時配置出現問題,所以費了些時間纔算穩定。
 
(1)busybox的配置
 
    對Telnetd的配置部分:
 

Networking Utilities --->

 

[*]telnetd

[*]  Support standalone telnetd (not inetd only)

 
    這個地方的配置說明,telnetd可以由inetd來啓動,也可以standalone啓動。
 
(2)編譯之後,因爲telnetd是busybox的一部分,我在編譯busybox時採用了動態編譯的方法,所以只要把busybox依賴的動態庫放到/lib下,就能保證telnetd不會產生找不到動態庫的問題。所以在make;make install之後,telnetd算是到了開發板上。但是僅僅這樣還不能讓telnetd正常運行。參考配置telnetd時的help部分:
 

    A daemon for the TELNET protocol, allowing you to log onto the host running the daemon. Please keep in mind that the TELNET protocol sends passwords in plain text. If you can't afford the space for an SSH daemon and you trust your network, you may say 'y' here. As a more secure alternative, you should seriously consider installing the very small Dropbear SSH daemon instead:

    http://matt.ucc.asn.au/dropbear/dropbear.html

    Note that for busybox telnetd to work you need several things:

    First of all, your kernel needs:

    UNIX98_PTYS=y

    DEVPTS_FS=y

    Next, you need a /dev/pts directory on your root filesystem:

    $ ls -ld /dev/pts

    drwxr-xr-x 2 root root 0 Sep 23 13:21 /dev/pts/

    Next you need the pseudo terminal master multiplexer /dev/ptmx:

    $ ls -la /dev/ptmx

    crw-rw-rw- 1 root tty 5, 2 Sep 23 13:55 /dev/ptmx

    Any /dev/ttyp[0-9]* files you may have can be removed.

    Next, you need to mount the devpts filesystem on /dev/pts using:

    mount -t devpts devpts /dev/pts

    You need to be sure that Busybox has LOGIN and FEATURE_SUID enabled. And finally, you should make certain that Busybox has been installed setuid root:

    chown root.root /bin/busybox

    chmod 4755 /bin/busybox with all that done, telnetd _should_ work....

 
    對Linux內核的配置而言,默認已經滿足。我出現錯誤主要是在mdev的初始化上,因爲對mdev不熟悉,導致在安排文件掛載順序時不合理,總是提示找不到/dev/pts。對於mdev如何安排順序,應該看一下文檔中的mdev.txt.
 

-------------
 MDEV Primer
-------------

For those of us who know how to use mdev, a primer might seem lame. For
everyone else, mdev is a weird black box that they hear is awesome, but can
't
seem to get their head around how it works. Thus, a primer.

-----------
 Basic Use
-----------

Mdev has two primary uses: initial population and dynamic updates. Both
require sysfs support in the kernel and have it mounted at /sys. For dynamic
updates, you also need to have hotplugging enabled in your kernel.

Here'
s a typical code snippet from the init script:
[1] mount -t sysfs sysfs /sys
[2] echo /bin/mdev > /proc/sys/kernel/hotplug
[3] mdev -s

Of course, a more "full" setup would entail executing this before the previous
code snippet:
[4] mount -t tmpfs mdev /dev
[5] mkdir /dev/pts
[6] mount -t devpts devpts /dev/pts

The simple explanation here is that [1] you need to have /sys mounted before
executing mdev. Then you [2] instruct the kernel to execute /bin/mdev whenever
a device is added or removed so that the device node can be created or
destroyed. Then you [3] seed /dev with all the device nodes that were created
while the system was booting.

For the "full" setup, you want to [4] make sure /dev is a tmpfs filesystem
(assuming you
're running out of flash). Then you want to [5] create the
/dev/pts mount point and finally [6] mount the devpts filesystem on it.

-------------
 MDEV Config (/etc/mdev.conf)
-------------

Mdev has an optional config file for controlling ownership/permissions of
device nodes if your system needs something more than the default root/root
660 permissions.

The file has the format:
        <device regex> <uid>:<gid> <octal permissions>
For example:
        hd[a-z][0-9]* 0:3 660

The config file parsing stops at the first matching line. If no line is
matched, then the default of 0:0 660 is used. To set your own default, simply
create your own total match like so:
        .* 1:1 777

If you also enable support for executing your own commands, then the file has
the format:
        <device regex> <uid>:<gid> <octal permissions> [<@|$|*> <command>]
The special characters have the meaning:
        @ Run after creating the device.
        $ Run before removing the device.
        * Run both after creating and before removing the device.

The command is executed via the system() function (which means you'
re giving a
command to the shell), so make sure you have a shell installed at /bin/sh.

For your convenience, the shell env var $MDEV is set to the device name. So if
the device 'hdc' was matched, MDEV would be set to "hdc".

----------
 FIRMWARE
----------

Some kernel device drivers need to request firmware at runtime in order to
properly initialize a device. Place all such firmware files into the
/lib/firmware/ directory. At runtime, the kernel will invoke mdev with the
filename of the firmware which mdev will load out of /lib/firmware/ and into
the kernel via the sysfs interface. The exact filename is hardcoded in the
kernel, so look there if you need to want to know what to name the file in
userspace.

 
    我修改之後的初始化順序爲:
 

[root@listentec ~]#cat /etc/fstab
proc /proc proc defaults 0 0
mdev /dev tmpfs defaults 0 0

[root@listentec ~]#cat /etc/init.d/rcS
#!/bin/sh
# Initial Environment

# mount /etc/fstab spcified device
/bin/mount -a

# mount devpts in order to use telnetd
/bin/mkdir /dev/pts
/bin/mount -t devpts devpts /dev/pts

# read the busybox docs: mdev.txt
/bin/mount -t sysfs sysfs /sys
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s

# when mdev is mounted, /sys can be umounted
/bin/umount /sys

 
    這樣,就沒有問題了。
 

[root@listentec ~]#cat /etc/inittab
::sysinit:/etc/init.d/rcS

::respawn:-/bin/login
::restart:/sbin/init

::once:/sbin/telnetd -l /bin/login

::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a

 
    現在只能是單獨啓動。使用inetd還不行。經過測試,沒有問題。
 
============================================================
運行telnetd(及telnet服務器)之後,別的站點就可以telnet登陸了。
 
1.運行telnetd和運行其它系統命令一樣,後面不帶參數
2.也可以放到/etc/init.d/rcS中,由系統自動啓動
#-----------------------------------
echo "start telnet_server"
/sbin/telnetd
#-----------------------------------
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章