Ubuntu and Debian Linux starter user guide
Useful Linux Command :
To have a root shell as root.
# sudo su
To have a root shell as /you/.
# sudo -s
Try both and do an echo $HOME to see the difference.
sudo su is the equivalent to sudo -i
Linux version
# uname -p version processeur i686 => x3
Taille partition disque
# df
Date check
# date
Logout session (Useful in case of permissions take effect)
# logout
Change keyboard to azerty for french language :
# loadkeys fr
Update the debian or ubuntu system :
# sudo apt-get update
What is my IP :
# ifconfig
Interface graphique (Kubuntu / prévoir 1Gb RAM)
# sudo apt-get install kubuntu-desktop
Show All Running Processes in Linux
ps command
Type the following ps command to display all running process see link :
# ps aux | less
Where,
- -A: select all processes
- a: select all processes on a terminal, including those of other users
- x: select processes without controlling ttys
Task: see every process on the system
# ps -A # ps -e
Task: See every process except those running as root
# ps -U root -u root -N
Task: See process run by user vivek
# ps -u vivek
Task: top command
The top program provides a dynamic real-time view of a running system. Type the top at command prompt:
# top
To quit press q, for help press h.
Task: display a tree of processes
pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown.
$ pstree
Task: Lookup process
Use pgrep command. pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to screen. For example display firefox process id:
$ pgrep firefox
Sample outputs:
3356
kill command syntax
The kill command causes the specified signal to be sent to the specified process. The kill command has the general form as follows:
kill -N PID
Where,
- N is a signal number
- PID is the Process Identification Number. If you do not know the PID, it can be learned through the ps command.
Understanding signal numbers
The signal number 1 is a hangup signal. I recommended using 1 signal because it should kill the process and it can save the buffer (if supported). For example if it is an editor, save the buffer. This is the default if you do not specify a signal number. Signal number 9, a kill signal, is the surest way to kill a process.
Some of the more commonly used signals:
signal # | Usage |
1 | HUP (hang up) |
2 | INT (interrupt) |
3 | QUIT (quit) |
6 | ABRT (abort) |
9 | KILL (non-catchable, non-ignorable kill) |
14 | ALRM (alarm clock) |
15 | TERM (software termination signal) |
How do I use kill command?
Terminate the processes with pids 1412 and 1157:
$ kill 1412 1157
Send the hangup signal (SIGHUP) to the process with pid 5071:
# kill -s HUP 5071
Determining Your Current Directory
While the general answer is pwd, note that this may give different results depending on how you reached a given directory and whether the route included symbolic links.
For instance if you have a directory called real, and a symbolic link to that directory called virtual and you cd
to the virtual
directory then pwd
will show that virtual
directory name, even though the actual directory you are in is real
.
To demonstrate:
$ mkdir real $ ln -s real virtual $ cd virtual $ pwd /home/<username>/virtual $ readlink -f . /home/<username>/real
Display username, hostname and current working directory in the prompt
The PS1 in this example displays the following three information in the prompt:
- \u – Username
- \h – Hostname
- \w – Full path of the current working directory
-bash-3.2$ export PS1="\u@\h \w> " ramesh@dev-db ~> cd /etc/mail ramesh@dev-db /etc/mail>
Make this setting permanent by adding export PS1=”\u@\h \w> ” to either .bash_profile (or) .bashrc as shown below.
ramesh@dev-db ~> vi ~/.bash_profile (or) ramesh@dev-db ~> vi ~/.bashrc
[Note: Add export PS1=”\u@\h \w> “ to one of the above files]
The grep command syntax
The syntax is as follows:
grep'word' filename grep'word' file1 file2 file3 grep'string1 string2' filename cat otherfile | grep 'something' command grep 'something' command option1 | grep 'data' grep --color 'data'fileName
How do I use grep command to search a file?
Search /etc/passwd file for boo user, enter:
$ grep boo /etc/passwd
Sample outputs:
foo:x:1000:1000:foo,,,:/home/foo:/bin/ksh
UNIX / Linux pipes and grep command
grep command often used with shell pipes. In this example, show the name of the hard disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo
Sample outputs:
model : 30 model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz model : 30 model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz
Change hostname
Type the following command :
# sudo hostname
Update file /etc/hostname
root@home:~# nano /etc/hostname
Update hostname home by dell
Do NOT Forget to Update alias file too /etc/hosts
root@home:~# nano /etc/hosts
Replace line : 127.0.0.1 home
by line : 127.0.0.1 dell
Register New host file name
root@home:~# /etc/init.d/hostname.sh start
root@home:~# hostname
dell
Verify Name Machine after reboot
root@dell:~#
Giving root access
Careful: The following command allows the user to execute sudo commands (root).
# or this one should work
sudo adduser mike sudo
To disable the root login use
# sudo passwd -l root
To change the default shell of the user to bash set the last entry of the corresponding user in the /usr/passwd
file to the /bin/bash following as in the following example.
testuser:x:1001:1001::/home/testuser:/bin/bash
Zipping files
To zip or unzip files on the command line you can use the following commands.
# Zip all pdf files in the ~/tmp/pdf/ diretory
zip ~/targetdir/myzip.zip ~/tmp/pdf/*.pdf
# Unzip the zip file
unzip ~/targetdir/myzip.zip
CREATE USER (with home directory) and group
Useradd –m nagios
Then create group nagios
Groupadd nagios
How to View and Delete Iptables Rules – List and Flush
1. View / List All iptables Rules
When you want to check what rules are in iptables, use –list option as shown below.
# iptables --list
Example 1: Iptables list output showing no rules
# iptables --list Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
The above output shows chain headers. As you see, there are no rules in it.
Example 2: Iptables list output showing some rules
When there is a rule to disable ping reply, you have the iptables list output as like the following. You can see the rule in the OUTPUT chain.
# iptables --list Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DROP icmp -- anywhere anywhere icmp echo-request
2. Delete iptables Rules using flush option
When you want to delete all the rules, use the flush option as shown below.
# iptables --flush
After doing this, your iptables will become empty, and the “iptables -–list” output will look like what is shown in the example 1.
You can also delete (flush) a particular iptable chain by giving the chain name as an argument as shown below.
# iptables --flush OUTPUT
3. Adding iptables Rules
iptables -A INPUT -p udp -m udp --dport 161 -j ACCEPT
iptables -A OUTPUT -p udp -m udp --sport 161 -j ACCEPT
List All Ports (both listening and non listening ports)
Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc.,
netstat supports a set of options to display active or passive sockets. The options –t, –u, –w, and –x show active TCP, UDP, RAW, or Unix socket connections. If you provide the –a flag in addition, sockets that are waiting for a connection (i.e., listening) are displayed as well. This display will give you a list of all servers that are currently running on your system.
Some useful samples :
netstat -an | grep udp
netstat -nlpu | grep snmp
netstat -nlpt
Checking ports (telnet ip port_number)
To list tcp ports that are being listened on, along with the name of each listener’s daemon and its PID, run:
sudo netstat -plnt
The following example shows netstat’s output for three common programs that are listening on three different sockets.
$ sudo netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3686/mysqld tcp 0 0 :::443 :::* LISTEN 2218/httpd tcp 0 0 :::80 :::* LISTEN 2218/httpd tcp 0 0 :::22 :::* LISTEN
List all ports using netstat -a
# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN udp 0 0 *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket
List all tcp ports using netstat -at
# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN
List all udp ports using netstat -au
# netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:bootpc *:* udp 0 0 *:49119 *:* udp 0 0 *:mdns *:*
Locating Files :
The find
command is used to locate files on a Unix or Linux system. find
will search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all subdirectories too. The syntax looks like this:
find where-to-look criteria what-to-do
All arguments to find
are optional, and there are defaults for all parts. (This may depend on which version of find
is used. Here we discuss the freely available Gnu version of find
, which is the version available on YborStudent
.) For example, where-to-look
defaults to .
(that is, the current working directory), criteria
defaults to none (that is, select all files), and what-to-do
(known as the find
action) defaults to ‑print
(that is, display the names of found files to standard output). Technically, the criteria and actions are all known as find
primaries.
For example:
find
will display the pathnames of all files in the current directory and all subdirectories. The commands
find . -print find -print find .
do the exact same thing. Here’s an example find
command using a search criterion and the default action:
find / -name foo
This will search the whole system for any files named foo
and display their pathnames. Here we are using the criterion -name
with the argument foo
to tell find
to perform a name search for the filename foo
. The output might look like this:
/home/wpollock/foo /home/ua02/foo /tmp/foo
If find
doesn’t locate any matching files, it produces no output.
The above example said to search the whole system, by specifying the root directory (“/
”) to search. If you don’t run this command as root, find
will display a error message for each directory on which you don’t have read permission. This can be a lot of messages, and the matching files that are found may scroll right off your screen. A good way to deal with this problem is to redirect the error messages so you don’t have to see them at all:
find / -name foo 2>/dev/null
You can specify as many places to search as you wish:
find /tmp /var/tmp . $HOME -name foo
How to check if package is installed (quickly)
On your terminal:
apt-cache policy bridge-utils
If it’s installed, it will display the version installed, if it’s not, it will display the lines of text below:
Code:
bridge-utils: Installed: (none) Candidate: 1.2-1build1 Version table: 1.2-1build1 0 500 http://archive.ubuntu.com gutsy/main Packages
How to check the installed packages list
Type the following command to get list of all installed software:
# dpkg --get-selections
How to delete a package install with apt-get
Simple delete without associated files :
sudo apt-get remove mon-paquet
Complete delete :
sudo apt-get purge mon-paquet
Ubuntu Openn SSH Installation
Type the following two command to install both ssh client and server:
# sudo apt-get install openssh-server openssh-client
# sudo start ssh
or you can use this command :
# sudo /etc/init.d/ssh start
Reboot Server Linux
Type the following command :
# sudo reboot or # sudo shutdown -r now
Restart Apache 2 web server, enter :
If you are using Ubuntu use sudo:
$ sudo /etc/init.d/apache2 restart
To stop Apache 2 web server, enter:
# sudo /etc/init.d/apache2 stop
Configuring a static IP address on Ubuntu / Debian
First of all perform :
ifconfig
Login to the server using your root account. Type the following to edit the interfaces file :
sudo nano /etc/network/interfaces
Look for the following line in the file:
iface eth0 inet dhcp
and change it to
iface etho inet static
Below that line add the following lines, changing the addresses as necessary
address 192.168.1.1 netmask 255.255.255.0 gateway 192.168.1.254
If you are using nano, Hit Ctrl-X, Y and enter to save and exit
Reboot the server for the static IP to take effect.
sudo reboot
Linux Server behind a corporate Firewall like TMG or Juniper :
If you are a proxy server so check the file /etc/apt/apt.conf
The contents were,
Acquire::http::proxy "http://<proxy>:<port>";
Acquire::ftp::proxy "ftp://<proxy>:<port>";
Acquire::https::proxy "https://<proxy>:<port>";
This was the reason why you could reach proxy but couldn’t get past it, since there is no username password information. So just put that info into it..
Acquire::http::proxy "http://<username>:<password>@<proxy>:<port>/";
Acquire::ftp::proxy "ftp://<username>:<password>@<proxy>:<port>/";
Acquire::https::proxy "https://<username>:<password>@<proxy>:<port>/";
save the file and you are done…
Note: More better add these lines in another file, /etc/apt/apt.conf.d/80proxy
. This will ensure that after a version upgrade changes won’t be lost.
For my corporate network I was given by my administrator the proxy name in the format:
http://[username]:[password]@[proxy-webaddress]:[port]
For our Windows network our username is in the format:
[domain]\[username]
For example:
http://mywindowsdomain\fossfreedom:password@askubuntu-proxy.com:8080
See How to install and setup apache2
See also how to set apache2 web permissions
See also how to set date.timezone in apache