How to make a shell script executable Linux CLI

This is a quick reference guide on how to make a shell script executable on Linux Based Operating Systems.

1. chmod +x

root@FTP:~# ./shell.sh
-bash: ./shell.sh: Permission denied
root@FTP:~# chmod +x shell.sh
root@FTP:~# ./shell.sh

Thank you for reading and please feel free to leave any feedback.

How to add a user to a group Linux CLI

This is a quick reference guide on how to check your network interfaces on Linux Based Operating Systems.

1. ifconfig -a

root@FTP1:~# addgroup ftpgroup user1
addgroup: The user `ftpgroup' does not exist.
root@FTP1:~# adduser user1 ftpgroup
Adding user `user1' to group `ftpgroup' ...
Adding user user1 to group ftpgroup
Done.
root@FTP1:~# adduser user2 ftpgroup
Adding user `user2' to group `ftpgroup' ...
Adding user user2 to group ftpgroup
Done.

Thank you for reading and please feel free to leave any feedback.

How to show interface statistics using netstat -i command Linux CLI

This is a quick reference on how to show interface statistics using the netstat -i command on Linux Based Operating Systems.

1. netstat -i

root@GNS3-Server:~# netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0    1500 0         0      0      0 0             0      0      0      0 BMU
eth0       1500 0       572      0      0 0           359      0      0      0 BMRU
eth1       1500 0        43      0      0 0            37      0      0      0 BMRU
lo        65536 0      3172      0      0 0          3172      0      0      0 LRU
tun1194    1500 0         0      0      0 0             0      0      0      0 MOPRU
virbr0     1500 0         0      0      0 0             0      0      0      0 BMU
root@GNS3-Server:~#

Thank you for reading and please feel free to leave any feedback.

How to show established tcp connections using the netstat command Linux CLI

This is a quick reference on how to show established tcp connections using the netstat command on Linux Based Operating Systems.

1. netstat

root@GNS3-Server:~# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 172.16.253.1:3080       172.16.253.1:43486      ESTABLISHED
tcp        0      0 172.16.253.1:43486      172.16.253.1:3080       ESTABLISHED
tcp        0      0 10.0.125.10:ssh         90.240.10.190:59675     ESTABLISHED

Thank you for reading and please feel free to leave any feedback.

How to check what ports are listening using the netstat -l command Linux CLI

This is a quick reference on how to check what ports are listening using the netstat – l command on Linux Based Operating Systems.

1. netstat -l

Proto refers to network protocol either tcp or udp, local address you will see the port that is currently listening and under state it will advise that the port is listening (LISTEN).

root@GNS3-Server:/tmp# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:3080                  *:*                     LISTEN
tcp        0      0 *:http                  *:*                     LISTEN
tcp        0      0 192.168.122.1:domain    *:*                     LISTEN
tcp        0      0 *:ssh                   *:*                     LISTEN
tcp        0      0 *:8003                  *:*                     LISTEN
tcp6       0      0 [::]:http               [::]:*                  LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN

Thank you for reading and please feel free to leave any feedback.

How to view files and logs using the head command Linux CLI

This is a quick reference on how to view files and logs using the head command on Linux Based Operating Systems.

The head command is a good way to check the first part of a file or the first logs recorded in a log file.

1. head

Will print the first 10 lines of a file

root@VPS2:/var/log# head syslog
Jun  6 15:26:53 VPS2 systemd[1]: Stopped Network Service.
Jun  6 15:26:53 VPS2 systemd-udevd[376]: Network interface NamePolicy= disabled on kernel command line, ignoring.
Jun  6 15:26:53 VPS2 systemd[1]: Starting Network Service...
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth1: Gained IPv6LL
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth0: Gained IPv6LL
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: Enumeration completed
Jun  6 15:26:53 VPS2 systemd[1]: Started Network Service.
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth1: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: lo: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth0: Configured
root@VPS2:/var/log#

2. head -n

You can specify the number lines printed by using -n option.

root@VPS2:/var/log# head -n 5 syslog
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: Enumeration completed
Jun  6 15:26:53 VPS2 systemd[1]: Started Network Service.
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth1: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: lo: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth0: Configured

Thank you for reading and please feel free to leave any feedback.

How to view files and logs using the tail command Linux CLI

This is a quick reference on how to view files and logs using the tail command on Linux Based Operating Systems.

The tail command is a good way to check part of a file or the last logs recorded in a log file. It also gives you the ability to check logs as they are generated.

