Linux How to Change Hostname

Linux How to Change Hostname

Hostname identifies a computer on the network and it is visible in the terminal. We set up hostname during the installation operating system, but sometimes we want to change it.

Use command “hostname” to change hostname:

hostname new_hostname

Will set the hostname of the system to new_hostname. Open a new terminal to verify change hostname.

If you want to change the hostname permanent use the command “sysctl kernel.hostname” to change it:

sysctl kernel.hostname=NewHostname

Output: kernel.hostname = Newhostname

Command hostname without any parameter prints the current hostname of the system.

Linux How to Extend Swap Space

Linux How to Extend Swap Space

Display swap usage summary

# swapon -s

Show how many space swap needs in megabytes

# free -m

Create new swap disk.

# fdisk /dev/sdb

Format new disk to swap format

# mkswap /dev/sdb1

Add new part of swap

# swapon /dev/sdb1

Verify if there is more space in swap space

# free -m

Append next line to file /etc/fstab.

/dev/sdb2 swap swap defaults 0 0

If you need old swap use

# swapoff /dev/sda6

Linux How to Execute Script

Linux How to Execute Script

There are many ways how to execute a script, the first one is to execute the script by specifying the interpreter:

bash script.sh
sh script.sh

For debugging use option “-x” to see what is being done.

bash -x script.sh

As an interpreter, you can use sh, ksh, csh, bash etc.

If you want to execute the script without specifying an interpreter, you need to set execute (+x) permission:

chmod +x script.sh

Then you can execute the script following way:

./script.sh

Another way how to run the script:

source script.sh

The script doesn’t need to execute permission in this example. If the script makes any changes to the environment, it will be visible after running the script, because commands in the script are executed in the current shell.

How to Make Software RAID in Linux

How to Make Software RAID in Linux

Add a new partition on the first physical disc:

# fdisk /dev/sda

Using the same option add a new partition on the second physical disc:

# fdisk /dev/sdb

Create a new RAID volume md0 type of RAID1 (raid level1):

# mdadm --create /dev/md0 --level=1 --raiddevices=2 /dev/sda7 /dev/sdb2

Check and verify addition:

# watch cat /proc/mdstat

Format the new RAID volume to ext3 filesystem:

# mkfs -t ext3 /dev/md0

Verify created RAID:

# mdadm --query /dev/md0

For more details enter:

# mdadm --query --detail /dev/md0

Create a directory for mounting RAID volume:

# mkdir /mirror

Mount RAID volume md0:

# mount /dev/md0 /mirror

Verify:

# df -hT

Let’s try to create directory /etc and file numbers.txt:

# cp -ar /etc /mirror
# seq 10000000 > /mirror/cisla.txt

Verify:

# ls -ltr /mirror
# tail /mirror/subor.txt

You can anytime monitor volume [UU]:

# cat /proc/mdstat

We need to configure automatic mounting volume md0 after the system starts:

# cat /etc/mtab | grep md0
# joe /etc/fstab

Append next lines at the end of file /etc/fstab:

/dev/md0 /mirror ext3 defaults 0 0

Append next lines at the end of file /etc/mdadm.conf. If any error occurred, the system will send an email about it.

DEVICE /dev/sd[a-z]*
ARRAY /dev/md0 level=raid1 devices=/dev/sda7,/dev/sdb2
MAILADDR root@localhost

Turn the monitoring service on:

# service mdmonitor start

Try to make disc down:

# yes 'We love linux' > /mirror/linux.txt

Caution: yes command will repeatedly output the string until killed.
If you pull out the disc during the yes command you should get an email with the error message:

# mutt

We can see one disc is not available [_U]:

# cat /proc/mdstat

Removing partitions that physically don’t exist:

# mdadm /dev/md0 --detached

Removing partitions that physically exist:

# mdadm --manage /dev/md0 --remove /dev/sdb2

Connect physical disc and monitor how it is being synchronized:

# mdadm --manage /dev/md0 --add /dev/sdb2
# watch cat /proc/mdstat

Test RAID functionality. We should see “active raid1”:

# cat /proc/mdstat

verify RAID:

# mdadm --detail /dev/md0

Disable RAID:

# umount /dev/md0
# mdadm --manage --stop /dev/md0

Test RAID functionality again, we should not see “active raid1”:

# cat /proc/mdstat

RAID: removing

Unmount mounted partition:

# umount /mirror

Stop RAID (to start it again use –assemble –scan):

# mdadm --stop /dev/md0

Remove RAID:

# mdadm --remove /dev/md0

RAID partitions are readable, we can mount it:

# mount /dev/sdb2 /mnt

We should see details about RAID:

# ls -ltrh /mnt

Unmount RAID partition:

# umount /dev/sdb2

Linux Find String in Folder

Linux Find String in Folder

In our examples, we assume, that we want to find the name of the network card “eno16777728” in /etc folder recursively.

This example search “eno16777728” in /etc folder (-r means recursive, -n means print line number):

grep -nr "eno16777728" /etc

In next example, we would like to add ignore case with “i” option:

grep -inr "eno16777728" /etc

If you are not super user, it is good idea to suppress error messages with “s” option:

grep -insr "eno16777728" /etc

In examples mentioned before, searched string could be also a part some string. In next example, we would like to find “eno16777728” as whole word only (w option):

grep -winsr "eno16777728" /etc

The alternative way is use find and exec option:

