顯示具有 Linux general commands 標籤的文章。 顯示所有文章
顯示具有 Linux general commands 標籤的文章。 顯示所有文章

2008年9月14日 星期日

Linux 用戶身份和密碼

Linux 分 root 和 user, root 是在 Windows 內的 administrator ! 在 login 時你會用 user 身份 login(你須 disable root 直接 login), 之後如果你想有 admin 權限, 你就須轉用 root 身份才可以做到更改 system 野, 就是有雙重保險, 因為你須用兩個不同的密碼, 所以如有人想控制你 system, 他須破解兩次你的密碼 !
# ( 代表 root)
$ (代表 user)

例如 :
[lisa@lisa ~]$ su -
Password:
[root@lisa ~]#

新增 user 和密碼
# adduser steven (是行一個 script 用 useradd 去製造 user, 會把 /etc/skel 的內容 copy 去 home directory)
# useradd steven (都可以製造 user, 但就吾見了 /etc/skel 內面的檔案, 如 Maildir)

給 user 製密碼 ( 用密碼, 一定要用強密碼, 就是用多過8個位的數目字加大小草和符號如#)
# passwd steven
Unix password

如一個 user 自己想改密碼也可以用
$ passwd lisa

# userdel -r steven (刪除 user)
# passwd -l steven (暫停戶口, Lock account)
# passwd -u steven (重開戶口, enable account)
# usermod sally -l sjones (改 user 名)
# w (檢視有那些 user login 過)
# who (檢視誰在 login)
# ftpwho (檢視誰在 login ftp)
# last steven (查看 steven 的 login 記錄)
# finger steven (查看 steven 的 shell)

# cat /etc/shadow (顯視 user 的壓縮了的密碼)
# cat /etc/passwd (會顯視系統內的全部 user, 可以最多收藏有 user ID = 2^16-1=65535 user)
# id steven (會顯視 steven 的 uid 和 gid )
# find / -uid 529 (找出 user id 529 的全部檔案)

# cat /etc/group (顯視 user 屬於那一個 group)
# find / -gid 504 (找出屬於 group id 504 的全部檔案)

多數 uid 和 gid 0 至 99 會分配給系統內的 applications :
0 是 root
2 是 daemon
(所以如果你見到某 uid 或 gid 是 0, 就代表已有駭客入了你系統)

你也可以用 root 身份去改 user 的 gid, 這樣同一個 group 的人都可以改到某些檔案 !
# vi /etc/group

每一個 user 有分配一個 user name 和一個 group name, 如果你在一個檔案夾內打
$ ls -l
-rwxr-xr-x 1 lisa lisa 19968 5月 14 2006 Regchangeaddress.doc
-rwxr-xr-x 1 lisa lisa 19968 5月 14 2006 Regchangebankac.doc
-rwxr-xr-x 1 lisa lisa 20480 5月 14 2006 sharedraw.doc

你就會見到以上顯視, 如果你想改某檔案的 owner 或 group, 你就可以轉 root 身份用以下指令
#chown user:group file

如果想改成個檔案夾內的全部檔案
#chown -R user:group directory

一個 user 都可以改自已的 group
$ newgrp newgroupname

Linux 開機 mode

呢樣叫 System Initialization.

當你開 Linux 時, 你可以選用視窗開或用文字模式開; 控制這 startup 是在以下 script 中, 你只須用編緝器入去改就可以的了 !

# vi /etc/inittab

0 - halt (關機)
1 - Single user mode (如在 Windows 內的 savemode)
2 - Multiuser without NFS
3 - Full multiuser mode (文字模式)
4 - unused
5 - X11 (Xwindow 視窗模式)
6 - reboot(重新開機)
id:3:initdefault (你只須 3 改為 5, 就可以用視窗開機, 而開動 Xwindow)

/etc/rc.d/rc0 (0至6)

#Trap CTRL-ALT-DELETE
ca:ctrlaltdel:/sbin/shutdown -t3 -r now (這代表如果你按 Ctrl+Alt+Delete, 部機就會重開, 所以想改Ctrl+Alt+Delete 功能, 可以在呢度改, 例如改為關機)

離開 vi 編緝器先按 Esc, 才打 :wq (save and quit), 只打 :w (save and not quit) 或 :q! (強行離開不儲存)

想用 single user mode, 在開機時按 Ctrl-x 和打 : single linux (本人沒用過, 所以你須自己去做 testing)

shutdown 指令, 會用在 script 內 :
# shutdown -h now (shut down)
# shutdown +5 (shut down after 5 min)
# shutdown -r now ( restart)
# shutdown -r 23:30 (restart at special time)
# shutdown [-t sec] [-arkhcfF] time [warning message]
t = default 5 sec
a = system will check /etc/shutdown.allow to give permission to shutdown
r = change to runlevel 6
k = send only shutdown message
h = change to runlevel 0
c = can still change your mind
f = skip fsck when reboot
F = force fsck when reboot
# shutdown -h +10 "system upgrade; start again after one hour"

現時我們在 console 會用以下指令來開關部機 :
# halt (停機)
# reboot(重開機)

2008年9月13日 星期六

Linux 硬碟顯視

IDE hard disk :
IDE controller 1
Primary master /dev/hda
Primary slave /dev/hdb
IDE controller 2
Secondary master /dev/hdc
Secondary slave /dev/hdd

SCSI hard disk
/dev/sda
/dev/sdb
/dev/sdc
/dev/sdd
等等..........

Partitions 會分為 /hda1, /hda2, /hda3, /hda4, 就是四個Primary partitions 或 Primary partitions + extended partition 中, /hda5, hda6, hda7 等等就是 logical drives.

其他 hardware 會似硬碟型式顯視出來 :
#ls -l /dev/cdrom
/dev/hdc

Printer /dev/Ipo
Modem /dev/ttyso, /dev/ttysa, /dev/ttys2

我在我部 laptop consol 打

[root@lisa ~]# df -hl
Filesystem 容量 已用 可用 已用% 掛載點
/dev/hda7 63G 31G 29G 52% /
/dev/hda5 9.4G 4.7G 4.7G 51% /backup
/dev/shm 379M 0 379M 0% /dev/shm

就顯視以上資料出來; 為何會變為 /dev/hda5 及 /dev/hda7 ?
可能因為我重裝了幾次和次次都保留 /backup 不格式 !
/dev/shm 就是我的 DVD-Rom


# ls -l /dev (顯視你部機的硬件)
# dmesg (檢查你部機的硬件運作)
# top (顯視你 CPU, RAM 和 Swap, 還有 run 緊的軟件和軟件用的 process ID), 打 Ctrl+C 退出 !
# free (檢查已用了幾多 swap)

Linux partition 和 Windows partition 一樣, 吾可以就甘改大小, 一定要用某些軟件來改, 吾係你會失去 partition 內的資料 !
你都可以用 LVM(Logical Volume Manager) 來控制 partition 的大小, 請看 LVM Howto 但用過後覺得幾麻煩, 因為如用在 server, 而你吾成日檢查, 可能因為吾夠位而某些 services 行吾到, 如 mail server 或 news server, 所以預先預多些空間俾重要的 application 就最好的了!

Symbolic link

Linux 有一樣好處, 就係可以用 symbolic link 來把一些檔案夾或檔案, 由一個 partition link 去第二個 partition, 所以你的檔案不一定要放在少位的 partition 內 !
例如你的相放在 /home/lisa/pic, 而你想 webserver 可以 display 你的相, 你可以去 webserver root 度, 如設在 /var/www
#cd /var/www
#ln -s /home/lisa/pic album
甘你就會見到在 /var/www 出現了一個叫 album 的檔案夾 !album 入面的檔案, 就是 pic 入面的檔案 !
當然還須留意 owner 和 permissions 的問題, 但就吾會在呢度講 !
註:我在 Fedora Core 遇到的問題是, 我不把舊 /home 在重裝時格式, 後來不能用 symbolic link 把 home folder link 去 /var/www ! 在另一部全新安裝部機中就做到 !

在 Windows IIS 都可以把第二個 partition 內的檔案夾 link 去 IIS webserver, 呢樣叫 Virtual Directory, 是一個對保安來說很好用的方法 !我以前用 IIS 時, 吾會用 wwwroot, 因為 standard 野, 好昜俾人破解攻入你 server !

2008年8月30日 星期六

Split 分割工具

一個在 Linux 系統已有的檔案分割工具, 可以把一個大檔案割成你想要的大小, 方便轉移 !

先把一些檔案合併和壓縮

# tar -czvf file.tar.gz filedirectory

之後分割

# split file.tar.gz --bytes=600000000 (about 572 MB)

檢視成果

# ls
xaa xab xac xad xae

之後可以用 FTP 或網頁轉移去第二部機

# wget http://www.yourdomain.com/xaa
# wget http://www.yourdomain.com/xab
# wget http://www.yourdomain.com/xac
# wget http://www.yourdomain.com/xad
# wget http://www.yourdomain.com/xae

才把分割檔合併

# cat x* > file.tar.gz

之後才解壓

# tar -xzvf file.tar.gz

2008年8月13日 星期三

RPM, apt-get, dpkg

RPM = Redhat Package Management

  • # rpm -qa |more (will show all your application installed in your machine)

  • # rpm -qa |less (less is more than more)

  • # rpm -qa | grep application (or egrep, show the version of the application)

  • # rpm -qi packagename (will show the informations about that package)

  • # rpm -ql packagename |more (will show in which directory the files of the package are saved)

  • # rpm -e filename (erase the file; you can type all the filenames separate with a comma)

  • # rpm -e software_name (uninstall)

  • # rpm -ivh mtools-3.9.7-3.i386.rpm (install the application)

  • # rpm -Uhv (can install or upgrade, not to use when you don't want to overwrite the old installation)

  • # rpm --test -Uhv packagename-packageversion no-buildno.CPU.rpm (for testing without installing)

  • # rpm --rebuilddb (when you have made changes to an application, you have to rebuild the database stored in /var/lib/rpm or /var/lib/dpkg for Debian)


for application installing in Debian:

  • #apt-get update (update the system)

  • #apt-get install application

  • #apt-get remove application

  • #apt-get --purge remove application

  • #apt-get upgrade application

  • #apt-get dist-upgrade application

  • #apt-cache search word

  • #apt-cache show word


  • #dpkg application.deb (install .deb package)

  • #dpkg -i application.deb (install package .deb)

  • #dpkg -r (remove)

  • #dpkg -P (purge)

  • #dpkg -L application (show info about the application installed by apt-get install)

  • #dpkg -l (show all the packages installed in debian)