1. tail

Will print the last 10 lines of a file

root@VPS2:/var/log# tail syslog
Jun  6 15:26:53 VPS2 systemd[1]: Stopped Network Service.
Jun  6 15:26:53 VPS2 systemd-udevd[376]: Network interface NamePolicy= disabled on kernel command line, ignoring.
Jun  6 15:26:53 VPS2 systemd[1]: Starting Network Service...
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth1: Gained IPv6LL
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth0: Gained IPv6LL
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: Enumeration completed
Jun  6 15:26:53 VPS2 systemd[1]: Started Network Service.
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth1: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: lo: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth0: Configured
root@VPS2:/var/log#

2. tail -n

You can specify the number lines printed by using -n option.

root@VPS2:/var/log# tail -n 5 syslog
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: Enumeration completed
Jun  6 15:26:53 VPS2 systemd[1]: Started Network Service.
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth1: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: lo: Link is not managed by us
Jun  6 15:26:53 VPS2 systemd-networkd[2958]: eth0: Configured

3. tail -f

This will track the file and display any additions to the file or log file.

tail -f syslog

Thank you for reading and please feel free to leave any feedback.

How to check the default gateway Linux CLI

This is a quick reference guide on how to check the default gateway on Linux Based Operating Systems.

2. ip route or ip r

[root@vps1 ~]# ip r
default via 10.0.125.254 dev ens3
10.0.125.0/24 dev ens3 proto kernel scope link src 10.0.125.11
[root@vps1 ~]# ip route
default via 10.0.125.254 dev ens3
10.0.125.0/24 dev ens3 proto kernel scope link src 10.0.125.11
[root@vps1 ~]#

Thank you for reading and please feel free to leave any feedback.

How to check network interfaces Linux CLI

This is a quick reference guide on how to check your network interfaces on Linux Based Operating Systems.

1. ifconfig -a

[root@vps1 ~]# ifconfig 
ens3: flags=4163  mtu 1500
        inet 10.0.125.11  netmask 255.255.255.0  broadcast 10.0.125.255
        inet6 fe80::aff:fe00:7d0b  prefixlen 64  scopeid 0x20
        ether 02:00:0a:00:7d:0b  txqueuelen 1000  (Ethernet)
        RX packets 1186  bytes 116523 (113.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 925  bytes 117753 (114.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6  bytes 416 (416.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 416 (416.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2. ip link show

[root@vps1 ~]# ip link show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens3:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 02:00:0a:00:7d:0b brd ff:ff:ff:ff:ff:ff

3. netstat -i

[root@vps1 ~]# netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens3      1500     1336      0      0 0          1022      0      0      0 BMRU
lo       65536        6      0      0 0             6      0      0      0 LRU

Thank you for reading and please feel free to leave any feedback.

How to change ownership of a file Linux CLI

This is a quick reference guide on how to change ownership of a file on Linux Based Operating Systems.

1. Change ownership of a file using chown command

[root@vps1 ~]# chown root file1
[root@vps1 ~]# ls -l file1
-rw-r--r-- 1 root root 0 Jun  4 11:06 file1
[root@vps1 ~]# chown vpsuser file1
[root@vps1 ~]# ls -l file1
-rw-r--r-- 1 vpsuser root 0 Jun  4 11:06 file1

2. Check the file permission using ls -l

root@vps:~# ls -l file1
-rwxr-x--x 1 root root 0 Jun  3 08:17 file1

A dash (-) indicates that the file is a regular file.

The letter (d) indicates that the file is a directory as per below:

A letter (l) preceding indicates a special file type called a symlink. A symlink is a pointer to another location in the file system.

drwxr-xr-x 3 root root       4096 Oct 18  2018 ufw

The first three letters above refer to the permission of the user so in this case rwx, so read, write, and execute.

The next three letters is for group r-x, so read and execute, but not write as indicated by the dash.

The next three letters is for other r-x, so read and execute, but not write as indicated by the dash. (so the same as group in this example)

The number 3 in this example after the permissions indicates the number of files or sub-directories contained within this particular directory.

The first name after the number (directory and file number) in the example below is the user or file owner. “vpsuser”

The second name refers to the files group. “group1”

-rwxr-x--x 1 vpsuser group1 0 Jun  3 08:17 file1
root@vps:~#

The remainder of the file details are the size of the file, the date and time that the file was created or last modified, and the file name.

Thank you for reading and please feel free to leave any feedback.