Hiển thị các bài đăng có nhãn Linux căn bản. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Linux căn bản. Hiển thị tất cả bài đăng

Thứ Hai, 3 tháng 2, 2014

// // Leave a Comment

Cấu hình mạng căn bản

I. Hostname (tên máy):
- Xem hostname hệ thống:
[root@cent5 ~]# hostname
cent5.thehai.com
hoặc:
[root@cent5 ~]# uname -n
cent5.thehai.com
- Thay đổi hostname:
[root@cent5 ~]# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
192.168.1.7     testserver

- Nếu muốn hostname thay đổi kể cả khi ta khởi động lại hệ thống, thì ta chỉnh sửa file:
[root@cent5 ~]# vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=cent5.thehai.com

(khởi động lại máy sau khi đổi HOSTNAME)

II. Cấu hình IP của card mạng:
Xem thông tin tất cả các card mạng
[root@cent5 ~]# ifconfig -a hoặc ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3691 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3296 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:341204 (333.2 KiB)  TX bytes:402449 (393.0 KiB)
          Interrupt:67 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2319 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2319 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3669160 (3.4 MiB)  TX bytes:3669160 (3.4 MiB)
 Nếu muốn xem từng card mạng thì
 [root@cent5 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3726 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3339 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:344185 (336.1 KiB)  TX bytes:407567 (398.0 KiB)
          Interrupt:67 Base address:0x2000

- Thay đổi địa chỉ IP:
[root@cent5 ~]# ifconfig eth0 192.168.36.231 netmask 255.255.255.0 up

Tuy nhiên IP này chỉ được gán tạm thời, và không được lưu lại khi khởi động lại máy hoặc khởi động lại service network

Vì thế, để lưu lại IP ta chỉnh sửa file ifcfg-eth0
[root@cent5 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HWADDR=00:0c:29:d8:12:fe
TYPE=Ethernet

Hoặc:

DEVICE=eth0
HWADDR=00:0C:29:14:11:7A
TYPE=Ethernet
IPADDR=192.168.1.99
NETMASK=255.255.255.0
UUID=a64c8673-c53a-4f6f-b46d-1f4a77419c20
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
GATEWAY=192.168.1.1

Hoặc - Dcom

DEVICE=eth1
HWADDR=00:0C:29:03:B2:53
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
PEERDNS=yes
PEERROUTES=yes
Hoặc (nc ngoài - chuẩn)
Card WAN:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=aa.aa.aa.aa        # e.g. 123.45.67.89
NETMASK=bb.bb.bb.bb       # e.g. 255.255.255.0
GATEWAY=ee.ee.ee.ee       # e.g. 123.45.67.1
HWADDR=00:30:4f:3b:af:45  # MAC address (optional entry)
Debian - Ubuntu:
auto eth0
iface eth0 inet static
address aa.aa.aa.aa
netmask bb.bb.bb.bb
gateway ee.ee.ee.ee
Card LAN:
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=cc.cc.cc.cc       # e.g. 192.168.0.1
NETMASK=dd.dd.dd.dd      # e.g. 255.255.255.0
HWADDR=00:50:8d:d1:24:db # MAC address of LAN NIC (optional entry)
Debian - Ubuntu:
auto eth1
iface eth1 inet static
address cc.cc.cc.cc
netmask dd.dd.dd.dd
- Khởi động / stop network:
[root@cent5 ~]# /etc/init.d/network start|stop|restart|reload|status
[root@cent5 ~]# service network start|stop|restart|reload|status
- Tạo card mạng ảo:
+ Tạo nhanh tạm thời và kiểm tra lại:
[root@cent5 ~]# ifconfig eth0:0 192.168.100.20 netmask 255.255.255.0 up
[root@cent5 ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:344 errors:0 dropped:0 overruns:0 frame:0
          TX packets:350 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:44584 (43.5 KiB)  TX bytes:35875 (35.0 KiB)
          Interrupt:67 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.100.20  Bcast:192.168.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:67 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1817 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1817 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2972052 (2.8 MiB)  TX bytes:2972052 (2.8 MiB)

Nhưng sau khi khởi động lại netword thì card eth0:0 sẽ bị mất. Để lưu lại, ta sao chép file ifcfg-eth0 thành 1 file khác với tên mới ifcfg-eth0:0, sau đó sửa lại IP
[root@cent5 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
Mở file vừa tạo và chỉnh sửa nội dung:
[root@cent5 network-scripts]# vi ifcfg-eth0:0

Nội dung (sửa IP và device thôi cũng đc):
DEVICE=eth0:0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.100.30
NETMASK=255.255.255.0
HWADDR=00:0c:29:d8:12:fe
TYPE=Ethernet

 Hoac

DEVICE=eth0:0
HWADDR=00:0C:29:14:11:7A
TYPE=Ethernet
IPADDR=192.168.1.110
NETMASK=255.255.255.0
UUID=a64c8673-c53a-4f6f-b46d-1f4a77419c20
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static

Khởi động lại network và kiểm tra lại thử:
[root@cent5 network-scripts]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:
Determining IP information for eth0... done.
                                                           [  OK  ]
[root@cent5 network-scripts]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:754 errors:0 dropped:0 overruns:0 frame:0
          TX packets:676 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:81836 (79.9 KiB)  TX bytes:78463 (76.6 KiB)
          Interrupt:67 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.100.30  Bcast:192.168.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:67 Base address:0x2000

III. Lệnh route:
- Xem routing table (bảng định tuyến):
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

- Set Default gateway cho từng ethernet device:
[root@cent5 /]# route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.100.1

Kiểm tra lại:
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   192.168.100.1   255.255.255.0   UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
- Thêm Default gateway:
[root@cent5 /]# route add default gw 192.168.100.1

Kiểm tra lại:
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   192.168.100.1   255.255.255.0   UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.100.1   0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

- Xóa 1 Default route:
[root@cent5 /]# route del default gw 192.168.100.1
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   192.168.100.1   255.255.255.0   UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

- Xóa 1 đường đi trong bảng định tuyến:
[root@cent5 /]# route del -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.100.1 eth0
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

IV. Lệnh netstat:
- Xem routing table:
[root@cent5 /]# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.100.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0

- Kiểm tra các port đang mở:
[root@cent5 /]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State   
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN  
tcp        0      0 0.0.0.0:905                 0.0.0.0:*                   LISTEN  
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN  

V. Các lệnh khác:
- Lệnh tracert: Kiểm tra kết nối đến 1 remote host:
[root@cent5 ~]# tracert www.google.com
traceroute to www.google.com (74.125.203.103), 30 hops max, 40 byte packets
 1  mygateway1.ar7 (192.168.1.1)  1.313 ms  1.936 ms  1.750 ms
 2  localhost (123.26.48.1)  14.510 ms  15.097 ms  17.336 ms
 3  vdc.vn (123.29.3.33)  17.896 ms  26.981 ms  27.135 ms
 4  * * *
 5  * * *
 6  * * *
 7   (74.125.51.214)  105.699 ms  76.613 ms  78.817 ms
 8   (209.85.241.54)  81.346 ms  (72.14.221.6)  75.772 ms

- Lệnh tcpdump: Capture các packets
[root@cent5 ~]# tcpdump -n icmp -i eth0

VI. Tập tin /etc/resolv.conf:
Khai báo cấu hình DNS Client
nameserver 192.168.1.1
seach thehai.com
nameserver 8.8.8.8
nameserver 8.8.4.4
 search : danh sách các domain cho việc lookup các host-name, mặc định khai báo local domain. (tối đa khai báo 6 domain và tổng số ký tự là 256 ký tự)
nameserver : chỉ định các DNS Servers

Read More

Thứ Sáu, 31 tháng 1, 2014

// // Leave a Comment

Quản lý người dùng và nhóm - CentOS 5.10 - 6.4


I. Xem thông tin người dùng:
1. Tập tin  /etc/passwd: là cơ sở dữ liệu các tài khoản người dùng trên Linux dưới dạng văn bản
thehai:x:500:500:the hai truong:/home/thehai:/bin/bash
tên tài khoản:mật khẩu:UID:GID:mô tả:thư mục home của tài khoản:login shell
2. Tập tin  /etc/shadow: nơi nơi lưu trữ mật khẩu của các user đã được mã hóa.
thehai:$1$RnytPPqk$IXFaXWDXTm1Wn8Tsnw5eU0:16099:0:99999:7:::
chú thích: next

CHÚ Ý: Cột thứ 2 chứa mật khẩu đã được mã hoá. Nếu:
bắt đầu bằng dấu * : tài khoản này đã bị vô hiệu hóa - disable
bắt đầu bằng !! : tài khoản này tạm thời bị khóa - locked

3. Tập tin /etc/group: lưu các thông tin về nhóm:
loptnk33:x:500:thehai, tuanmanh,baodung
tên nhóm:mật khẩu của nhóm:GID:các thành viên trong nhóm
II. Quản lý người dùng:
1. Tạo tài khoản người dùng:
- Cú pháp xem trước các lệnh và ý nghĩa của chúng: [root@cent5 ~]# man useradd

- Tạo người dùng có tên haitruong: [root@cent5 ~]# useradd haitruong
- Kiểm tra haitruong trong: [root@cent5 ~]# more /etc/passwd
haitruong:x:501:501::/home/haitruong:/bin/bash

- Kiểm tra haitruong trong [root@cent5 ~]# more /etc/shadow
haitruong:!!:16101:0:99999:7:::
#tài khoản này đang tạm thời bị khóa do chưa đc tạo password
- Kiểm tra haitruong trong /etc/group:
[root@cent5 ~]# cat /etc/group
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
......
thehai:x:500:
haitruong:x:501:
hieutruong:x:502:
Đặt mật khẩu cho haitruong:
[root@cent5 ~]# passwd haitruong
Changing password for user haitruong.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

- Kiểm tra lại:
[root@cent5 ~]# cat /etc/shadow
root:$1$l3nKuOSI$YR9tWa9HeNarsO9pw/2Zx0:16099:0:99999:7:::
bin:*:16099:0:99999:7:::
daemon:*:16099:0:99999:7:::
.....
gdm:!!:16099:0:99999:7:::
thehai:$1$RnytPPqk$IXFaXWDXTm1Wn8Tsnw5eU0:16099:0:99999:7:::
haitruong:$1$bQ2zheZq$itUoWGKG3.ZJck5LThVyO.:16114:0:99999:7:::
Chú ý:
  • Khi tạo ra 1 user, nếu ta ko thay đổi passwprd cho user thì user đó sẽ tạm thời bị khóa và chưa sử dụng đc
  • Khi tạo user mà ko chỉ userID thì hệ thống sẽ tự đắt userID với giá trị >= 500
  • Khi tạo user với userID = 0 thì user đó có quyền root
  • khi tạo user mà ko chỉ ra home directori thì mặc định home dir sẽ nằm trong thư mục /home

- Tạo user anhtuyet có home dir là /tmp/anhtuyet với dòng mô tả: day la ha thi anh tuyet

[root@cent5 ~]# useradd -c "day la ha thi anh tuyet" -d /tmp/anhtuyet anhtuyet
- Kiểm tra lại:
[root@cent5 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
.....
anhtuyet:x:503:503:day la ha thi anh tuyet:/tmp/anhtuyet:/bin/bash

2. Đổi mật khẩu: #passwd username

3. Thay đổi thông tin người dùng - CentOS 6.4:
 - Xem trước cú pháp lệnh:
[root@linux ~]# man usermod
-bash: man: command not found
[root@linux ~]# usermod
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -Z, --selinux-user            new SELinux user mapping for the user account
- Thay đổi home dir của user thehai từ :/home/thehai thành :/home/thehaitruong
[root@linux ~]# usermod -d /home/thehaitruong/ thehai
[root@linux ~]# tail 5 /etc/passwd #Kiểm tra lại
tail: cannot open `5' for reading: No such file or directory
==> /etc/passwd <==
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
thehai:x:500:500::/home/thehaitruong/:/bin/bash
squid:x:23:23::/var/spool/squid:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin

- Thay đổi/Thêm user thehai vào 1 nhóm nào đó group1 - nhom2

 [root@linux ~]# groupadd nhom1 #Tạo nhóm
[root@linux ~]# groupadd nhom2 #Tạo nhóm 2
[root@linux ~]# usermod -g nhom1 thehai #thêm user thehai vào nhóm1
[root@linux ~]# tail 5 /etc/passwd #Kiêm tra lại thông tin user thehai
tail: cannot open `5' for reading: No such file or directory
==> /etc/passwd <==
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
thehai:x:500:501::/home/thehaitruong/:/bin/bash #gID đã tahy đổi từ 500 thành 501
squid:x:23:23::/var/spool/squid:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
[root@linux ~]# tail 5 /etc/group # Kiểm tra thông tin 2 gruop vừa tạo
tail: cannot open `5' for reading: No such file or directory
==> /etc/group <==
postfix:x:89:
sshd:x:74:
rpc:x:32:
rpcuser:x:29:
nfsnobody:x:65534:
thehai:x:500:
squid:x:23:
apache:x:48:
nhom1:x:501:
nhom2:x:502:


Chuyển user thehai từ nhom1 vào nhom2
[root@linux ~]# usermod -g nhom2 thehai
[root@linux ~]# tail 5 /etc/passwd #Kiểm tra lại thông tin user thehai
tail: cannot open `5' for reading: No such file or directory
==> /etc/passwd <==
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
thehai:x:500:502::/home/thehaitruong/:/bin/bash
squid:x:23:23::/var/spool/squid:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
 4. Khóa và mở khóa tài khoản người dùng:
KHÓA:
[root@linux ~]# passwd -l thehai
Locking password for user thehai.
passwd: Success
Kiểm tra lại:
[root@linux ~]# tail 5 /etc/shadow
tail: cannot open `5' for reading: No such file or directory
==> /etc/shadow <==
vcsa:!!:16013::::::
saslauth:!!:16013::::::
postfix:!!:16013::::::
sshd:!!:16013::::::
rpc:!!:16114:0:99999:7:::
rpcuser:!!:16114::::::
nfsnobody:!!:16114::::::
thehai:!!$6$lGtjFmBa$aR756LnHLkXOD/SvQT5Mw2gclpDkRRGu6R8/YmgvEn8t8gFYs/97kPTtU9Q1kDhCECtUPau38Sau9wvuVQqcl.:16115:0:99999:7:::
squid:!!:16115::::::
apache:!!:16115::::::
MỞ KHÓA:
[root@linux ~]# passwd -u thehai
Unlocking password for user thehai.
passwd: Success
kiểm tra lại
[root@linux ~]# tail 5 /etc/shadow
tail: cannot open `5' for reading: No such file or directory
==> /etc/shadow <==
vcsa:!!:16013::::::
saslauth:!!:16013::::::
postfix:!!:16013::::::
sshd:!!:16013::::::
rpc:!!:16114:0:99999:7:::
rpcuser:!!:16114::::::
nfsnobody:!!:16114::::::
thehai:$6$lGtjFmBa$aR756LnHLkXOD/SvQT5Mw2gclpDkRRGu6R8/YmgvEn8t8gFYs/97kPTtU9Q1kDhCECtUPau38Sau9wvuVQqcl.:16115:0:99999:7::: #đã mất dấu !!
squid:!!:16115::::::
apache:!!:16115::::::

5. Tạo nhóm người dùng:
Cú pháp xem trước lệnh:
[root@linux ~]# groupadd
Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
Tạo và kiểm tra thông tin nhóm:
[root@linux ~]# groupadd nhom3 #Tạo nhóm
[root@linux ~]# tail 5 /etc/group #Kiểm tra thông tin nhóm
tail: cannot open `5' for reading: No such file or directory
==> /etc/group <==
sshd:x:74:
rpc:x:32:
rpcuser:x:29:
nfsnobody:x:65534:
thehai:x:500:
squid:x:23:
apache:x:48:
nhom1:x:501:
nhom2:x:502:
nhom3:x:503:
6. Thay đổi thông tin nhóm:
- Cú pháp xem lệnh:
[root@linux ~]# groupmod
Usage: groupmod [options] GROUP

Options:
  -g, --gid GID                 change the group ID to GID
  -h, --help                    display this help message and exit
  -n, --new-name NEW_GROUP      change the name to NEW_GROUP
  -o, --non-unique              allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD       change the password to this (encrypted)
                                PASSWORD

- Đổi tên và gID của nhom3 thành group3 với gID = 505
[root@linux ~]# groupmod -g 505 -n group3 nhom3
[root@linux ~]# tail 5 /etc/group #Kiểm tra lại
tail: cannot open `5' for reading: No such file or directory
==> /etc/group <==
sshd:x:74:
rpc:x:32:
rpcuser:x:29:
nfsnobody:x:65534:
thehai:x:500:
squid:x:23:
apache:x:48:
nhom1:x:501:
nhom2:x:502:
group3:x:505:
7. Xóa nhóm:
[root@linux ~]# groupdel group3
[root@linux ~]# tail 5 /etc/group # Kiểm tra lại
tail: cannot open `5' for reading: No such file or directory
==> /etc/group <==
postfix:x:89:
sshd:x:74:
rpc:x:32:
rpcuser:x:29:
nfsnobody:x:65534:
thehai:x:500:
squid:x:23:
apache:x:48:
nhom1:x:501:
nhom2:x:502:
III. Login/Logout:
- Từ root đăng nhập vào usera: su usera
- Từ usera đăng nhập vào userc: su userc
- Thoát khỏi 1 tài khoản: exit
[root@linux ]# su thehai #Từ root, đnawg nhập vào user thehai
bash-4.1$ pwd #Kiểm tra thư mục hiện hành
/home/thehai/
bash-4.1$ touch /home/thehai/xinchao.txt #Tạo 1 file bất kỳ trong thư mục này
bash-4.1$ ls /home/thehai #Kiểm tra lại
xinchao.txt
bash-4.1$ exit #Thoát user thehai
exit
[root@linux ]#

IV. Định nghĩa cấu hình mặc định cho người dùng:
Khi dùng lệnh useradd không có option kèm theo để tạo một user, các thuộc tính của user sẽ được tìm kiếm theo cấu hình mặc định trong các file:
/etc/default/useradd
/etc/login.defs
/etc/skel/


- Xem nội dung file /etc/default/useradd:
Thay đổi giá trị của option HOME thành "/var/home":
[root@linux ~]# vi /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/var/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
Tạo folder home trong /var
[root@linux ~]# mkdir /var/home
- Dùng lệnh useradd, tạo một user mới thehieu.
- Kiểm tra thấy, thehieu có thư mục home directory trong /var/home:

 [root@linux ~]# useradd thehieu
[root@linux ~]# ll /var/home/
total 4
drwx------. 2 thehieu thehieu 4096 Feb 17 02:34 thehieu
- Liệt kê nội dung trong thư mục /var/home/thehieu (bao gồm cả file ẩn):
[root@linux ~]# ll -a /var/home/thehieu/
total 20
drwx------. 2 thehieu thehieu 4096 Feb 17 02:34 .
drwxr-xr-x. 3 root    root    4096 Feb 17 02:34 ..
-rw-r--r--. 1 thehieu thehieu   18 Feb 21  2013 .bash_logout
-rw-r--r--. 1 thehieu thehieu  176 Feb 21  2013 .bash_profile
-rw-r--r--. 1 thehieu thehieu  124 Feb 21  2013 .bashrc

- Các file ẩn này được tạo default trong thư mục /etc/skel. Khi tạo mới một user, nội dung trong thư mục /etc/skel sẽ được tự tạo cho mỗi user:
[root@linux ~]# ll -a /etc/skel/
total 20
drwxr-xr-x.  2 root root 4096 Nov  4 18:41 .
drwxr-xr-x. 61 root root 4096 Feb 17 02:34 ..
-rw-r--r--.  1 root root   18 Feb 21  2013 .bash_logout
-rw-r--r--.  1 root root  176 Feb 21  2013 .bash_profile
-rw-r--r--.  1 root root  124 Feb 21  2013 .bashrc
- Thử test 1 công dụng:
[root@linux ~]# vi /etc/skel/xin_chao.txt #Tạo thử 1 file
Xin chao ban, chao mung den voi cty! #Nội dung
[root@linux ~]# useradd anhtuyet #Tạo 1 user mới
[root@linux ~]# ll -a /var/home/anhtuyet/ #Kiểm tra home dir của user vừa tạo
total 24
drwx------. 2 anhtuyet anhtuyet 4096 Feb 17 02:41 .
drwxr-xr-x. 4 root     root     4096 Feb 17 02:41 ..
-rw-r--r--. 1 anhtuyet anhtuyet   18 Feb 21  2013 .bash_logout
-rw-r--r--. 1 anhtuyet anhtuyet  176 Feb 21  2013 .bash_profile
-rw-r--r--. 1 anhtuyet anhtuyet  124 Feb 21  2013 .bashrc
-rw-r--r--. 1 anhtuyet anhtuyet   37 Feb 17 02:40 xin_chao.txt

- Xem nội dung file /etc/login.defs:
[root@linux ~]# cat /etc/login.defs
#
# Please note that the parameters in this configuration file control the
# behavior of the tools from the shadow-utils component. None of these
# tools uses the PAM mechanism, and the utilities that use PAM (such as the
# passwd command) should therefore be configured elsewhere. Refer to
# /etc/pam.d/system-auth for more information.
#

# *REQUIRED*
#   Directory where mailboxes reside, _or_ name of file, relative to the
#   home directory.  If you _do_ define both, MAIL_DIR takes precedence.
#   QMAIL_DIR is for Qmail
#
#QMAIL_DIR      Maildir
MAIL_DIR        /var/spool/mail
#MAIL_FILE      .mail

# Password aging controls:
#
#       PASS_MAX_DAYS   Maximum number of days a password may be used.
#       PASS_MIN_DAYS   Minimum number of days allowed between password changes.
#       PASS_MIN_LEN    Minimum acceptable password length.
#       PASS_WARN_AGE   Number of days warning given before a password expires.
#
-  Đây là file định nghĩa các policy liên quan đến password: độ dài password, ngày hết hạn, ngày warning…
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

#
# Min/max values for automatic uid selection in useradd
#
UID_MIN                   500
UID_MAX                 60000

#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN                   500
GID_MAX                 60000

#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD    /usr/sbin/userdel_local

#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
-  File này cũng cho phép ta đinh nghĩa khi tạo user mới, có tạo home directory không?
CREATE_HOME     yes

# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK           077

# This enables userdel to remove user groups if no members exist.
#
-  Khi xóa một user, có xóa luôn group không? (Group chỉ có một member). Có xóa cả các cron, job không?
USERGROUPS_ENAB yes

# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512


==============

/etc/passwd
User account information.
/etc/shadow
Secure user account information.
/etc/group
Group account information.
/etc/gshadow
Secure group account information.
/etc/default/useradd
Default values for account creation.
/etc/skel/
Directory containing default files.
/etc/login.defs
Shadow password suite configuration.


Read More

Thứ Năm, 30 tháng 1, 2014

// // Leave a Comment

Quản lý Package "rpm" - CentOS 5.10

1. Quản lý trên giao diện đồ họa:
Vào menu Aplications -> Add/Remove Software (hoặc có thê sử dụng lênh #system-config-packages)
Read More