Linux Commands
Linux Commands
All commands are in simple
[Ctrl] + [Atl] + F2 F2 to F6 virtual consoles
[Ctrl] + [Atl] + F1 GUI mode
[Ctrl] + d log out from the system
[Ctrl] +l clear screen
# vim /boot/grub/grub.conf grub
# vim /etc/grub.conf
# vim /etc/inittab run levels
The runlevels used are:
0 - halt (Do NOT set initdefault to this)
1 - Single user mode
2 - Multiuser, without NFS (The same as 3, if you do not have networking)
3 - Full multiuser mode multi-user.target
4 - unused
5 - X11 / GUI graphical.target
6 - reboot (Do NOT set initdefault to this)
# init [run level]
Change default run level:
systemctl set-default multi-user.target;
systemctl set-default graphical.target;
To switch from graphical to multi-user:
systemctl isolate multi-user.target;
To switch from multi-user to graphical:
systemctl isolate graphical.target;
Eg:
# chkconfig httpd on
# chkconfig - -level 235 httpd [on / off]
------------------------------------------------
Manuals
$ man [command]
$ man ls
: /[search pattern]
: n /f next page
Dumidu Senanayake Page 1 of 10
Linux Commands
:p/b previous page
------------------------------------------------
# cal view the Calendar
# cal [month] [year] # cal 2016 # cal 5 2016
# date view the current date and time
# history view recently used commands
# !3 execute the 3rd command in the history
# history 5 Show the last five commands from the history list
# !! Execute the last command again
# !-5 Execute the fifth command from the bottom of the history list
# !ls Execute the most recent ls command
------------------------------------------------
# [variable name] = [value] BASH shell variables
# HISTSIZE=500
# echo $[variable name] View the variable
# echo $HISTSIZE
------------------------------------------------
“which” command to display the full path to the command
# which date /bin/date
# which cal /usr/bin/cal
------------------------------------------------
Alias # alias mycal="cal 2014"
# mycal
------------------------------------------------
# echo /etc/t* /etc/terminfo /etc/timezone
Shell
# chsh change shell
# chsh –l list of shell programs
# echo $SHELL view the current used shell
------------------------------------------------
# command [option] [parameter]
# shutdown –h now shutdown commands
# power off
# halt
# shutdown –r now restart
# reboot
Dumidu Senanayake Page 2 of 10
Linux Commands
#w current users in the system
# whoami present user
# pwd present working directory
# uname displays information about the current system
# uname –n / # uname --nodename
# whereis to find where a command (or its man pages) is located
# useradd add a new user to the system
# passwd change the password of the user
$ ls list files, directories
$ ls –l list files with details
$ ls –lh list details of files with human friendly format
$ ls –al list hidden files
$ ls –a /home/student
$ ls –all long format
# date
# watch ls -lh
# watch du ./newfile.txt
# cat view files
# more
# less
# touch [file name] create new file
# mv [source file] [destination file] move a file to a new location
# mv /home/test/test.txt /home/new/test.txt
# cp [source file] [destination file] copy a file
# cp –r /home/student /tmp r = recursive
# mkdir [directory name] create a directory
# rmdir [directory name] delete a directory
# rm [file name] delete a file
# rm -rf [file name] forcefully delete a file
# rm -rf dir [directory name]/ forcefully delete a directory
$ cd change directory
# su login to root without changing environment
# su - login to root
# su – [user name] switch user
# echo $HOME current user directory
# vim /etc/sudoers allows particular users to run various commands as the root user, without needing
Dumidu Senanayake Page 3 of 10
Linux Commands
the root password.
[user name] ALL=(ALL) ALL
# env show environment variables
# env | less
~ home directory
.. previous directory
# du –sh /home
# du –h /home/student
# du –h free space
# mount file systems currently mounted
yum install samba samba-client cifs-utils
mkdir /mnt/mymount
mount.cifs //10.11.10.26/snaps /mnt/mymount -o user=Girish.KG
umount /mnt/mymount
Process ID
# nice –n -5 update db
# renice –n -2 –p 3128
# pidof [service] process id of a service
# pidof httpd
# pgrep [service] process ids of a service
# pgrep httpd
# kill [nice] [process id] (-9) forcefully exit (-15) finish the process and stop
# kill -15 3128
Search: # grep [search pattern] [search location]
# grep tty1 file1
# w | grep tty1
view the file | filter command
# cat /etc/passwd | grep student
# cat /etc/passwd View list of users in the system:
# vim /etc/inittab
# runlevel show current run level
Dumidu Senanayake Page 4 of 10
Linux Commands
# pstree show process tree
Ownership
# chown [root] .[group][file name]
# chown root file1 Change ownership of a file
# chown –R student.student ~/test change ownership of a directory
Permissions
# chmod [ugo][+-=] [rwx] [file name] u = user, g = group, o = other owners of the file
r = read, w = write, x = execute permissions
# chmod u+wx, go+r backup.sh
rwx rwx rwx
421 421 421
111 110 110
7 6 6
# chmod 766 test
SSH
Connect to a remote server
# ssh 192.168.1.1 login with the current user
# ssh –X [email protected] X - graphical configuration tools
Copy a file through ssh
# scp [user]@[destination pc ip add.]: [source file] [destination]
# scp
[email protected]: /home/allusers/test.txt /home/allusers
File archiving and compressing
Archive # tar -cvf [archive file] [directory]
# tar -cvf dir1.tar dir1
# tar –tvf Display the contents of a tar file
# tar –xvf Extract the contents of an archive
Zip # zip [output file zip] [source file]
# zip file1.txt.zip file1.txt
# unzip file1.txt.zip
Gzip # gzip [file name]
# gunzip [gz file]
Bzip2 # bzip2 [file name]
# bunzip2 [bz2 file]
Xz # xz [file name]
# unxz [xz file]
Dumidu Senanayake Page 5 of 10
Linux Commands
Links
Symbolic link: # ln –s [target] [file name]
# ln –s /etc/sysconfig/iptables /home/admin/Desktop/iptables
Hard link: # ln [target][filename]
# ln /etc/sysconfig/iptables /home/admin/Documents/iptables
Package Installation (YUM)
# vim /etc/yum.conf
*** if behind a proxy proxy = http://192.168.1.1:3128
Search packages
# yum search [package name]
# yum search httpd
Repository list
# yum repolist
# yum –enable repo=remi update firefox -y
Install package
#yum install [package name]
Bypass signature check
# yum install - -nogpgcheck [package name]
Uninstall a package
# yum remove [package name]
Yum processors
# ps aux | grep yum
Update a package
# yum update -y [package name] -y = yes
# yum history
# yum history info [transaction id]
# yum history undo [transaction id]
# yum shell
.> Install htap
.> remove ntfs-3g
.> run
Check package installed or not.
# rpm –qa q = quary a = all
# rpm -qa [package name]
# rpm -qa httpd*
# rpm -qa coreutils*
Dumidu Senanayake Page 6 of 10
Linux Commands
# rpm -q [package name] - -filesbypkg
# rpm -q httpd - -filesbypkg
# rpm -q coreutils - -filesbypkg less
View the content of a file
# rpm –qf [file name] shows the package owns files
# rpm –ql [package name] lists the files in the package
# rpm –V [package name] verifies package installation
# rpm –ivh [package file.rpm] -i = install v = verbose h = visual progress of installation
# rpm –Uvh [package file.rpm] -U = update
# rpm –evh [package file] -e = erase (uninstall)
# yum whatprovides /bin/ls
Special Devices
# vim /dev view information on devices
/dev/null
/dev/zero
File mounting
Check mounted file systems
# mount | column –t
# cat /etc/mtab
# cat /proc/mounts
# fdisk –l /dev/sda
Create a disk image
# dd if = /dev/cdrom of = /home/student/cd.img
# mount t [fs type] –o [mount option] [device] [mount point]
# mount –o loop /home/allusers/cdimage.iso /mnt -o = mount potion
# mount –o ro /dev/sda1 /mnt ro = read only
Mount a disk
# mount [device (flash drive)] [mount point]
# mount sd1 /home/student/dir1
# mount /dev/cdrom /mnt
Unmounts
# umount /media /[device (flash drive)]
# watch ls –lh
Dumidu Senanayake Page 7 of 10
Linux Commands
# watch du ./newfile
SELinux
# vim /etc/selinux/config [enforcing / permissive / disabled]
PAM (Pluggable Authentication Modules)
# vim /etc/pam.d
TCP Rappers
# vim /etc/hosts.allow
# vim /etc/hosts.deny
Networking
# vim /etc/sysconfig/network define host name and gateway
# vim /etc/sysconfig/network-scripts/ifcfg-eth0 network interface configuration
Multiple IP addresses for single interface
# vim /etc/sysconfig/network-scripts/ifcfg-eth0:1
# vim /etc/sysconfig/network-scripts/ifcfg-eth0:2
# system-config-network
# ifconfig
# ipaddr
# netstat
# tcpdump
# route view routing table
# route -n
# ping –c4 192.168.1.1
# service network [start / restart / stop / status]
# /etc/rc.d/init.d/network [start / stop]
Firewall (iptables)
Filtering point table
filter nat mangle
chain INPUT X X
OUTPUT X X
FORWARD X X X
PREROUTING X X
POSTROUTING X X
# iptables –L view firewall rules
# iptables –L –n view firewall rules with numerical values
# iptables –L –t [table name] view a specific table
# iptables –L –t filter
-A append
Dumidu Senanayake Page 8 of 10
Linux Commands
-D delete
Commands:
# iptables –A INPUT –S 172.17.9.66 –j DROP
# iptables –p tcp –m tcp –dport 25 –s [ip add.] –j ACCEPT
CRON
# at command
# crontab –l list all schedules
# crontab –l –u [user name] list all schedules specific to user
# crontab –l –u admin
# crontab –e edit schedules
# crontab –r remove schedules
# cat /etc/crontab cron configuration file
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
#| | | | |
# * * * * * user-name command to be executed
Ex:
Apache web server
# service httpd [start / restart / stop / status]
# vim /etc/httpd/conf/httpd.conf
# tail -f /var/log/httpd/access_log
# curl -I [domain name]
# curl -I www.kernel.org
Samba
# service smb [start / restart / stop / status]
# vim /etc/samba/smb.conf
# testparm check for syntax errors
# cat smbusers
# smbclient –L [ip address]
# smbclient –L 192.168.1.8
# smbclient –L [ip address] –U [username]
Dumidu Senanayake Page 9 of 10
Linux Commands
# smbclient –L 192.168.1.8 –U user1
Bind (DNS)
# service named [start / restart / stop / status]
# vim /etc/resolve.conf
# vim /etc/named.conf
# cat named.localhost
NTP (Network Time Protocol)
# service ntpd [start / restart / stop / status]
# vim /etc/ntp.conf
# System-config-date
NFS (Network File Sharing)
# service nfs [start / restart / stop / status]
# vim /etc/exports
/exports * (rw sync) * = all hosts in network / ip address of specific host
# exports
# exportfs –r reload exports
# mount –t nfs [network share] [mount point]
# mount –t nfs ip address:/exports /mnt
Check for file systems on hosts
# showmount –e [ip address]
# rpcinfo –p [ip address]
Squid Proxy
Dumidu Senanayake Page 10 of 10