2008. 7. 21. 00:03

Chapter 5. Unix Basic Commands _ 기타 관리용 명령어

기타 관리용 명령어

■ wc CMD

■ su CMD

■ id CMD

■ groups CMD

■ last CMD

■ who CMD

■ w CMD

■ rusers CMD

wc CMD

_________________

display a count of lines, words and characters in a file

The wc utility reads one or more input files and, by

default, writes the number of newline characters, words and

bytes contained in each input file to the standard output.

# wc /etc/passwd

# wc -l /etc/passwd // line count

# wc -w /etc/passwd // word count

# wc -c /etc/passwd // character count


[
참조] wc -l 명령어 응용예

# cat /etc/passwd | wc -l

# pkginfo | wc -l

# patchadd -p | wc -l

# ps -ef | wc -l


(
시스템 성능/사용량 카운트 수집)


(
첫번째 ) Apache Webserver

- apache 1.3.X (Process 방식)

- apache 2.X (Thread 방식)

Web Client -----------------> Web Server

http://Server httpd

# ps -ef | grep httpd | wc -l > httpd.count

// process counting


(
두번째 )

# df -k

# df -k / | tail -1 | awk '{print $5}' > df.count


(
세번째 )

# ftpcount | grep realuser | awk '{print $5}' > ftp.count


su CMD

__________________

become super user or another user

The su command allows one to become another user without

logging off or to assume a role. The default user name is

root (superuser).


(
명령어 형식)

su [user]

su - [user]


■ End User => root (O) EX) user01 => root

■ End User => End User (O) EX) user01 => user02

■ root => End User (O) EX) root => user01

■ root => root (X)

Becoming User bin While Retaining Your Previously

Exported Environment

To become user bin while retaining your previously exported

environment, execute:

example% su bin

Example 2: Becoming User bin and Changing to bin's Login

Environment

To become user bin but change the environment to what would

be expected if bin had originally logged in, execute:

example% su - bin

su 명령어에 "-"(Dash) 기호가 없는 경우에는 사용자의 EUID/EGID 값을 받게 되지만 사용자의 환경 변수들은 이전 사용자의 환경변수값을 받는다. 사용자만 전환하게 되는 것이다. su 명령어에 "-" 기호가 있는 경우에는 사용자의 EUID/EGID 값을 받게 되는 것 뿐만 아니라원본 사용자가 새로 로그인 한것 처럼 해준다. 사용자가 로그인을 하게 되면 홈디렉토리로 로그인 하게 되고 사용자의 환경 파일들을 읽어 들이기 때문에 사용자의 환경으로 적용되게 된다.

[EX] "su bin / su - bin" 명령어 실습

# cd /etc

# pwd

# su user01

