You are on page 1of 47

#include <linux/ctype.

h>
#include <linux/export.h>
#include <linux/kexec.h>
#include <linux/kmod.h>
#include <linux/kmsg_dump.h>
#include <linux/reboot.h>
#include <linux/suspend.h>
#include <linux/syscalls.h>
#include <linux/syscore_ops.h>
#include <linux/uaccess.h>

/*
* this indicates whether you can reboot with ctrl-alt-del: the default is yes
*/

int C_A_D = 1;
struct pid *cad_pid;
EXPORT_SYMBOL(cad_pid);

#if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)


#define DEFAULT_REBOOT_MODE = REBOOT_HARD
#else
#define DEFAULT_REBOOT_MODE
#endif

1 – Fundamental Linux
enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;

/*
* This variable is used privately to keep track of whether or not
* reboot_type is still set to its default value (i.e., reboot= hasn't
* been set on the command line). This is needed so that we can
* suppress DMI scanning for reboot quirks. Without it, it's
* impossible to override a faulty reboot quirk without recompiling.
*/
int reboot_default = 1;

Boost Camp 2017 int reboot_cpu;


enum reboot_type reboot_type = BOOT_ACPI;
int reboot_force;

/*
* If set, this is used for preparing the system to power off.
*/

void (*pm_power_off_prepare)(void);

/**
* emergency_restart - reboot the system
*
* Without shutting down any hardware or taking any locks
* reboot the system. This is called when we know we are in
* trouble so this is our best effort to reboot. This is
* safe to call in interrupt context.
*/
void emergency_restart(void)
{
kmsg_dump(KMSG_DUMP_EMERG);
machine_emergency_restart();
}
EXPORT_SYMBOL_GPL(emergency_restart);

void kernel_restart_prepare(char *cmd)


