Enter The Linux – An Introduction To The Linux Operating System

Linux is a free and open-source operating system that is based on the Unix kernel. It is known for its flexibility and versatility, as the source code is freely available for anyone to modify, use, and redistribute.

The use of the Linux kernel has resulted in a wide range of operating systems, known as Linux distributions, which are used on a variety of devices including personal computers, servers, and smartphones. Linux is widely respected for its security features and is often used in enterprise environments.

Linux TUX

The Linux operating system gets its name from the Linux kernel, which was developed in 1991 by Linus Torvalds. In addition to the kernel, the system also includes a variety of utilities and libraries that are derived from the GNU operating system. The GNU operating system was first announced in 1983 by Richard Stallman, and its contributions to the Linux system have led to the alternate name GNU/Linux being used to refer to the operating system as a whole. It is widely supported by major corporations such as Dell, Hewlett-Packard, IBM, Novell, Oracle Corporation, Red Hat, Sun Microsystems, and even Microsoft. These companies provide support, services, and resources for businesses and individuals using Linux in their operations.

Accessing Linux server remotely from Windows workstation

There are two main ways to remotely access a Linux server: using a terminal application and using an SFTP client. Using a terminal application, such as Putty, is the recommended method for accessing a Linux server, as it allows you to enter commands and interact with the server directly.

To access a Linux server using Putty:

Install and start the Putty application.

putty session
  1. Enter the hostname or IP address of the server you want to access.
  2. Select the “SSH” connection type.
  3. Enter a name for the session (e.g. “myserver1”) and save the session.
  4. Open the saved session to establish a connection to the Linux server.

Accessing a Linux server using WinSCP

WinScp session

Alternatively, you can use an SFTP client, such as WinScp, to transfer files to and from the Linux server. This is a good option if you don’t need to enter commands or interact with the server directly. Simply start the SFTP client, enter the connection details for the Linux server, and you will be able to transfer files to and from the server.

To connect to a Linux server using tools like Putty or WinSCP, you will need to ensure that you have the necessary credentials and access permissions. One thing to keep in mind is that the server may use Secure Shell (SSH) keys for secure identification. If you are connecting to a server for the first time, you may be prompted to accept the server’s SSH key. It is important to accept the key and store it locally on your computer to ensure a secure connection. If you have any issues or questions about SSH keys or connecting to the server, it is always a good idea to reach out to the server administrator or your IT support team for assistance.

General Linux remarks

  • Path separator on Linux is: / (slash, or forward slash)
  • Linux is case-sensitive: always use the correct case: a FileName is not the same as afilename
  • Text files on Linux have a different line ending than text files on windows (Linux: n, Windows: rn). This can cause problems when transferring files: transfer text files as text files, line endings are automatically corrected. Otherwise you get weird characters in your files (^M) or the whole file is on one line
  • Transfer non-text (binary) files as binary files. Otherwise the files may get corrupted!
  • There is no Recycle Bin, think twice before you’ll perform delete action!
  • Everything that its name starts with a . (dot) is a hidden file or directory

1. Some common tasks that can be performed in a terminal session in a Linux include:

  • Changing your password. Do it now!

The passwd command allows you to change your password:
• Enter your old password (when not a root user)
• Enter your required new password
• Enter your required new password again

  • Navigating the file system: You can use the cd command to move between directories, and the ls command to view the contents of a directory.
  • Editing files: You can use a text editor like nano, vi, or emacs to create and edit text files in the terminal.
  • Running programs and scripts: You can run programs and scripts by typing their names at the command prompt and pressing Enter.
  • Installing and updating software: You can use package management tools like apt or yum to install new software and update existing software.
  • Configuring the system: You can use various commands to view and modify system settings, such as the hostname, networking settings, and environment variables.
  • Managing system processes: You can use commands like ps, top, and kill to view and control processes running on the system.
  • Transferring files: You can use tools like scp and sftp to transfer files between systems.

2. Commands to navigate the file system

Look at directory contentsls
Change directorycd
Find filesfind
Copy and move filescp and mv
Remove filesrm
Create directoriesmkdir
Remove directoriesrmdir (dir must be empty)

