Linux Basic Usage
Linux Basic Usage
useradd -D
When invoked without the -D option, the useradd command creates a new user
account using the values specified on the command line plus the default values from
the system.
Usermod
Modifies a user account.
The usermod command is used by an administrator to change a user's system
account settings
usermod [options] LOGIN
Example
usermod -d /home/exampleusernew exampleuser
Userdel
The userdel command deletes a user account and all associated files.
The userdel command modifies the system account files, deleting all entries
that refer to the user name LOGIN. The named user must exist.
userdel syntax
Now lets set a password for this user. The 'passwd' command would be helpful in this case :
$ sudo passwd nixUser
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
groupadd
1
Creates a new group.
The groupadd command creates a new groupaccount using the values specified
on the command line plus the default values from the system. The new group will
be entered into the system files as needed.
groupadd syntax
KDE
KDE’s default settings keep things simple with one toolbar at the bottom of the
screen and a single main menu. Its initial color scheme favors blue and grey.
2
Basic Desktop Operations
OR
Explain basic general-purpose utility commands in LOS?
When you first start editing a file with the vi editor you will be in vi command mode.
In this mode you can issue many vi commands, including commands like insert,
append, and delete, and other search and navigation commands that let you move
around your file.
In command mode you cant insert text immediately. You first need to issue an insert,
append, or open command to insert text.
INSERT MODE
3
Once you issue a vi insert, append, or open command, you will be in vi insert mode.
If
You’re working with a modern vi or vim implementation, your vi editor is typically
configured to show the current mode of operation, so when you go into insert mode,
you will see a text string like this on the last line of your vi editor window:
-- INSERT --
At this point you can (a) type text into your file and (b) use the arrow keys to
navigate
around your file just as you would do with any other text editor.
LAST LINE MODE
The last vi mode is known as vi last line mode. You can only get to last line mode
from
command mode, and you get into last line mode by pressing the colon key, like this:
:
After pressing this key, you will see a colon character appear at the beginning of the
last line of your vi editor window, and your cursor will be moved to that position.
This indicates that vi is ready for you to type in a “last line command”.
You can do simple things from this command mode like quitting your vi session, like
this:
:q
or this:
:q!
or this:
:wq
From last line more you can also perform some amazing vi that tells vi to show lines
numbers in your current editor window:
:set shownumber
NAVIGATION THROUGH vi
1. Line navigation
2. Screen navigation
3. Word navigation
4. Special navigation
5. Paragraph navigation
6. Search navigation
7. Code navigation
8. Navigation from command line
4
j – navigate downwards
l – navigate right side
h – navigate left side
By using the repeat factor in VIM we can do this operation for N times. For example,
when
you want to
go down by 10 lines, then type “10j”.
Within a line if you want to navigate to different position, you have 4 other options.
0 – go to the starting of the current line.
^ – go to the first non blank character of the line.
$ – go to the end of the current line.
g_ – go to the last non blank character of the line.
2. Vim Screen Navigation
Following are the three navigation which can be done in relation to text shown in the
screen.
H – Go to the first line of current screen.
M – Go to the middle line of current screen.
L – Go to the last line of current screen.
ctrl+f – Jump forward one full screen.
ctrl+b – Jump backwards one full screen
ctrl+d – Jump forward (down) a half screen
ctrl+u – Jump back (up) one half screen
3. Vim Special Navigation
You may want to do some special navigation inside a file, which are:
N% – Go to the Nth percentage line of the file.
NG – Go to the Nth line of the file.
G – Go to the end of the file.
`” – Go to the position where you were in NORMAL MODE while last closing the
file.
`^ – Go to the position where you were in INSERT MODE while last closing the
file.
g – Go to the beginning of the file.
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
e – go to the end of the current word.
E – go to the end of the current WORD.
b – go to the previous (before) word.
B – go to the previous (before) WORD.
w – go to the next word.
W – go to the next WORD.
5
space.
word – word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
192.168.1.1 – single WORD
192.168.1.1 – seven words.
5. Vim Paragraph Navigation
{ – Go to the beginning of the current paragraph. By pressing { again and again
move to the previous paragraph beginnings.
} – Go to the end of the current paragraph. By pressing } again and again
move to
the next paragraph end, and again.
6. Vim Search Navigation
/i – Search for a pattern which will you take you to the next occurrence of it.
?i – Search for a pattern which will you take you to the previous occurrence
of it.
* – Go to the next occurrence of the current word under the cursor.
# – Go to the previous occurrence of the current word under the cursor.
7. Vim Code Navigation
% – Go to the matching braces, or parenthesis inside code.
8. Vim Navigation from Command Line
Vim +N filename: Go to the Nth line of the file after opening it.
vim +10 /etc/passwd
Vim +/pattern filename: Go to the particular pattern’s line inside the file, first
occurrence
from first. In the following example, it will open the README file and jump to the
first
occurrence of the word “install”.
vim +/install README
Vim +?patten filename: Go to the particular pattern’s line inside the file, first
occurrence
from last. In the following example, it will open the README file and jump to the last
occurrence of the word “bug”.
vim +?bug README
Gedit
File System
In computing, a file system (or filesystem) is used to control how data is stored and
retrieved. Taking its name from the way paper-based information systems are named, each
group of data is called a "file". The structure and logic rules used to manage the groups of
information and their names is called a "file system"
6
File system Architecture
1. / – Root
Every single file and directory starts from the root directory.
Only root user has write privilege under this directory.
Please note that /root is root user’s home directory, which is not same as /.
7
4. /etc – Configuration Files
Contains configuration files required by all programs.
This also contains startup and shutdown shell scripts used to start/stop individual programs.
For example: /etc/resolv.conf, /etc/logrotate.conf
8
File Types
9
On the other hand, symbolic links contain a pointer, or pathname, to the original file. If the
original file is deleted, its data can no longer be accessed using the symbolic link, and the
link is then considered to be a stale link.
Named Pipes
Named pipes are tools that allow two or more system processes to communicate
with each other using a file that acts as a pipe between them. This type of
communication is known as interprocess communication, or IPC for short.
Sockets
Sockets are also tools used for interprocess communication. The difference between sockets
and pipes is that sockets will facilitate communication between processes running on
different systems, or over the network.
With so many different types of files, it's often wise to identify a file's type before
performing any operation with it. The ls -l command and the file command are useful for
determining file types.
Consider the long listing of the livefirelabs1 file:
The first character of the first field indicates the file type. In this example, the first character
is a - (hyphen) indicating that livefirelabs1 is an ordinary or regular file.
The first character of the first field is the letter l indicating live1 is a symbolic link.
File Attributes
Type:
Whether ordinary, directory, device, etc.
Permissions:
Determines who can read, write or execute a file.
Links:
Number of hard links to the file. A number of files in the file system can actully reference
the same file on the drive.
Owner:
A file is owned by a user, by default its creator. The owner can change many file attributes
and set the permissions.
Group Owner:
The group which owns the file. The owner by default belongs to this group.
10
File Size:
Number of bytes of data contained.
Shell As Interpreter
It is a command language interpreter that executes commands read from the standard input
device such as keyboard or from a file. The shell gets started when you log in or open a
console (terminal). The shell is not part of system kernel, but uses the system kernel to
execute programs, create files etc.
[In computing, a shell is a user interface for access to an operating system's services.]
Types of Shell
B shell - /bin/sh – This is the default Unix shell for many Unix operating systems .
Bourne shell was written by S. R. Bourne and its more emphasis is to use it as a scripting
language rather than an interactive shell .
C-shell /bin/csh was designed to provide the interactive features lacking in b shell such as
job control and aliasing .
K shell /bin/ksh – was created by David Korn and has features of both B shell and C shell
along with some additional features . It also includes the ability for developers to create new
shell commands as the need arises.
Bash – the Bourne again shell was developed by GNU project .It is based on B shell language
and has features of C and K shells.
tcsh is the default shell of FreeBSD and its descendants. Essentially it is C shell with
programmable command line completion, command-line editing, and a few other features.
Zsh is a shell designed for interactive use and it has many of the useful features of bash, ksh,
and tcsh along with many new features.
11
To find your current shell type following command
$ echo $SHELL
Lines 2 - 5 are output from running the command. Most commands produce output and it
will be listed straight under the issuing of the command. Other commands just perform their
task and don't display any information unless there was an error.
Line 6 presents us with a prompt again. After the command has run and the terminal is
ready for you to enter another command the prompt will be displayed. If no prompt is
displayed then the command may still be running (you will learn later how to deal with this).
Another Example-
user@Linux-003 ~ $
For this particular case following code means:
Somebody with user name "user" has logged in to the machine with host name "Linux-003".
"~" - represent the home folder of the user, conventionally it would be /home/user/, where
"user" is the user name can be anything like /home/johnsmith.
"$" - is just a sign of the shell prompt, means that shell is ready to accept commands, you
can understand it as a separator after which, you can interact with a shell. Can also be "#"
which shows that root is the user who's session is going on.
12
Running Commands and getting Help
Basic Commands
1. ls
The ls command - the list command - functions in the Linux terminal to show all of the major
directories filed under a given file system. For example, the command:
ls /applications
...will show the user all of the folders stored in the overall applications folder.
The ls command is used for viewing files, folders and directories.
2. cd
The cd command - change directory - will allow the user to change between file directories. As the
name command name suggest, you would use the cd command to circulate between two different
directories. For example, if you wanted to change from the home directory to the Arora directory,
you would input the following command:
cd/arora/applications
As you might have noted, the path name listed lists in reverse order.
Logically cd/arora/applications reads change to the arora directory which is stored in the
applications directory. All Linux commands follow a logical path.
3. mv
The mv command - move - allows a user to move a file to another folder or directory. Just like
dragging a file located on a PC desktop to a folder stored within the "Documents" folder, the mv
command functions in the same manner. An example of the mv command is:
mv/arora/applications/majorapps /arora/applications/minorapps
The first part of the command mv/arora/applications/majorapps lists the application to be moved. In
this case, arora. The second part of the command /arora/applications/minorapps lists where arora
will be moved to - from majorapps to minorapps.
4. man
The man command - the manual command - is used to show the manual of the inputted command.
Just like a film on the nature of film, the man command is the meta command of the Linux CLI.
Inputting the man command will show you all information about the command you are using. An
example:
man cd
The inputting command will show the manual or all relevant information for the change directory
command.
5. mkdir
13
The mkdir - make directory - command allows the user to make a new directory. Just like making a
new directory within a PC or Mac desktop environment, the mkdir command makes new directories
in a Linux environment. An example of the mkdir command
mkdir testdirectory
6. rmdir
The rmdir - remove directory - command allows the user to remove an existing command using the
Linux CLI. An example of the rmdir command:
rmdir testdirectory
7. touch
The touch command - a.k.a. the make file command - allows users to make files using the Linux CLI.
Just as the mkdir command makes directories, the touch command makes files. Just as you would
make a .doc or a .txt using a PC desktop, the touch command makes empty files. An example of the
touch command:
touch testfile.txt
8. rm
The rm command - remove - like the rmdir command is meant to remove files from your Linux OS.
Whereas the rmdir command will remove directories and files held within, the rm command will
delete created files. An example of the rm command:
rm testfile.txt
The aforementioned command removed testfile.txt. Interestingly, whereas the rmdir command will
only delete an empty directory, the rm command will remove both files and directories with files in
it. This said, the rm command carries more weight than the rmdir command and should be used with
more specificity.
9. locate
The locate - a.k.a. find - command is meant to find a file within the Linux OS. If you don't know the
name of a certain file or you aren't sure where the file is saved and stored, the locate command
comes in handy. A locate command example:
locate -i *red*house**city*
14
The stated command will locate an file with the a file name containing "Red", "House" and "City". A
note on the input: the use of "-i" tells the system to search for a file unspecific of capitalization
(Linux functions in lower case). The use of the asterik "*" signifies searching for a wildcard. A
wildcard tells the system to pull any and all files containing the search criteria.
By specifying -i with wildcards, the locate CLI command will pull back all files containing your search
criteria effectivley casting the widest search net the system will allow.
10. clear
The clear command does exactly what it says. When your Linux CLI gets all mucked up with various
eadouts and information, the clear command clears the screen and wipes the board clean. Using the
clear command will take the user back to the start prompt of whatever directory you are currently
operating in. To use the clear command simply type clear.
Cd
rm
find
pwd
mkdir
rmdir
cp
man
cat
Locating Files
find
The starting point is the folder where you want to start searching from. To start searching the whole
drive you would type the following:
find /
If however, you want to start searching for the folder you are currently in then you can use the
following syntax:
find .
Generally, when searching you will want to search by name, therefore, to search for a file
called myresume.odt across the whole drive you would use the following syntax:
15
find / -name myresume.odt
The first part of the find command is obviously the word find.
If you want to search for the current file system you can use a full stop as follows:
The above command will look for a file or folder called the game in all folders under the current folder.
You can find the name of the current folder using the pwd command.
If you want to search the entire file system then you need to start at the root folder as follows:
The starting position can be literally anywhere on your file system. For example to search for
the home folder type the following:
The tilde is a metacharacter commonly used for denoting the home folder of the current user.
Expressions
The -name expression lets you search for the name of a file or folder.
16
-mtime n - file's data was last modified n days ago
-name name - search for a file with the specified name
-newer name - search for a file edited more recently than the file given
-nogroup - search for a file with no group id
-nouser - search for a file with no user attached to it
-path path - search for a path
-readable - find files which are readable
-regex pattern - search for files matching a regular expression
-type type - search for a particular type
-uid uid - files numeric user id is the same as uid
-user name - file is owned by user specified
-writable - search for files that can be written to
If you want to find all the empty files and folders in your system use the following command:
find / -empty
If you want to find all of the executable files on your computer use the following command:
find / -exec
To find all of the files that are readable use the following command:
find / -read
Patterns
When you search for a file you can use a pattern. For example, maybe you are searching for all files
with the extension mp3.
Time
Linux stores time data about access times, modification times, and change times.
Access Time: Last time a file was read or written to.
Modification Time: Last time the contents of the file were modified.
Change Time: Last time the file's inode meta-data was changed.
We can use these with the "-atime", "-mtime", and "-ctime" parameters. These can use the
plus and minus symbols to specify greater than or less than, like we did with size.
The value of this parameter specifies how many days ago you'd like to search.
To find files that have a modification time of a day ago, type:
find / -mtime 1
If we want files that were accessed in less than a day ago, we can type:
find / -atime -1
To get files that last had their meta information changed more than 3 days ago, type:
find / -ctime +3
There are also some companion parameters we can use to specify minutes instead of days:
find / -mmin -1
17
This will give the files that have been modified type the system in the last minute.
Find can also do comparisons against a reference file and return those that are newer:
find / -newer myfile
Finding by Owner and Permissions
You can also search for files by the file owner or group owner.
You do this by using the "-user" and "-group" parameters respectively. Find a file that is
owned by the "syslog" user by entering:
find / -user syslog
Similarly, we can specify files owned by the "shadow" group by typing:
find / -group shadow
We can also search for files with specific permissions.
If we want to match an exact set of permissions, we use this form:
find / -perm 644
This will match files with exactly the permissions specified
LOCATE
To find files with locate, simply use this syntax:
locate query
You can filter the output in some ways.
For instance, to only return files containing the query itself, instead of returning every file
that has the query in the directories leading to it, you can use the "-b" for only searching the
"basename":
locate -b query
Permission Groups
Each file and directory has three user based permission groups:
owner - The Owner permissions apply only the owner of the file or
directory, they will not impact the actions of other users.
group - The Group permissions apply only to the group that has been
assigned to the file or directory, they will not effect the actions of other
users.
all users - The All Users permissions apply to all other users on the system,
this is the permission group that you want to watch the most.
Permission Types
Each file or directory has three basic permission types:
r=4
w=2
x=1
19
You add the numbers to get the integer/number representing the permissions you
wish to set. You will need to include the binary permissions for each of the three
permission groups.
So to set a file to permissions on file1 to read _rwxr_____, you would enter chmod
740 file1.
Owners and groups
You use the chown command to change owner and group assignments, the syntax is
simplechown owner:group filename, so to change the owner of file1 to user1 and the group
to family you would enter chown user1:family file1.
20