EXPCamp17-D1M1-Rev2 { 1
blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTA
system_state = SYSTEM_RESTART
We’re glad you’re here.

EXPCamp17-D1M1-Rev2 2
What is EXPcamp?
• EXPerience camp intends to help you walk in the path
of computer programming
• To give you fundamental concepts, inspirations, and passion in
your field of study.
• To help and assist those with low “flying hours”. Don’t worry.
We’ll be with you.
• A first place for you to socialise with your friends. Smile to ones
sitting nearby you!

EXPCamp17-D1M1-Rev2 3
Your first step Linux

EXPCamp17-D1M1-Rev2 4
What is Linux?
• A free, opensource operating
system.
• First created by Linus Torvalds (on
the right), and maintained by
several people around the world.
• Considered the power behind many
technologies.

EXPCamp17-D1M1-Rev2 5
How do we code?
• We’re remoting into Linux servers.
• The way we’re remoting is called SSH (Secure Shell).
• And we are using command lines.
• Yes, those black screens. Feeling like a hacker now?

EXPCamp17-D1M1-Rev2 6
Remoting into the server
• Using PuTTY to
remote is simple.
• Firstly, fill in your server.

EXPCamp17-D1M1-Rev2 7
Remoting into the server
• Using PuTTY to
remote is simple.
• Firstly, fill in your server.
• Type the username on
that black screen and
enter.
• Type your password (it
won’t show, don’t
worry), and enter again.

EXPCamp17-D1M1-Rev2 8
Getting started on Linux
Commands

EXPCamp17-D1M1-Rev2 9
Change your password.
• First thigs first – change your password.
• Type passwd and press Enter.
• Now follow on-screen instructions.
• Everytime you type your password, it do appears like you have
typed nothing. This is normal.
• And you have done running your first Linux commands.

EXPCamp17-D1M1-Rev2 10
Try some more commands!
Commands to try Its purpose
pwd Print working directory
whoami Print your username
clear Clear the terminal

EXPCamp17-D1M1-Rev2 11
Linux filesystem
• A hierarchical filesystem.
• Files inside folders inside
folders with other files.
• You are used to this.

EXPCamp17-D1M1-Rev2 12
Structure of files and directories

EXPCamp17-D1M1-Rev2 13
Navigating in and out folders
• The main command for
changing directory is cd
• When you start your SSH
session, your are
automatically brought to
your home folder.

EXPCamp17-D1M1-Rev2 14
Navigating in and out folders
• To get list of files and
folders, type ls
• To go into specific folder,
type cd [path]
• But what is path?

EXPCamp17-D1M1-Rev2 15
What is path?
• The way of telling location of files and directories
• Two different ways: relative path and absolute path
• Your home directory is where you save all your works.
• The path for your home directory is ~
• The path for someone’s home directory is ~, followed by their
username (E.g. ~mike is the home of user named mike)
• .. (double dot) means to go up one directory.

EXPCamp17-D1M1-Rev2 16
Relative path and absolute path

Relative path Absolute Path


• Calls file by its relative • Begins with /
location from the • Calls for directories by
directory user is in starting at the root
directory (which, the root
directory is the / itself).

EXPCamp17-D1M1-Rev2 17
Relative path and absolute path

/ home srakrn works test.txt


(Your home)

Blue: You are here Red: Destination file/folder

Relative path: works/test.txt


Absolute path: /home/srakrn/works/test.txt

EXPCamp17-D1M1-Rev2 18
Relative path and absolute path

/ home srakrn works test.txt


(Your home)

Blue: You are here Red: Destination file/folder

Relative path: ../../.. (three steps up)


Absolute path: /

EXPCamp17-D1M1-Rev2 19
Relative path and absolute path

/ home srakrn works test.txt


(Your home)

Blue: You are here Red: Destination file/folder

Relative path: ../.. (two steps up)


Absolute path: ~ or ~srakrn

EXPCamp17-D1M1-Rev2 20
More on Linux commands

EXPCamp17-D1M1-Rev2 21
Structure of a commands
ls -al /var/www/html

EXPCamp17-D1M1-Rev2 22
Structure of a commands
ls -al /var/www/html

The command
Different command do different tasks.
`ls` is for directory listing. Other commands do other jobs.

EXPCamp17-D1M1-Rev2 23
Structure of a commands
ls -al /var/www/html

The options/flags
Begins with the dash, and one alphabet per parameter.
Works like an option to the command.
• a means to list all files, include those hidden files.
• l means to list details of files, not only the name.

(try ls and ls -l to see the difference)

EXPCamp17-D1M1-Rev2 24
Structure of a commands
ls -al /var/www/html

The argument(s)
Some are mandatory, while some are not necessary.

If no path is specified to ls, the programme list for files in the


directory where the user is in.
If argument were given, however, the programme lists for files in
that given directory.

EXPCamp17-D1M1-Rev2 25
File manipulation commands
Commands Tasks
ls [path] List for files and subdirectories in the
specified directory path.
pwd Show the directory you’re in.
cd [path] Change the directory to the one specified.
Change to home when no path were
specified.
mv [source] [destination] Move file from source to destination. Could
be use to rename the file.
cp [source] [destination] Copy file(s) from source to destination.
rm [file] Remove the file. (Delete)

EXPCamp17-D1M1-Rev2 26
File manipulation commands
Commands Tasks
mkdir [foldername] Create a folder as named.
touch Creates an empty file.
cat [file] Show contents of the file
long [file] A better way to show contents of the file
(which is long)

EXPCamp17-D1M1-Rev2 27
Understandness check
• These are questions found in your first pre-test.

• cowquotes
• cowquotes
• home directory
• Can you do it now?

EXPCamp17-D1M1-Rev2 28
File permissions

EXPCamp17-D1M1-Rev2 29
About file permission
• Every file is owned by a user. He is
the one who control the rights to
edit the file.

EXPCamp17-D1M1-Rev2 30
About file permission
• Every file is owned by a user. He is
the one who control the rights to
edit the file.
• You can group users in Linux. File
owner can decide how much can
people in the same group with
him do so something with the
file.

EXPCamp17-D1M1-Rev2 31
About file permission
• Every file is owned by a user. He is
the one who control the rights to
edit the file.
• You can group users in Linux. File
owner can decide how much can
people in the same group with
him do so something with the
file.
• And there are other people who
does not belong to the group. File
owners also can decide how
much these other people can
mess with the file.
EXPCamp17-D1M1-Rev2 32
Introducing permission

• Try running `ls –l`
~ ls -l

total 56

drwxrwxr-x 3 srakrn srakrn 4096 Jul 4 07:50 C


• See that things on the first
-rwxrwxr-x 1 srakrn srakrn 10729 Jul 2 15:15 ezopenvpn.sh

-rwxrwxr-x 1 srakrn srakrn 8424 Jul 6 09:05 helloworld columns? It’s the file
-rw-rw-r-- 1 srakrn srakrn 69 Jul 6 09:05 helloworld.c permission.
drwx------ 2 srakrn srakrn 4096 Jul 2 15:26 openvpn-ca

-rw-r--r-- 1 root root 13266 Jul 2 15:17 ovpn-


srakrn.zip

drwxrwxr-x 3 srakrn srakrn 4096 Jul 9 01:44 transitth

EXPCamp17-D1M1-Rev2 33
Introducing permission
drwxrwxr-x 3 srakrn srakrn 4096 Jul 9 01:44 transitth

drwxrwxrwx
r = read
w = write
x = execute (run)

Letters if allowed. Dash (-) if not allowed.

EXPCamp17-D1M1-Rev2 34
Introducing permission
drwxrwxr-x 3 srakrn srakrn 4096 Jul 9 01:44 transitth

drwxrwxrwx
Directory? Other permission
Group permission

User permission

EXPCamp17-D1M1-Rev2 35
Understandness check
-rwxr-xr-- is the permission code.
Tick the boxes with the permission granted.

Read Write Execute


User (owner)
Group
Other

Also, this is file or folder?

EXPCamp17-D1M1-Rev2 36
Permission number
Without minding the first (directory) letter. For each group of letters.

4 = read
2 = write Sum up the numbers
1 = execute (run)

EXPCamp17-D1M1-Rev2 37
Permission number
4 = read
2 = write
1 = execute (run)

drwxr-xr--

EXPCamp17-D1M1-Rev2 38
Permission number
4 = read
2 = write
1 = execute (run)

drwxr-xr--

4 = read
2 = write 4+2+1 = 7
1 = execute (run)

7
EXPCamp17-D1M1-Rev2 39
Permission number
4 = read
2 = write
1 = execute (run)

drwxr-xr--

4 = read
1 = execute (run) 4+1 = 5

75
EXPCamp17-D1M1-Rev2 40
Permission number
4 = read
2 = write
1 = execute (run)

drwxr-xr--

4 = read

754
EXPCamp17-D1M1-Rev2 41
Permission number
4 = read
2 = write
1 = execute (run)

drwxr-xr-- = 754

EXPCamp17-D1M1-Rev2 42
Permission number
So, where does those 1-7 numbers come from?
It’s a three-digit binary (base 2), converted to decimal (base 10)

rwx = 111 - Convert to binary → 7


r-x = 101 - Convert to binary → 5
r-- = 100 - Convert to binary → 4

EXPCamp17-D1M1-Rev2 43
Changing file permission
chmod [perm] [filename]

This is the 3 digits number mentioned before.

Example

chmod 777 quiz_bank.txt


Question: Considered quiz_bank.txt to be the question for your exam.
This file is somewhere in the server you can access.
Is running this command good (to the instructors team)? Why?

EXPCamp17-D1M1-Rev2 44
Changing file permission
chmod [perm] [filename]
The permission code can be also writed in the form of who what
who means what means
u User r Read
g Group w Write
o Others x Execute

Example

chmod o-rw quiz_bank.txt


Question: You are not the owner of the file quiz_bank.txt.
When the owner execute this command, can it still be read by you?

EXPCamp17-D1M1-Rev2 45
Changing file ownership
chown srakrn:ta quiz_bank.txt
Group

User

Now the file owner is srakrn


and the group of people who own this file is ta.

EXPCamp17-D1M1-Rev2 46
#include <linux/ctype.h>
#include <linux/export.h>
#include <linux/kexec.h>
#include <linux/kmod.h>
#include <linux/kmsg_dump.h>
#include <linux/reboot.h>
#include <linux/suspend.h>
#include <linux/syscalls.h>
#include <linux/syscore_ops.h>
#include <linux/uaccess.h>

/*
* this indicates whether you can reboot with ctrl-alt-del: the default is yes
*/

int C_A_D = 1;
struct pid *cad_pid;
EXPORT_SYMBOL(cad_pid);

#if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)


#define DEFAULT_REBOOT_MODE = REBOOT_HARD
#else
#define DEFAULT_REBOOT_MODE
#endif

1 – Fundamental Linux
enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;

/*
* This variable is used privately to keep track of whether or not
* reboot_type is still set to its default value (i.e., reboot= hasn't
* been set on the command line). This is needed so that we can
* suppress DMI scanning for reboot quirks. Without it, it's
* impossible to override a faulty reboot quirk without recompiling.
*/
int reboot_default = 1;

EXPcamp 2017 int reboot_cpu;


enum reboot_type reboot_type = BOOT_ACPI;
int reboot_force;

/*
* If set, this is used for preparing the system to power off.
*/

void (*pm_power_off_prepare)(void);

/**
* emergency_restart - reboot the system
*
* Without shutting down any hardware or taking any locks
* reboot the system. This is called when we know we are in
* trouble so this is our best effort to reboot. This is
* safe to call in interrupt context.
*/
void emergency_restart(void)
{
kmsg_dump(KMSG_DUMP_EMERG);
machine_emergency_restart();
}
EXPORT_SYMBOL_GPL(emergency_restart);

void kernel_restart_prepare(char *cmd)


EXPCamp17-D1M1-Rev2 { 47
blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTA
system_state = SYSTEM_RESTART

You might also like