Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

TOC

1. vim
2. whoami
3. adduser/useradd
4. passwd
5. /etc/passwd
6. /etc/shadow
7. addgroup
8. /etc/group
9. delgroup
10. usermod
11. Super user privileges
12. su/sudo
13. /etc/sudores
14. ls/dir
15. mkdir
16. touch
17. cat
18. find
19. cp
20. mv
21. Introduction to File permissions
22. id
23. stat
24. chmod
25. chown

vim

Two modes: Command/Editing


Command mode: <Esc>
Editing mode: i/<Insert>
Cursor movement
left: h
down: j
up: k
right: l
Or you can use arrow keys
Selection/Visual mode
Step 1: v or CTRL-v for vertical select
Step 2: Use cursor keys to select the text
Copy/Cut Paste
Copy: yy
Cut: dd
Paste: p

Copy into buffer: "ayy

1 of 8
Cut into buffer: "add
Paste from buffer: "ap
Undo
Undo: u
Undo to a given step: :undo <number>
Undo backward: :earlier <number>f
Redo forward: :later <number>f
Find and replace
Line: :%s/<find>/<replace>/
Range: :45,50 s/<find>/<replace>/g
Full document: :%s/<find>/<replace>/g
Split/Tabs
Split: :split <file> or :vsplit <file>
Change active split: <Ctrl>w+<Ctrl>-w
Tab: :tabe <file>
Check spell
Start: :set spell
Stop: :set spell!
Corrections: z=
More configuration (with ~/.vimrc)
syntax on
set tabstop=3
Code completion
Start: :autocmd FileType php set omnifunc=phpcomplete#CompletePHP
Help (Really useful)
Start: :help or <F1>
Jump to subject: <Ctrl>]
Jump back: <Ctrl>o

whoami

Print the user name associated with the current effective user
whoami

adduser/useradd

Both will add users to the system


adduser have more automations than useradd
adduser <username>
Adding user
Adding new group
Creating home directory
Copying files from '/etc/skel'

passwd

The passwd command changes passwords for user accounts.


Report password status on all accounts:
password -a
Delete the password for the named account:
password -d
Force expire the password for the named account:
password -e

2 of 8
Display this help message and exit
password -h
Change password only if expired
password -k
Set password inactive after expiration
password -i

/etc/passwd

www-data :x :33 :33 :www-data :/var/www :/bin/sh


1 2 3 4 5 6 7
1. Username: It is used when user logs in. It should be between 1 and 32 characters in length.
2. Password: An x character indicates that encrypted password is stored in /etc/shadow file.
3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are
reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative
and system accounts/groups.
4. Group ID (GID): The primary group ID (stored in /etc/group file)
5. User ID Info: The comment field. It allow you to add extra information about the users such as user's full
name, phone number etc.
6. Home directory: The absolute path to the directory the user will be in when they log in. If this directory
does not exists then users directory becomes /
7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please
note that it does not have to be a shell.

/etc/shadow

nayanajith "$5$jdRDdIbD$g7i2tM8Yyl3YfZsGIpdXfr7LNT1VfzGy0ipmSPXRzT0 :14895 :0 :99999 :7 : : :


1 2 3 4 5 6 78
1. User name : It is your login name
2. Password: It your encrypted password. The password should be minimum 6-8 characters long including
special characters/digits
3. Last password change (lastchanged): Days since Jan 1, 1970 that password was last changed
4. Minimum: The minimum number of days required between password changes i.e. the number of days left
before the user is allowed to change his/her password
5. Maximum: The maximum number of days the password is valid (after that user is forced to change his/her
password)
6. Warn : The number of days before password is to expire that user is warned that his/her password must be
changed
7. Inactive : The number of days after password expires that account is disabled
8. Expire : days since Jan 1, 1970 that account is disabled i.e. an absolute date specifying when the login
may no longer be used

addgroup

Add a group to the system


addgroup <groupname>

/etc/group

scanner :x :114 :saned,nayanajith


1 2 3 4

3 of 8
1. group_name: It is the name of group. If you run ls -l command, you will see this name printed in the group
field.
2. Password: Generally password is not used, hence it is empty/blank. It can store encrypted password. This
is useful to implement privileged groups.
3. Group ID (GID): Each user must be assigned a group ID. You can see this number in your /etc/passwd file.
4. Group List: It is a list of user names of users who are members of the group. The user names, must be
separated by commas.

delgroup

Delete group from the system

usermod

Changing user's groups


Append the user to the supplemental GROUPS:
usermod <username> -G www-data -a

Super user privileges

In Linux and Unix like computer operating systems, root is the conventional name of the user who has all
rights or permissions (to all files and programs) in all modes (single- or multi-user).
The root user can do many things an ordinary user cannot, such as changing the ownership of files and
binding to ports numbered below 1024. The etymology of the term may be that root is the only user
account with permission to modify the root directory of a Unix system.

su/sudo