find /etc -type f -exec grep -il 'eno16777728' {} \;

In this case option exec executes grep command for each founded file. In {} is a list of founded files.

Linux How to Remove Directory

Linux How to Remove Directory

To remove an empty directory use the command:

rmdir directory/

To remove a directory which contains files or sub-directories use this way:

rm -r not-emty-directory/

If you want to ignore nonexistent files and never prompt, use the option “f”:

rm -rf not-empty-directory/

Command rm has option “v” which explains what is being done.

rm -rv directory

If you don’t have permision to delete folder add sudo at the beginning:

sudo rm -rv directory

If you are not 100 % sure if you won’t remove all the files in directory, use -i option:

rm -rvi directory

Shell will ask you before removing each file.

Linux How to Find File by Name

Linux How to Find File by Name

To search for files on the disk, you can use the find command. The find command has the following syntax:

find /where_to_start -name "name_of_file"

If you do not mention the parameter /where_to_start, it will automatically search in the current directory. The current directory, which you currently stand, is available by typing pwd command. The second parameter -name “name_of_file” – is shown filter. This filter shows only those files in which there is a string “name_of_file”. You can also include an asterisk, for example: “name_of_file.*”.

If you want to recursively overview /etc directory and find all files that have the extension “.conf”, you can do it this way:

find /etc -name "*.conf"

If the find command does not access to any folder, it write error about it. If you do not run the command as super user, it is better to redirect error messages to $HOME/find_errors or to trash /dev/null.

In next example we redirect errors to file find_errors that will be situated in our home folder:

find /etc -name "*.conf" 2> $HOME/find_errors

In next example we redirect errors to the system trash:

find /etc -name "*.conf" 2> /dev/null

To find file by a name, ignoring the case use option -iname:

find /etc -iname "name_of_file"

If you want to find all files these don’t match the pattern:

find /etc -not -name ".*.conf"

JOE Tutorial for Linux

JOE Tutorial for Linux

Start joe:

joe ./textFile.txt

Show help:

Ctrl + K + H (to hide help, enter this command again)

Save file

Ctrl + K + D

Save and exit:

Ctrl + K + X

Exit without saving:

Ctrl + K + Q

Go to previous/next screen:

Ctrl + U/Ctrl + V

Move cursor to the start/end of file file:

Ctrl + K + U/Ctrl + K + V

UnDo recent change:

Ctrl + Shift + _

ReDo undone change:

Ctrl + Shift + ^

Insert or overwrite:

Ctrl + T

Selecting text:

Ctrl + K + B (block begin)
Ctrl + K + K (block end)

When you have block selected, to move block using:

Ctrl + K + M

Delete block:

Ctrl + K + Y

Copy block:

Ctrl + K + C

Search in the file:

Ctrl + K + F

Then choose between ignore (I), replace (R) options. To navigate to next result use:

Ctrl + L

If you want to change joe’s default setting use:

Ctrl + T

You can turn on/off auto-indent, word wrap, line numbers, highlighting and set tab width, left margin, etc.

Linux How to Change Password

Linux How to Change Password

If you want to change password of user that are currently logged in, just type:

passwd

Input old password, then type new password 2 times.

Did you run the passwd command as root? There is a difference between whether you change the password as an root or as a standard user. While the root is called upon to enter a password, the password policy more fully. But if he fails, the password is changed, even though the system grumble. Current user must comply with password policy.

Let’s assume, that you are user bob. If you would like to change bob’s password, log as bob and type:

passwd

If you are root, and if you want to change bob’s password, type:

passwd bob

Please, always try to check, if you set good new password. There is a possibility, that you misspell the password, so try to log in to second console (or putty session) immediately after login.

You can also set the following options:

Option Property
-d Delete password for an account. It will set a passwordless account.
-e Expire. The user will be forced to change the password in the next login.
-n Minimum password lifetime.
-x Maximum password lifetime.

 

Linux How to Zip

Linux How to Zip

To create a compressed file and save disk space use following the following syntax:

zip options name.zip file1 file2 file3 folder

The zip program compresses one or more files into the name.zip archive. Zip has options from “1” to “9”. Option -1 use for fast compression and -9 for better compress ratios.

zip -9 name.zip file1 file2

If you want to insert a new file to the existing zip archive use options “u” which means update.

zip -u name.zip newFile

To decompress the zip archive use command:

unzip name.zip

To decompress to a specific directory use the “d” option:

unzip name.zip -d /directory

Command tar is a great tool too. You can compress files with tar:

tar -zcvf name.tgz file1 file2 file3 folder

To unzip compressed file type:

tar -zxvf name.tgz

NANO Tutorial for Linux

NANO Tutorial for Linux

Start nano:

nano textFile.txt

Show help:

Ctrl + G

Hide help:

Ctrl + X

Save file:

Ctrl + O

Save and exit:

Ctrl + X then answer Y

Exit without saving:

Ctrl + X then answer N

Go to previous/next line:

Ctrl + P/Ctrl + N

Go to previous/next screen:

Ctrl + Y/Ctrl + V

Go to the line and column number:

Ctrl + _

Insert another file into the current file:

Ctrl + R, enter path to the file

Search for string or regular expression:

Ctrl + W

Cut the current line and store it in the cutbuffer:

Ctrl + K

Uncut from the cutbuffer to the current line:

Ctrl + U

Copy the current line to cut buffer:

Alt + 6 or Alt + ^