Viewing and search in files:

To determine the type of a filefile
Dump a file to the screencat
Page thru a fileless
Search in a filegrep
Tail a filetail
Count words or lines in a filewc or wc –l
View activity on the serverps and top

The ls commands lets you retrieve the contents of a directory


ls Shows directory contents
ls -a Shows all directory contents (including hidden files starting with a dot
ls -l Shows directory contents as a list with extra info
ls -la Shows all directory contents as list
ls -lh Shows directory contents as list, sizes in a readable format (Kb Mb)
ls -lt Shows directory contents as list, ordered by file-change time
ls -ltr Shows directory contents as list, ordered by file-change time (reversed, last changed file at the

Example:

$ls -la
drwxr-xr-x 1 file_owner owners_group 946 2010-11-11 11:11 filename

Fields explanation

drwxr-xr-x 1 user1 users 946 2010-11-11 11:11 filename
PermissionsNR of Links, DirectoriesOwnerGroupFile SizeDate ModifiedTime ModifiedFile name
d rwx r-x r--1user1users9462010-11-1111:11file-name

Permissions determine who is allowed to access and perform certain actions on a file or directory.
There are three types of permissions:

r = read permission
w = write permission
x = execute permission
- = no permission

Permissions – Special indicators:
d
= directory
- = regular file
l = symbolic link
s = UNIX domain socket
p = named pipe
c = character device file
b = block device file

Examples:

rwx r-x r-- <file-name>

owner: can read, write and execute the file rwx
group: can only read and execute the file r-x
others: can only read the file r--

dr-x --- --- <directory-name>
owner has read and execute access r-x (x allows directory browsing)
group and others have no access to the directory --- ---

Directory commands

  • The cd command lets you switch directories.
cd without a parameter leads you to your home dir
cd ~ (with tilde) does the same
pwd shows the current directory
mkdir lets you create directories
rmdir lets you remove a directory (dir must be empty)
  • Command: find

The find command lets you find files

find . – name somename Find file with name somename starting from current directory
find /dir – name ‘some*’ Find file with name starting some with some starting from /dir. Use the quotes!
find . – iname somename Find file with name somename starting from current directory, ignoring case of the filename
  • Commands: cp and mv

The cp command lets you copy files and directories

cp srcfile tgtfile Copy file ‘srcfile’ to file ‘tgtfile’
cp *.log target_dir Copy all logfiles to directory ‘target_dir’
cp one.log two.log target_dir Copy ‘one.log’ and ‘two.log’ to directory ‘target_dir’
cp –R source_dir target_dir Copy source_dir and all its content to directory target_dir’
cp –R source_dir/* target_dir Copy the content of source_dir to directory ‘target_dir’

• The mv command lets you move (and rename) files and directories

mv srcfile tgtfile Move (rename) file ‘srcfile’ to file ‘tgtfile’
mv *.log target_dir Move all logfiles to directory ‘target_dir’
mv one.log two.log target_dir Move ‘one.log’ and ‘two.log’ to directory ‘target_dir’
mv –R source_dir target_dir Move source_dir and all its content to directory ‘target_dir’
mv –R source_dir/* target_dir Move the content of source_dir to directory ‘target_dir’
  • Command: rm

The rm command lets you delete files

rm logfile.log Delete a file.
rm –rf /dir_1 Deletes all files and directories recursively in directory /dir_1 including. (Attention! Never perform something like this: rm –rf / ).
  • Commands: tar & gzip – Packing & unpacking files

The tar command lets you archiving files and directories into a single file, it does not compress them

tar –cvf logs.tar *.log Archive all log files into logs.tar (no compression yet)
tar –xvf logs.tar Unarchive all files from logs.tar

• The gzip command compresses a file

gzip logs.tar Compresses logs.tar to logs.tar.gz (removing logs.tar)
gunzip logs.tar.gz Decompresses logs.tar.gz to logs.tar (removing logs.tar.gz)

• Combined (you could combine tar with gzip in one command)

tar –cvzf logs.tar.gz *.log Packs and compresses all log files into logs.tar.gz
tar –xvzf logs.tar.gz Decompresses and unpacks all files from logs.tar.gz
  • File viewing commands

less – shows the content on your screen and allows you to page through it
grep – searches a file for a string
tail – shows the end of a file
wc – counts words or lines in a file
file – displays the type of a file
cat – dumps the contents of a file on your screen

  • Command: less

The less command shows the content on your screen and allows you to page through it

$ less filename

Shows the file content on the screen (scroll using cursors)

– Keys (use thise keys while browsing the file):
space Next page
b (char) Previous page
/ Search forwards
? Search backwards
q Quits

  • Command: grep

The grep command searches for strings in files

grep error logfile.log Searches for the string ‘error’ in ‘logfile.log’
grep –i error logfile.log Searches case-insensitive for the string ‘error’ in ‘logfile.log’ (so finds Error, eRror and error)
grep error *.log Searches for the string ‘error’ in all files ending with .log in the current directory
  • Command: tail

The tail command shows the last part of a file

tail -n 50 logfile.log Shows the last 50 lines of the file ‘logfile.log’
tail -f logfile.log Follows the end of ‘logfile.log’, every line that is appended by another process is shown on your screen (very handy tracking files in real time)
  • Command: wc

The wc command counts words or lines in a file

wc -l logfile.log – Count the number of lines in the file ‘logfile.log’

  • Command: ps

The ps command shows a snapshot of current processes

For continuous information: use top

ps Show all processes for the current user at the current terminal
ps -e Show all processes on the system PID, TTY, TIME and CMD
ps -ef Show all processes on the system with extended info UID, PID, PPID, C, STIME, TTY, TIME and CMD
ps -efH Show all processes on the system with extended info as a tree UID, PID, PPID, C, STIME, TTY, TIME and CMD
ps auxf it’s also cool display

UID The user id
PID The process id
PPID The parent process id
C Percentage CPU usage
STIME Starting time of the process. If the elapsed time is > 24 hours, the starting date
TTY The terminal that started the process
TIME The CPU time used
CMD The executing command

  • Command: top

The top command displays active system tasks

Example of output: screen with a header and top-active tasks

$ top
09:49:31 up 41 days, 16:26, 4 users, load average: 0.21, 0.27, 0.23 Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie Cpu(s): 0.9%us, 0.1%sy, 5.3%ni, 93.5%id, 0.1%wa, 0.0%hi, 0.1%si, 0.0%st Mem: 8309568k total, 8215884k used, 93684k free, 233920k buffers Swap: 1052248k total, 70764k used, 981484k free, 504504k cached

us: user cpu time

sy: system cpu time
ni: user nice cpu time
id: idle cpu time
wa: io wait cpu time
hi: hardware irq (servicing hardware interrupts)
si: software irq (servicing software interrupts)
st: steal time (time in involuntary wait by virtual cpu while hypervisor is servicing another processor)

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1946 user1 16 0 3320 1056 792 R 1 0.0 0:00.24 top
1410 root 18 0 1249m 180m 13m S 23 5.9 0:31.85 rm -rf /

Columns explained:

PID The process id
USER The user executing the process
PR Priority value
NI Nice value
VIRT Amount of virtual memory used by a task
RES The non-swapped physical memory used by a task
SHR The amount of shared memory used by a task
S Status: D = Uninterruptable sleep, R = Running, S = Sleeping, T = traced or stopped, Z = zombie
%CPU Percentage of CPU used
%MEM Percentage of physical memory used by a task
TIME+ CPU time used by this task since started
COMMAND The command that is being executed in the process
  • Combining commands together

A lot of small commands that all do a single thing combined together they are much more powerful “|” a vertical pipe mark is being used

Examples:

Count the number of lines in logfile.log that contain the string ‘error’

grep error logfile.log | wc –l

Count the number of logfiles

ls –l *.log | wc -l

View a paginated list of logfiles

ls –l *.log | less

• Tail a logfile only showing lines that contain the string ‘error’

tail –f logfile.log | grep error

Gets a list of active processes Filter lines that contain the string ‘java’ Count the lines

ps –ef | grep –I java | wc -l

Executing programs

• If the program is in the PATH: just enter the program name

# ifconfig

• If  the program is not in the path: enter full path + program name

# /sbin/ifconfig

• The current directory is NEVER in the path, use: ./

# cd /sbin
# ./ifconfig

• Shell scripts that are executable: just execute as a normal program

# cd /home/myscripts
# ls -la
-rwxr-xr-x 1 user69 users 0 Nov 11 16:33 myscript.sh

# ./myscript.sh

• Shell scripts that are not executable, use: sh 

# cd /home/myscripts
# ls -la
-rw-r--r-- 1 user69 users 0 Nov 11 16:33 myscript.sh

# sh myscript.sh

• Check whether the program is in your $PATH

# which ifconfig
/sbin/ifconfig
  • Other useful stuff

ctrl+c cancels a running program (most of the time)
ctrl+d ends a session (as does the exit command)
screen: (must be installed) Use multiple screens, and keep sessions alive between (dis)connects

while in screen mode:

ctrl+a c create a new screen (taping order: ctrl+a then release ctrl+a and press “c” button separate)
ctrl+a switch to screen (keyboard’s numbers)
screen -wipe shows actual screens
screen -x connects to detached screen
ctrl+d end a screen
ctrl+a esc scroll through a screen (esc ends scrolling)

man : Asks for the manual page of a command / program e.g. man cp
which : find out the path of the command
tab key auto completion

For instance:
taping “cd /home/userone” could be written much faster with help of tab key functionality:

cd /ho[press tab]/use[press tab] – observe that the right existing path will be auto filled up – very handy! use it!

Short Description of the Linux file structure

Filesystems

Description

root “/”

Basic operating system and maintenance tools. The content of this file system should be sufficient to start up the system and perform emergency maintenance and repairs if they were necessary.

 

The parts of the root file system are:

/bin

–executables (binaries) needed during bootup that might be used by normal users.

/sbin

–executables (system binaries) not intended for use by general users (users may still use them, but this directory is not on their PATH).

/etc

–system-wide configuration files for your operating system.

/root

–the home directory of the system administrator (called super-user or root).

/dev

–device files. Devices appear on Linux as files so that hardware is abstracted and it is easy to write to them or read from them.

/mnt

–mount points for removable media (floppy, cdrom, zip drive), partitions of other operating systems (e.g. MS Windows), network shares, and anything else that is mounted on the file system temporarily. It normally contains a separate subdirectory for each mounting share. The contents of these drives/shares appear in these subdirectories–there are no drive letters on Linux.

/lib

–shared libraries for programs that reside on the root filesystem and kernel modules.

/boot

–files used by the bootstrap loader (LILO or GRUB), the thing that loads first when the computer is booted and perhaps gives you the option of which operating system to boot, if you have more than one OS on your computer). It typically also contains the Linux kernel (compressed, file vmlinuz), but this can be stored somewhere else, only if LILO/GRUB is configured to know where it is.

/opt

–optional large applications, for example kde under RedHat 5.2 (under RedHat 6.0, kde is distributed as any other X-windows distribution, main executables are in the /usr/bin directory).

/tmp

–temporary files. This directory may clean up automatically.

/lost+found

–files recovered during the filesystem repair.

/usr

All commands, libraries, documentation, and other files that do not change during normal operation. This will also contain major applications that come with your Linux distribution, for example Netscape.

/var

Files that change: spool directories, log files, lock files, temporary files, and formatted (on use) manual pages.

/home

User files (users’ own settings, customization files, documents, data, mail, caches, etc). The contents of this directory should be preserved on an operating system upgrade. ~ (tilde) Is the alias for your home directory ex. cd ~ or cd will lead you to your home directory instead cd /home/your_user

/proc

Entirely illusionary files. They do not really exist on the disk and do not take up any space there (although ls -l will show their size). When viewing them, you really access information stored in the memory. It is used to access information about the system.

 

Pro Tip:

MobaXterm is an enhanced terminal application for Windows that provides a wide range of tools and features to help you work with your servers and networks. It includes an X11 server, a tabbed SSH client, and a variety of network tools and utilities. It is a powerful and versatile tool that is perfect for system administrators and IT professionals. You can download MobaXterm here.