Change user:
su -l <username>
Change to super user:
su -l
sudo allows a permitted user to execute a command as the superuser or another user, as specified in the
sudoers file.
Run command as super user:
sudo <command>
Use shell with super user privileges:
sudo -i or sudo bash

/etc/sudores

Edit: visudo or sudoedit /etc/sudores


Enable super user for a user:
<username> ALL=(ALL) ALL
Enable super user for a user w/o password:
<username> ALL=NOPASSWD: ALL

ls/dir

List content of a directory


Long list:
ls -l

4 of 8
All:
ls -A
Sort:
ls -tr

mkdir

Create directories
Create a directory:
mkdir <dir_name>
Create directory hierarchy:
mkdir -p kkk/lll/{mmm,nnn}

touch

Change file timestamps / Create empty files

Create empty file:


touch <file_name>
Change only the access time:
touch -a <file_name>
Change only the modification time:
touch -m <file_name>

cat

Dump content of a file:


cat <file_name>
Concatenate files:
cat <file_name1> <file_name2> > <file_name3>
Redirect into file:
cat > <file_name>
Print all:
cat -A <file_name>
Print with line No.:
cat -n <file_name>
Cat with grep to filter:
cat <file_name> | grep <regexp>

find

Find a files/dirs:
find <dir_name> -iname <file_name_regexp>
Find a files:
find <dir_name> -type f -iname <file_name_regexp>
Find files/dirs modified before 7 days:
find <dir_name> -mtime -7 -iname <file_name_regexp>
Delete what found:
find <dir_name> -mtime -7 -iname <file_name_regexp> -delete
Execute commands on what found:
find <dir_name> -mmin -10 -exec ls -l '{}' \;

5 of 8
cp

Copy file:
cp <source_file> <destination>
Copy directory:
cp -r <source_dir> <destination>
Create hard links instead of copy:
cp -l <source_file> <destination>
See what's copying:
cp -v <source_file> <destination>

mv

Move file:
mv <source_file> <destination>
Rename file:
mv <prev_name> <cur_name>

Introduction to File permissions

1 2 3 4 5 6 7 8 9 10
File User Permissions Group Permissions Other Permissions
Type Read Write Execute Read Write Execute Read Write Execute
d r w x r w x r w x
Description of each character
Character 1 is the type of file: - is ordinary, d is directory, l is link.
Characters 2-4 show owner permissions. Character 2 indicates read permission, character 3
indicates write permission, and character 4 indicates execute permission.
Characters 5-7 show group permissions. Character 5=read, 6=write, 7=execute
Characters 8-10 show permissions for all other users. Character 8=read, 9=write, 10=execute
There are 5 possible characters in the permission fields. They are:
1. r = read - This is only found in the read field.
2. w = write - This is only found in the write field.
3. x = execute - This is only found in the execute field.
4. s = setuid - This is only found in the execute field.
5. If there is a "-" in a particular location, there is no permission. This may be found in any field whether
read, write, or execute field.

id

The Linux id command can be used to print user identity.

Current user's user ID:


id -u
Current user's group ID;
id -g
View all group IDs of the user:
id -G
Print names instead of IDs:
id -n

6 of 8
stat

Display file or file system status

File status:
stat <file_name>
File status formatted output:
stat <file_name> -c <format_string>
File type: %F
Time of last modification: %y
Total size, in bytes: %s
File system status:
stat -f <file_name>
File system status formatted output:
stat -f <file_name> -c <format_string>
File system type: %T
Total data blocks in file system: %b
Total file nodes in file system: %c
Access,Modify,Change
Access Time: This is the time that the file was last accessed, read or written to.
Modify Time: This is the last time the actual contents of the file were last modified.
Change Time: This is the time that the inode information (permissions, name, etc., the metadata, as
it were) was last modified

chmod

Changing File Permissions

Who
u : The user who owns the file (this means “you.†)
g : The group the file belongs to.
o : The other users
a : all of the above (an abbreviation for ugo)
Permissions
r : Permission to read the file.
w : Permission to write the file.
x : Permission to execute the file, or, in the case of a directory, search it.
Change mode to user can write,group can read only:
chmod u+w,g-w <file_name>
Set UID/Set GID (the program will run as the user/group who own file)
chmod u+s <file_name>
chmod g+s <file_name>
Do it by numbers
Binary decimal Permission
000 0 ---

001 1 --x
010 2 -w-

011 3 -wx

100 4 r--
101 5 r-x

7 of 8
110 6 rw-

111 7 rwx
chmod u+w,g-w with numbers:
chmod 644 <file_name>
Set permisions recursively:
chmod 644 -R <dir_name>

chown

Change ownership of a file/directory

Change the owner of <dir_name> to "root".


chown root <dir_name>
Likewise, but also change its group to "staff".
chown root:staff <dir_name>
Change the owner of <dir_name> and subfiles to "root".
chown -R root <dir_name>

8 of 8

You might also like