(a). shell prompt (# echo $PS1)

(b). current directory (# pwd)

(c). echo $PATH

# id

# exit

# cd /etc

# pwd

# su - user01

(a). shellp prompt (# echo $PS1)

(b). current directory (# pwd)

(c). echo $PATH

$ id

$ exit


[
참고] 오라클 사용자로 전환(When Oracle User Switching)

(시나리오) root 사용자로 작업을 하다가 oracle 사용자로 전환 해야 하는 경우 다음과

명령어를 사용하지 않는다면 문제가 발생할 있다. 이것은 oracle 사용자의 홈디렉토

리에 존재하는 환경파일(: ~/.profile) 변수 설정이 되지 않는다면 oracle 사용자가

정상적으로 작업을 할수 없기 때문이다.


[EX]
스크립트에서 많이 사용되는 su 명령어의

To execute command with the temporary environment and permissions of user bin

# su - bin -c "command args"



id CMD

__________________

return user identity

If no user operand is provided, the id utility writes the

user and group IDs and the corresponding user and group

names of the invoking process to standard output. If the

effective and real IDs do not match, both are written. If

multiple groups are supported by the underlying system,

/usr/xpg4/bin/id also writes the supplementary group affili-

ations of the invoking process.

(명령어 형식)

# id

# id -a

# id user01


[
참고] root 사용자의 UID, GID

UID=0(root), GID=1(other)


[
참고] UID, GID, EUID, EGID

- UID/GID - who am i // login 당시 !!!

- EUID/EGID - id, whoami // 현재 내가 누구인지

- UID (User Identification)

- EUID(Effective UID)

- GID (Group Identification)

- EGID(Effective GID)


[EX] UID, GID, EUID, EGID
개념의 이해

# telnet localhost

user01 사용자로 로그인

$ id

uid=100(user01) gid=100(class1)

$ /usr/ucb/whoami

user01

$ who am i

user01 pts/9 Feb 2 01:48 (localhost)

$ su - root

Password: <----- root 사용자 암호 입력

# id

uid=0(root) gid=1(other)

# /usr/ucb/whoami

root

# who am i

user01 pts/9 Feb 2 01:48 (localhost)

===== HOSTA ===== ===== HOSTB ======

# telnet HOSTB ------------> user01 사용자로 로그인

$ id

$ whoami

$ who am i

$ su - root

# id

# whoami

# who am i

----------------------------

UID EUID

----------------------------

500 500 --> user01 사용자로 로그인 했을때

500 0 --> "su - root" 명령어 수행

----------------------------



groups CMD

__________________

print group membership of user

The command groups prints on standard output the groups to

which you or the optionally specified user belong. Each user

belongs to a group specified in /etc/passwd and possibly to

other groups as specified in /etc/group. Note that

/etc/passwd specifies the numerical ID (gid) of the group.

The groups command converts gid to the group name in the

output.

(명령어 형식)

# groups

# groups user01

# groups user01 user02


[
참조] /etc/group 그룹 정의


[EX] groups
명령어 실습

# groups

other root bin sys adm uucp mail tty lp nuucp daemon

# groups user01

staff

# groups user01 root

user01 : staff

root : other root bin sys adm uucp mail tty lp nuucp daemon



last CMD

-------------

display login and logout information about users and terminals

The last command looks in the /var/adm/wtmpx file, which

records all logins and logouts, for information about a

user, a terminal, or any group of users and terminals. Argu-

ments specify names of users or terminals of interest. If

multiple arguments are given, the information applicable to

any of the arguments is printed. For example, last root con-

sole lists all of root's sessions, as well as all sessions

on the console terminal. last displays the sessions of the

specified users and terminals, most recent first, indicating

the times at which the session began, the duration of the

session, and the terminal on which the session took place.

last also indicates whether the session is continuing or was

cut short by a reboot.

The pseudo-user reboot logs in when the system reboots.

Thus, # last reboot

(명령어 형식)

# last

# last user01

# last reboot

+----------+

| |

| User |

| |

+----------+

| |

| reboot |

| |

+----------+


(
명령어 출력 화면 해석)

# last user01

.....

user01 pts/11 localhost Sat Jan 12 14:51 - 15:29 (00:38)

----------------------------------------------------

필드 설명

----------------------------------------------------

user01 사용자의 이름

pts/11 제어 터미널(가상 터미널)

localhost 원격 호스트

Sat Jan 12 14:51 로그인 시간

15:29 로그아웃 시간

(00:38) 사용한 시간

----------------------------------------------------


# last reboot

......

reboot system boot Thu Jan 10 15:39

----------------------------------------------------

필드 설명

----------------------------------------------------

reboot 가상사용자

system boot 상태정보

Thu Jan 10 15:39 리부팅 시간

----------------------------------------------------



[
참고] wtmpx, utmpx 파일

/var/adm/utmpx - who 명령어 참조

/var/adm/wtmpx - last 명령어 참조

at linux

/var/adm/utmp – who

/var/adm/wtmp - last



[EX] /var/adm/utmpx & /var/adm/wtmpx

# cd /var/adm

# mv wtmpx wtmpx.old

# last

# who

# mv wmtpx.old wtmpx



exit CMD

--------------

End the application

Terminate the process, returning returnCode to the system as

the exit status. If returnCode isn't specified then it

defaults to 0.

(명령어 형식)

# exit [Number]

[참고] exit Number

0 : 정상 종료(Normal Termination)

1 ~ 255 : 비정상 종료(Abnormal Termination)


[EX1]
현재 터미널 종료

# exit


[EX2]
로그 아웃

# telnet 172.16.8.254

root 사용자로 로그인

# hostname

# exit


[EX3]
서브 종료

# bash

# ps

# exit



who CMD

__________________

who is on the system // 현재 로그인된 사용자 정보 확인

The who utility can list the user's name, terminal line,

login time, elapsed time since activity occurred on the

line, and the process-ID of the command interpreter (shell)

for each current UNIX system user. It examines the

/var/adm/utmpx file to obtain its information. If file is

given, that file (which must be in utmpx(4) format) is exam-

ined. Usually, file will be /var/adm/wtmpx, which contains a

history of all the logins since the file was last created.


(
명령어 형식)

# who

# who -r (현재 Runlevel 확인)

# whoami (현재 사용자 정보 확인)

# who am i (로그인한 사용자 정보 확인)

# who -H (Headline, 헤드라인 정보 표시)


(
명령어 출력 화면 해석)

# who

root pts/4 1 14 09:57 (192.168.222.1:0.0)

----------------------------------------------------

필드 설명

----------------------------------------------------

root 사용자 정보

pts/4 제어 터미널

1 14 09:57 로그인 시간

192.168.222.1 원격호스트

----------------------------------------------------


[
참고] /var/adm/utmpx

# mv /var/adm/utmpx /var/adm/utmpx.old

# who

# mv /var/adm/utmpx.old /var/adm/utmpx


[EX] 172.16.8.254 login Test

# telnet 172.16.8.254

user01 사용자로 로그인

$

(At solaris254)

# who


whodo / w CMD

__________________

who is doing what // 현재 로그인한 사용자가 수행중인 명령어 확인

The whodo command produces formatted and dated output from

information in the /var/adm/utmpx and /proc/pid files.

The display is headed by the date, time, and machine name.

For each user logged in, device name, user-ID and login time

is shown, followed by a list of active processes associated

with the user-ID. The list includes the device name,

process-ID, CPU minutes and seconds used, and process name.


(
명령어 형식)

# whodo

# whodo user01

# whodo -l (w CMD 명령어 출력 형태로 출력)

# man whodo

.....

-l Produce a long form of output. The fields displayed

are: the user's login name, the name of the tty the

user is on, the time of day the user logged in (in

hours:minutes), the idle time - that is, the time

since the user last typed anything (in hours:minutes),

the CPU time used by all processes and their children

on that terminal (in minutes:seconds), the CPU time

used by the currently active processes (in

minutes:seconds), and the name and arguments of the

current process.

.....


(
명령어 출력 화면 해석)

# w

.....

user01 pts/8 7:16오후 -ksh

----------------------------------------------------

필드 설명

----------------------------------------------------

user01 사용자이름

pts/8 제어터미널

7:16오후 명령어 수행시간

-ksh 수행 명령어

----------------------------------------------------


[EX]
악의적인 사용자 로그아웃 시킴


[TERM1]
사용자 윈도우

# telnet localhost

user01 사용자로 로그인

$ vi /etc/passwd


[TERM2]
관리자 윈도우

# w

# w user01 (# w | grep user01)


[
참고] 사용자 활동 모니터링

# while [ 1 ]

> do

> w user01

> sleep 2

> done


[TERM3]
관리자 윈도우

# ps -U user01

ksh's PID(450)

# kill -9 450



rusers CMD

__________________

who is logged in on remote machines

The rusers command produces output similar to who(1), but

for remote machines. The listing is in the order that

responses are received, but this order can be changed by

specifying one of the options listed below.

A remote host will only respond if it is running the rusersd

daemon, which may be started up from inetd(1M) or

listen(1M).


(
명령어 형식)

# rusers -a

# rusers -h

# rusers -l

# rusers 172.16.8.202

# man rusers

.....

-a Give a report for a machine even if no users are

logged on.

-h Sort alphabetically by host name.

-l Give a longer listing in the style of who(1).

.....



출처 : http://cafe.daum.net/bscsolaris