NetAcad - NDG Linux Unhatched

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

NDG Linux Unhatched

CLI (Command Line Interface) basics


Directory structure

 “ls” list folders/files in current working directory


o “ ls -l “ long list (detailed list)
o “ ls -r” alphabetically reverse list
o “ ls -l /var/log”
 “pwd” print working directory
 “cd” change directory
o “ cd / ” change directory to main root (~=main root)
o “ cd ~ ” change directory to /home/sysadmin (~=home)
o “ cd .. ” change directory to upper branch
Listing files
 File Type

 - rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log


 d rwxr-x--- 2 root adm 4096 Dec 20 2017 apache2

The first field actually contains ten characters, where the first character indicates the type of
file and the next nine specify permissions. The file types are:

Symbo
l File Type Description

d directory A file used to store other files.

- regular file Includes readable files, images files, binary


files, and compressed files.

l symbolic Points to another file.


link

s socket Allows for communication between processes.

p pipe Allows for communication between processes.

b block file Used to communicate with hardware.

c character Used to communicate with hardware.


file
The first file alternatives.log is a regular file -, while the second file apache2 is a
directory d.

 Permissions

d rwxr-xr-x 2 root root 4096 Apr 11 2014 upstart

Permissions indicate how certain users can access a file. Keep reading to learn more about
permissions.
 Hard Link Count

-rw-r----- 1 syslog adm 1346 Oct 2 22:17 auth.log

This number indicates how many hard links point to this file. Hard links are beyond the scope
of this module, but are covered in the NDG Linux Essentials course.
 User Owner

-rw-r----- 1 syslog adm 106 Oct 2 19:57 kern.log


User syslog owns this file. Every time a file is created, the ownership is automatically
assigned to the user who created it.

 Group Owner

-rw-rw-r-- 1 root utmp 292584 Oct 2 19:57 lastlog

Indicates which group owns this file


 File Size

-rw-r----- 1 syslog adm 19573 Oct 2 22:57 syslog

Directories and larger files may be shown in kilobytes since displaying their size in bytes
would present a very large number. Therefore, in the case of a directory, it might actually be
a multiple of the block size used for the file system. Block size is the size of a series of data
stored in the filesystem.
 Timestamp

drwxr-xr-x 2 root root 4096 Dec 7 2017 fsck

This indicates the time that the file's contents were last modified.
 Filename

-rw-r--r-- 1 root root 47816 Dec 7 2017 bootstrap.log

The final field contains the name of the file or directory.


Administrative access
su command
The su command allows you to temporarily act as a different user. It does this by creating a new
shell. The shell is simply a text input console that lets you type in commands. By default, if a user
account is not specified, the su command will open a new shell as the root user, which provides
administrative privileges.

-bash: /usr/bin/XXXXX: Permission Denied (admin access denied)


sysadmin@localhost:~$ su - // su - // su –login
Password:
root@localhost:~$ (admin access allowed)
root@localhost:~$ exit (admin access exited)

sudo command
The sudo command allows a user to execute a command as another user without creating a new
shell. Instead, to execute a command with administrative privileges, use it as an argument to
the sudo command. Like the su command, the sudo command assumes by default the root user
account should be used to execute commands.

sudo help
Permissions
Permission types
read (r) Allows for file contents to be read or Without execute permission on the directory, allows for a no
copied. execute permission, ls -l can provide a detailed listing.

write (w) Allows for contents to be modified or For this permission to work, the directory must also have ex
overwritten. Allows for files to be
added or removed from a directory.

execute (x) Allows for a file to be run as a process, Allows a user to change to the directory if parent directories
although script files require read well.
permission, as well.

Changing file permissions


Change modes of access: chmod

The Symbolic Method


chmod [<SET><ACTION><PERMISSIONS>]... FILE

To use the symbolic method of chmod first indicate which set of permissions is being changed:

chmod [ <SET> <ACTION><PERMISSIONS>]... FILE

u User: The user who owns the file.

g Group: The group who owns the file.

o Others: Anyone other than the user owner or member of the group owner.

a All: Refers to the user, group and others.


Next, specify an action symbol:

chmod [<SET> <ACTION> <PERMISSIONS>]... FILE

+ Add the permission, if necessary

= Specify the exact permission

- Remove the permission, if necessary

chmod [<SET><ACTION> <PERMISSIONS> ]... FILE


r read

w write

x execute

./ hello.sh

This indicates the “command” should be run from the current directory.

Changing file ownership


chown [OPTIONS] [OWNER] FILE

In the following example, the file hello.sh ownership is changed from sysadmin to root. Once done, it can
only be executed with
Viewing files
cat [OPTIONS] [FILE] //cat =~ concatenate

The cat command displays all five lines of the file above. When viewing larger files,
the cat command can result in very lengthy output that cannot be paused to scroll through. A better
method of viewing long text files, is with a pager command which has a functionality that can pause
and scroll through the output of the file.

You might also like